-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
64 lines (54 loc) · 1.58 KB
/
blog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
"time"
)
// Represents a microblog - deals with articles and updating them.
type Blog struct {
ViewDir string
Views *template.Template
Pm PostManager
}
// Create a new blog that reads views and articles from the given directories.
func NewBlog(viewDir string, articleDir string) Blog {
return Blog{
ViewDir: viewDir,
Views: nil,
Pm: NewPostManager(articleDir),
}
}
// Update the state of the blog at the given interval of time. This should be run as a goroutine.
func (b *Blog) UpdateTask(updateInterval time.Duration) {
for {
var err error
b.Views, err = template.New("").Funcs(TemplateFunctions).ParseGlob(b.ViewDir + "/*")
if err != nil {
log.Fatal(err)
}
b.Pm.UpdatePosts()
time.Sleep(updateInterval)
}
}
// Handle the route for the blog's index.
func (b *Blog) GetPostList(w http.ResponseWriter, _ *http.Request) {
if err := b.Views.ExecuteTemplate(w, "index.gohtml", b.Pm.IndexedPosts); err != nil {
log.Println(err)
http.Error(w, "An internal server error occurred.", 500)
}
}
// Handle routes for posts on the blog.
func (b *Blog) GetPost(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// Find the post based on the permalink path
post, ok := b.Pm.Posts[PermaPath{vars["year"], vars["month"], vars["day"], vars["permaTitle"]}]
if !ok || !post.Visible {
http.Error(w, "Post not found", 404)
}
if err := b.Views.ExecuteTemplate(w, "post.gohtml", post); err != nil {
log.Println(err)
http.Error(w, "An internal server error occurred.", 500)
}
}