made automatic updates possible

This commit is contained in:
Jacob 2017-12-06 20:04:37 +01:00
parent 0773a48f2c
commit 87da73a5de
2 changed files with 11 additions and 3 deletions

View File

@ -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)
}

View File

@ -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"}