fix context problems
This commit is contained in:
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "Launch Package",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}/backend/cmd/",
|
||||||
|
"args": ["serve"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
BIN
backend/blog.db
BIN
backend/blog.db
Binary file not shown.
@ -10,7 +10,7 @@ type authkey int
|
|||||||
const keyClaims = iota
|
const keyClaims = iota
|
||||||
|
|
||||||
func writeToContext(r *http.Request, claims *Claims) *http.Request {
|
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)
|
return r.WithContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ func (s *Service) Authenticated(next http.HandlerFunc, roles ...model.Role) http
|
|||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r = writeToContext(r, &claims)
|
r = writeToContext(r, &claims)
|
||||||
next(w, r)
|
next(w, r)
|
||||||
})
|
})
|
||||||
|
@ -2,6 +2,7 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@ -9,12 +10,14 @@ import (
|
|||||||
|
|
||||||
// Init initializes the database connection, auto-migrates models, and seeds a default post.
|
// Init initializes the database connection, auto-migrates models, and seeds a default post.
|
||||||
func Init() *gorm.DB {
|
func Init() *gorm.DB {
|
||||||
|
_, filerr := os.Open("./blog.db")
|
||||||
db, err := gorm.Open(sqlite.Open("./blog.db"))
|
db, err := gorm.Open(sqlite.Open("./blog.db"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
db.AutoMigrate(&Post{}, &Comment{}, &User{}, &InvalidJWT{})
|
db.AutoMigrate(&Post{}, &Comment{}, &User{}, &InvalidJWT{})
|
||||||
|
|
||||||
|
if filerr != nil {
|
||||||
db.Save(&Post{
|
db.Save(&Post{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
Title: "Hello World",
|
Title: "Hello World",
|
||||||
@ -32,6 +35,7 @@ The default login is:
|
|||||||
> Password: admin
|
> Password: admin
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,6 @@ export class CreatePostComponent {
|
|||||||
content: '',
|
content: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
constructor() {
|
|
||||||
setTimeout(() => {
|
|
||||||
this.post.set({ ...this.post(), title: 'adf' });
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
publish() {
|
publish() {
|
||||||
this.postsService.createPost(this.post());
|
this.postsService.createPost(this.post());
|
||||||
this.location.back();
|
this.location.back();
|
||||||
|
Reference in New Issue
Block a user