From 87da73a5de7738d5b008f3f42f16698ceddba6fa Mon Sep 17 00:00:00 2001 From: jacobhartmann Date: Wed, 6 Dec 2017 20:04:37 +0100 Subject: [PATCH] made automatic updates possible --- postCollection.go | 6 ++++-- websiteTemplate.go | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/postCollection.go b/postCollection.go index b10d32c..ddc35b1 100644 --- a/postCollection.go +++ b/postCollection.go @@ -29,20 +29,22 @@ var PostsCollection []Post func NewPostsCollection() { verifyFolderModification() initializeCollection() - go acquirePosts(100) + go acquirePosts(10) } func initializeCollection() { PostsCollection = nil filepath.Walk("./posts", newPost) + fmt.Println(len(PostsCollection)) PostsCollection = reversePosts() } func acquirePosts(duration time.Duration) { for { if verifyFolderModification() == true { + fmt.Println("updated") initializeCollection() - PostsCollection = reversePosts() + setPostsCollection() } time.Sleep(duration * time.Second) } diff --git a/websiteTemplate.go b/websiteTemplate.go index 4e1d09f..fc0bb6d 100644 --- a/websiteTemplate.go +++ b/websiteTemplate.go @@ -32,7 +32,8 @@ func main() { http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images")))) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css")))) - posts = PostsCollection[:] + //posts = PostsCollection[:] + setPostsCollection() if os.Args[1] == "local" { http.ListenAndServe(":8080", nil) @@ -44,6 +45,10 @@ func main() { } +func setPostsCollection() { + posts = PostsCollection[:] +} + func errorHandler(w http.ResponseWriter, req *http.Request, status int) { w.WriteHeader(status) if status == http.StatusNotFound { @@ -57,6 +62,7 @@ func blogHandler(w http.ResponseWriter, req *http.Request) { return } length := len(posts) + fmt.Println("Hi, blog handler here. There are ", length, " posts.") posts = posts[:3] //posts = getNewestPosts(3) page := Page{name, posts, "blog"}