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 postsCollection []Post
func InitializeCollection() {
postsCollection = nil
filepath.Walk("./posts", newPost)
fmt.Println(len(postsCollection))
for _, post := range postsCollection {
fmt.Println(post.Title)
}
func NewPostsCollection() {
verifyFolderModification()
initializeCollection()
go acquirePosts(100)
}
func AcquirePosts(duration time.Duration) {
for {
if isFolderModified() == true {
func initializeCollection() {
postsCollection = nil
filepath.Walk("./posts", newPost)
}
func acquirePosts(duration time.Duration) {
for {
if verifyFolderModification() == true {
initializeCollection()
}
time.Sleep(duration * time.Second)
}
}
func isFolderModified() bool {
func verifyFolderModification() bool {
f, err := os.Open("./posts")
if err != nil {
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)
if err != nil {
fmt.Println(err)
//return err
return nil
}
title := strings.Replace(path, "posts/", "", -1)
@ -75,7 +76,6 @@ func newPost(path string, f os.FileInfo, err error) error {
}
s := string(content)
doc, _ := html.Parse(strings.NewReader(s))
var p = Post{
getDate(doc),
title,
@ -83,25 +83,32 @@ func newPost(path string, f os.FileInfo, err error) error {
template.HTML(string(content[:len(content)])),
}
//insertPostAccordingToDate(p, postsCollection)
postsCollection = insertPostAccordingToDate(p, postsCollection)
fmt.Println(len(postsCollection))
for _, d := range postsCollection {
fmt.Println("dates are ", d.Date)
}
return nil
}
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" {
for _, a := range n.Attr {
if a.Key == "datetime" {
t, _ = time.Parse(layout, a.Val)
return t
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
t = getDate(c)
d = getDate(c)
if d != zero {
return d
}
}
return t
@ -109,12 +116,9 @@ func getDate(n *html.Node) time.Time {
func getNewestPosts(numberOfPosts int) []Post {
var posts []Post
if len(postsCollection) == 0 {
return []Post{}
}
for i := numberOfPosts; i > 0; i-- {
post := postsCollection[i]
posts = append(posts, getPostByURLTitle(post.URLTitle))
l := len(postsCollection)
for i := l - 1; i >= l-numberOfPosts; i-- {
posts = append(posts, postsCollection[i])
}
return posts
}
@ -129,14 +133,16 @@ func getPostByURLTitle(title string) Post {
}
func insertPostAccordingToDate(post Post, arr []Post) []Post {
fmt.Println(post.Title)
for i, p := range arr {
if p.Date.After(post.Date) {
s := make([]Post, len(arr)+1, cap(arr)+1)
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
s[i] = post //insert new element into hole
return s
}
}
arr = append(arr, post)
return arr
postsCollection = append(postsCollection, post)
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() {
InitializeCollection()
go AcquirePosts(100)
NewPostsCollection()
http.HandleFunc("/", blogHandler)
http.HandleFunc("/post/", postHandler)