From 55ae9a0f05aa810806a1158352c86d0355fbfb7f Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sat, 10 Aug 2024 10:18:43 -0500 Subject: [PATCH] Added draft blogs Fixed paths --- internal/blog/blog.go | 7 ++++--- main.go | 13 +++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/internal/blog/blog.go b/internal/blog/blog.go index 6fddc18..28acca3 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -22,6 +22,7 @@ type Blog struct { Title string `json:"title" bson:"title"` Blog string `json:"blog" bson:"blog"` StaticPage bool `json:"staticPage" bson:"staticPage"` + Draft bool `json:"draft" bson:"draft"` CreateTime int64 `json:"createTime" bson:"createTime"` UpdateTime int64 `json:"updateTime" bson:"updateTime"` } @@ -47,7 +48,7 @@ func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) { } func (b *BlogApp) Blog(ID string) (*Blog, error) { - res := b.blogCol.FindOne(context.Background(), bson.M{"_id": ID}) + res := b.blogCol.FindOne(context.Background(), bson.M{"_id": ID, "draft": false}) if res.Err() != nil { if res.Err() == mongo.ErrNoDocuments { return nil, backend.ErrNotFound @@ -178,8 +179,8 @@ func (b *BlogApp) updateBlog(w http.ResponseWriter, r *http.Request) { } func (b *BlogApp) LatestBlogs(page int64) ([]*Blog, error) { - res, err := b.blogCol.Find(context.Background(), bson.M{"staticPage": false}, options.Find(). - SetSort(bson.M{"createTime": 1}). + res, err := b.blogCol.Find(context.Background(), bson.M{"staticPage": false, "draft": false}, options.Find(). + SetSort(bson.M{"createTime": -1}). SetLimit(5). SetSkip(page*5)) if err != nil { diff --git a/main.go b/main.go index 8dfef04..367fcdd 100644 --- a/main.go +++ b/main.go @@ -105,14 +105,11 @@ func mainHandle(w http.ResponseWriter, r *http.Request) { return } spl := strings.Split(path, "/") - stat, err = os.Stat(filepath.Join(*webRoot, spl[0])) - if stat.IsDir() { - ind := filepath.Join(*webRoot, spl[0], "index.html") - stat, err = os.Stat(ind) - if err == nil { - http.ServeFile(w, r, ind) - return - } + ind := filepath.Join(*webRoot, spl[0], "index.html") + _, err = os.Stat(ind) + if err == nil { + http.ServeFile(w, r, ind) + return } blogHandle(w, r, path) }