diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..091c1c3 --- /dev/null +++ b/.vscode/launch.json @@ -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"] + } + ] +} \ No newline at end of file diff --git a/backend/blog.db b/backend/blog.db index cef8449..daf3010 100644 Binary files a/backend/blog.db and b/backend/blog.db differ diff --git a/backend/internal/auth/ctx.go b/backend/internal/auth/ctx.go index f61b246..bd62604 100644 --- a/backend/internal/auth/ctx.go +++ b/backend/internal/auth/ctx.go @@ -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) } diff --git a/backend/internal/auth/middleware.go b/backend/internal/auth/middleware.go index 848710f..cf48e28 100644 --- a/backend/internal/auth/middleware.go +++ b/backend/internal/auth/middleware.go @@ -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) }) diff --git a/backend/internal/model/init.go b/backend/internal/model/init.go index d6b3e16..aa1f97f 100644 --- a/backend/internal/model/init.go +++ b/backend/internal/model/init.go @@ -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 } diff --git a/frontend/src/app/routes/post/create-post/create-post.component.ts b/frontend/src/app/routes/post/create-post/create-post.component.ts index 5928d48..7c1163e 100644 --- a/frontend/src/app/routes/post/create-post/create-post.component.ts +++ b/frontend/src/app/routes/post/create-post/create-post.component.ts @@ -19,12 +19,6 @@ export class CreatePostComponent { content: '', }); - constructor() { - setTimeout(() => { - this.post.set({ ...this.post(), title: 'adf' }); - }, 1000); - } - publish() { this.postsService.createPost(this.post()); this.location.back();