website works with uploads now

This commit is contained in:
Jacob 2017-11-18 19:48:47 +01:00
parent 014d2f5af5
commit 34edb22c65
3 changed files with 32 additions and 27 deletions

View File

@ -24,26 +24,27 @@ const layout = "2006-01-02"
var modTime time.Time var modTime time.Time
var postsCollection []Post var postsCollection []Post
func InitializeCollection() { func NewPostsCollection() {
postsCollection = nil verifyFolderModification()
filepath.Walk("./posts", newPost) initializeCollection()
fmt.Println(len(postsCollection)) go acquirePosts(100)
for _, post := range postsCollection {
fmt.Println(post.Title)
}
} }
func AcquirePosts(duration time.Duration) { func initializeCollection() {
for {
if isFolderModified() == true {
postsCollection = nil postsCollection = nil
filepath.Walk("./posts", newPost) filepath.Walk("./posts", newPost)
}
func acquirePosts(duration time.Duration) {
for {
if verifyFolderModification() == true {
initializeCollection()
} }
time.Sleep(duration * time.Second) time.Sleep(duration * time.Second)
} }
} }
func isFolderModified() bool { func verifyFolderModification() bool {
f, err := os.Open("./posts") f, err := os.Open("./posts")
if err != nil { if err != nil {
fmt.Println("file-error here: \n", f) fmt.Println("file-error here: \n", f)
@ -66,7 +67,7 @@ func newPost(path string, f os.FileInfo, err error) error {
content, err := ioutil.ReadFile(path) content, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
//return err return nil
} }
title := strings.Replace(path, "posts/", "", -1) title := strings.Replace(path, "posts/", "", -1)
@ -75,7 +76,6 @@ func newPost(path string, f os.FileInfo, err error) error {
} }
s := string(content) s := string(content)
doc, _ := html.Parse(strings.NewReader(s)) doc, _ := html.Parse(strings.NewReader(s))
var p = Post{ var p = Post{
getDate(doc), getDate(doc),
title, title,
@ -83,25 +83,32 @@ func newPost(path string, f os.FileInfo, err error) error {
template.HTML(string(content[:len(content)])), template.HTML(string(content[:len(content)])),
} }
//insertPostAccordingToDate(p, postsCollection)
postsCollection = insertPostAccordingToDate(p, postsCollection) postsCollection = insertPostAccordingToDate(p, postsCollection)
fmt.Println(len(postsCollection))
for _, d := range postsCollection {
fmt.Println("dates are ", d.Date)
}
return nil return nil
} }
func getDate(n *html.Node) time.Time { func getDate(n *html.Node) time.Time {
var t time.Time var zero, d, t time.Time
if n.Type == html.ElementNode && n.Data == "time" { if n.Type == html.ElementNode && n.Data == "time" {
for _, a := range n.Attr { for _, a := range n.Attr {
if a.Key == "datetime" { if a.Key == "datetime" {
t, _ = time.Parse(layout, a.Val) t, _ = time.Parse(layout, a.Val)
return t
} }
} }
} }
for c := n.FirstChild; c != nil; c = c.NextSibling { for c := n.FirstChild; c != nil; c = c.NextSibling {
t = getDate(c) d = getDate(c)
if d != zero {
return d
}
} }
return t return t
@ -109,12 +116,9 @@ func getDate(n *html.Node) time.Time {
func getNewestPosts(numberOfPosts int) []Post { func getNewestPosts(numberOfPosts int) []Post {
var posts []Post var posts []Post
if len(postsCollection) == 0 { l := len(postsCollection)
return []Post{} for i := l - 1; i >= l-numberOfPosts; i-- {
} posts = append(posts, postsCollection[i])
for i := numberOfPosts; i > 0; i-- {
post := postsCollection[i]
posts = append(posts, getPostByURLTitle(post.URLTitle))
} }
return posts return posts
} }
@ -129,14 +133,16 @@ func getPostByURLTitle(title string) Post {
} }
func insertPostAccordingToDate(post Post, arr []Post) []Post { func insertPostAccordingToDate(post Post, arr []Post) []Post {
fmt.Println(post.Title)
for i, p := range arr { for i, p := range arr {
if p.Date.After(post.Date) { if p.Date.After(post.Date) {
s := make([]Post, len(arr)+1, cap(arr)+1) s := make([]Post, len(arr)+1, cap(arr)+1)
copy(s[:], arr[:]) //make a copy of the slice copy(s[:], arr[:]) //make a copy of the slice
copy(s[i+1:], arr[i:]) //move the upper part of the slice ahead, creating a hole copy(s[i+1:], arr[i:]) //move the upper part of the slice ahead, creating a hole
s[i] = post //insert new element into hole s[i] = post //insert new element into hole
return s
} }
} }
arr = append(arr, post) postsCollection = append(postsCollection, post)
return arr return postsCollection
} }

Binary file not shown.

View File

@ -22,8 +22,7 @@ var templates = template.Must(template.ParseFiles("blog.tmpl", "blog_roll.tmpl",
func main() { func main() {
InitializeCollection() NewPostsCollection()
go AcquirePosts(100)
http.HandleFunc("/", blogHandler) http.HandleFunc("/", blogHandler)
http.HandleFunc("/post/", postHandler) http.HandleFunc("/post/", postHandler)