forked from ohayo-jacob/takunomi-blog
refactored blog
This commit is contained in:
parent
ea6b16d6ab
commit
29745008c0
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
@ -22,19 +23,8 @@ var templates = template.Must(template.ParseFiles("templates/blog.tmpl", "templa
|
|||
|
||||
func main() {
|
||||
|
||||
NewPostsCollection()
|
||||
|
||||
http.HandleFunc("/about/", aboutHandler)
|
||||
http.HandleFunc("/past/", pastHandler)
|
||||
http.HandleFunc("/contact/", contactHandler)
|
||||
http.HandleFunc("/post/", postHandler)
|
||||
http.HandleFunc("/", pastHandler)
|
||||
//http.HandleFunc("/", blogHandler)
|
||||
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images"))))
|
||||
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
|
||||
|
||||
//posts = PostsCollection[:]
|
||||
setPostsCollection()
|
||||
buildHandlers()
|
||||
retrievePosts()
|
||||
|
||||
if os.Args[1] == "local" {
|
||||
http.ListenAndServe(":8080", nil)
|
||||
|
@ -46,8 +36,21 @@ func main() {
|
|||
|
||||
}
|
||||
|
||||
func setPostsCollection() {
|
||||
posts = PostsCollection[:]
|
||||
func retrievePosts() {
|
||||
posts = nil
|
||||
filepath.Walk("./posts", newPost)
|
||||
posts = reversePosts()
|
||||
}
|
||||
|
||||
func buildHandlers() {
|
||||
http.HandleFunc("/about/", aboutHandler)
|
||||
http.HandleFunc("/past/", pastHandler)
|
||||
http.HandleFunc("/contact/", contactHandler)
|
||||
http.HandleFunc("/post/", postHandler)
|
||||
http.HandleFunc("/update", updateHandler)
|
||||
http.HandleFunc("/", pastHandler)
|
||||
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images"))))
|
||||
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
|
||||
}
|
||||
|
||||
func errorHandler(w http.ResponseWriter, req *http.Request, status int) {
|
||||
|
@ -57,20 +60,6 @@ func errorHandler(w http.ResponseWriter, req *http.Request, status int) {
|
|||
}
|
||||
}
|
||||
|
||||
func blogHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.Path != "/" {
|
||||
errorHandler(w, req, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
length := len(posts)
|
||||
posts = posts[:3]
|
||||
//posts = getNewestPosts(3)
|
||||
page := Page{name, posts, "past"}
|
||||
//page := Page{name, posts, "blog"}
|
||||
renderTemplates(w, page)
|
||||
posts = posts[:length]
|
||||
}
|
||||
|
||||
func postHandler(w http.ResponseWriter, r *http.Request) {
|
||||
title := strings.TrimPrefix(r.URL.Path, "/post/")
|
||||
posts := []Post{getPostByURLTitle(title)}
|
||||
|
@ -93,6 +82,10 @@ func contactHandler(w http.ResponseWriter, r *http.Request) {
|
|||
renderTemplates(w, page)
|
||||
}
|
||||
|
||||
func updateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
retrievePosts()
|
||||
}
|
||||
|
||||
func renderTemplates(w http.ResponseWriter, p Page) {
|
||||
s1 := templates.Lookup("blog.tmpl")
|
||||
err := s1.ExecuteTemplate(w, "blog", p)
|
|
@ -6,7 +6,6 @@ import (
|
|||
"html/template"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -22,54 +21,7 @@ type Post struct {
|
|||
|
||||
const htmlDateLayout = "2006-01-02"
|
||||
const normalDateLayout = "02 Jan 2006"
|
||||
|
||||
var modTime time.Time
|
||||
var PostsCollection []Post
|
||||
|
||||
func NewPostsCollection() {
|
||||
verifyFolderModification()
|
||||
initializeCollection()
|
||||
go acquirePosts(100)
|
||||
}
|
||||
|
||||
func initializeCollection() {
|
||||
PostsCollection = nil
|
||||
filepath.Walk("./posts", newPost)
|
||||
PostsCollection = reversePosts()
|
||||
}
|
||||
|
||||
func acquirePosts(duration time.Duration) {
|
||||
for {
|
||||
if verifyFolderModification() == true {
|
||||
initializeCollection()
|
||||
setPostsCollection()
|
||||
}
|
||||
time.Sleep(duration * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func verifyFolderModification() bool {
|
||||
f, err := os.Open("./posts")
|
||||
if err != nil {
|
||||
fmt.Println("file-error here: \n", f)
|
||||
f.Close()
|
||||
return false
|
||||
}
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
fmt.Println("info here: \n", info)
|
||||
f.Close()
|
||||
return false
|
||||
}
|
||||
_modTime := info.ModTime()
|
||||
if modTime != _modTime {
|
||||
modTime = _modTime
|
||||
f.Close()
|
||||
return true
|
||||
}
|
||||
f.Close()
|
||||
return false
|
||||
}
|
||||
|
||||
func newPost(path string, f os.FileInfo, err error) error {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
|
@ -95,7 +47,7 @@ func newPost(path string, f os.FileInfo, err error) error {
|
|||
template.HTML(string(content[:len(content)])),
|
||||
}
|
||||
|
||||
PostsCollection = insertPostAccordingToDate(p, PostsCollection)
|
||||
posts = insertPostAccordingToDate(p, posts)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -123,25 +75,16 @@ func getDate(n *html.Node) time.Time {
|
|||
}
|
||||
|
||||
func reversePosts() []Post {
|
||||
length := len(PostsCollection)
|
||||
length := len(posts)
|
||||
s := make([]Post, length)
|
||||
for i := 0; i < length; i++ {
|
||||
s[i] = PostsCollection[length-(i+1)]
|
||||
s[i] = posts[length-(i+1)]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func getNewestPosts(numberOfPosts int) []Post {
|
||||
var posts []Post
|
||||
l := len(PostsCollection)
|
||||
for i := l - 1; i >= l-numberOfPosts; i-- {
|
||||
posts = append(posts, PostsCollection[i])
|
||||
}
|
||||
return posts
|
||||
}
|
||||
|
||||
func getPostByURLTitle(title string) Post {
|
||||
for _, post := range PostsCollection {
|
||||
for _, post := range posts {
|
||||
if post.URLTitle == title {
|
||||
return post
|
||||
}
|
||||
|
@ -159,6 +102,6 @@ func insertPostAccordingToDate(post Post, arr []Post) []Post {
|
|||
return s
|
||||
}
|
||||
}
|
||||
PostsCollection = append(PostsCollection, post)
|
||||
return PostsCollection
|
||||
posts = append(posts, post)
|
||||
return posts
|
||||
}
|
Loading…
Reference in New Issue
Block a user