fix context problems
This commit is contained in:
BIN
backend/blog.db
BIN
backend/blog.db
Binary file not shown.
@ -10,7 +10,7 @@ type authkey int
|
||||
const keyClaims = iota
|
||||
|
||||
func writeToContext(r *http.Request, claims *Claims) *http.Request {
|
||||
ctx := context.WithValue(r.Context(), claims, claims)
|
||||
ctx := context.WithValue(r.Context(), keyClaims, claims)
|
||||
return r.WithContext(ctx)
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ func (s *Service) Authenticated(next http.HandlerFunc, roles ...model.Role) http
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
r = writeToContext(r, &claims)
|
||||
next(w, r)
|
||||
})
|
||||
|
@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
@ -9,17 +10,19 @@ import (
|
||||
|
||||
// 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{})
|
||||
|
||||
db.Save(&Post{
|
||||
ID: 1,
|
||||
Title: "Hello World",
|
||||
TLDR: "introduction to ng-blog",
|
||||
Content: `
|
||||
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.
|
||||
@ -31,7 +34,8 @@ The default login is:
|
||||
>
|
||||
> Password: admin
|
||||
`,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
Reference in New Issue
Block a user