-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (77 loc) · 2.43 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"github.com/bseto/BlogEngine/logger"
"github.com/gorilla/mux"
"html/template"
"net/http"
"path/filepath"
)
type GeneralPage struct {
ActiveTab string
}
type Article struct {
Title string `json:"title"`
Path string `json:"path"`
Tags []string `json:"tags, omitempty"`
Body string `json:"body, omitempty"`
CreateDate string `json:"create_date"`
}
type ListYMLStruct struct {
List []YMLStruct `yaml:"articles"`
}
type YMLStruct struct {
Title string `yaml:"title"`
Path string `yaml:"path"`
CreateDate string `yaml:"create_date"`
Tags []string `yaml:"tags"`
}
func Home(w http.ResponseWriter, req *http.Request) {
page := GeneralPage{ActiveTab: "Home"}
logger.Log("Inside Home")
RenderTemplate(w, page,
filepath.Join("tmpl", "home.html"))
}
func Articles(w http.ResponseWriter, req *http.Request) {
page := GeneralPage{ActiveTab: "Articles"}
logger.Log("Inside List Articles")
RenderTemplate(w, page,
filepath.Join("tmpl", "articles.html"))
}
func Playground(w http.ResponseWriter, req *http.Request) {
page := GeneralPage{ActiveTab: "Playground"}
logger.Log("Inside Playground")
RenderTemplate(w, page,
filepath.Join("tmpl", "playground.html"))
}
func RenderTemplate(w http.ResponseWriter, p interface{}, tmplName ...string) {
logger.Log("Rendering templates:%v ", tmplName)
layout := filepath.Join("tmpl", "layout.html")
tmplName = append(tmplName, layout)
tmpl, err := template.ParseFiles(tmplName...)
if err != nil {
logger.Error("Could not parse all template files: %v", err)
//If err, then we will make tmpl 404
notFound := filepath.Join("tmpl", "404.html")
tmpl = template.Must(template.ParseFiles(layout, notFound))
}
if err := tmpl.ExecuteTemplate(w, "layout", p); err != nil {
logger.Log("Couldn't execute template %s : %s", tmpl, err)
return
}
}
func main() {
logger.Log("Starting Server")
//This is required to serve the css files, or anything we have in /resources
fs := http.FileServer(http.Dir("./resources"))
http.Handle("/resources/", http.StripPrefix("/resources/", fs))
r := mux.NewRouter()
r.HandleFunc("/", Home)
r.HandleFunc("/articles", Articles)
//r.HandleFunc("/playground", Playground)
r.HandleFunc("/home", Home)
r.HandleFunc("/article/{article-title}", GetArticle)
r.HandleFunc("/api/list_articles", ListArticles)
http.Handle("/", r)
logger.Log("Listening on 8000")
http.ListenAndServe(":8000", nil)
}