serve frontend from go
This commit is contained in:
37
internal/model/auth.go
Normal file
37
internal/model/auth.go
Normal file
@ -0,0 +1,37 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleAdmin Role = " admin"
|
||||
RoleUser Role = "user"
|
||||
RoleGuest Role = "guest"
|
||||
)
|
||||
|
||||
// InvalidJWT Represents a JWT that has expired or is otherwise invalid.
|
||||
type InvalidJWT struct {
|
||||
JWT string `gorm:"primarykey"`
|
||||
ValidUntil time.Time
|
||||
}
|
||||
|
||||
// User represents a user with an ID, UUID, name, role, and password.
|
||||
type User struct {
|
||||
ID uint `gorm:"primarykey" json:"-"`
|
||||
UUID uuid.UUID `gorm:"type:uuid" json:"uuid"`
|
||||
Name string `json:"name" gorm:"unique"`
|
||||
Role Role `json:"role"`
|
||||
Password []byte `json:"-"`
|
||||
}
|
||||
|
||||
// NewUser creates a new User struct with a generated UUID.
|
||||
func NewUser() User {
|
||||
return User{
|
||||
UUID: uuid.New(),
|
||||
}
|
||||
}
|
24
internal/model/blog.go
Normal file
24
internal/model/blog.go
Normal file
@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Post represents a blog post with associated comments and user ID.
|
||||
type Post struct {
|
||||
gorm.Model
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Title string `json:"title"`
|
||||
TLDR string `json:"tldr"`
|
||||
Content string `json:"content"`
|
||||
Comments []Comment
|
||||
UserID uint `gorm:"->;<-:create"`
|
||||
}
|
||||
|
||||
// Comment represents a comment on a post, including its ID, post association, content, and creator.
|
||||
type Comment struct {
|
||||
ID uint
|
||||
PostID uint
|
||||
Content string `json:"content"`
|
||||
UserID uint `gorm:"->;<-:create"`
|
||||
}
|
41
internal/model/init.go
Normal file
41
internal/model/init.go
Normal file
@ -0,0 +1,41 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Init initializes the database connection, auto-migrates models, and seeds a default post.
|
||||
func Init() *gorm.DB {
|
||||
_, filerr := os.Open("./blog.db")
|
||||
db, err := gorm.Open(sqlite.Open("./blog.db"))
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
db.AutoMigrate(&Post{}, &Comment{}, &User{}, &InvalidJWT{})
|
||||
|
||||
if filerr != nil {
|
||||
db.Save(&Post{
|
||||
ID: 1,
|
||||
Title: "Hello World",
|
||||
TLDR: "introduction to ng-blog",
|
||||
Content: `
|
||||
## Welcome
|
||||
|
||||
This is ng-blog, your simple blog written in Angular and Golang.
|
||||
|
||||
### Login
|
||||
|
||||
The default login is:
|
||||
> Username: admin
|
||||
>
|
||||
> Password: admin
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
Reference in New Issue
Block a user