-
Notifications
You must be signed in to change notification settings - Fork 0
/
gornl.go
43 lines (36 loc) · 993 Bytes
/
gornl.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
package main
import (
"github.com/spf13/viper"
"html/template"
"net/http"
)
var (
templates = template.Must(template.ParseFiles(
"tmpl/header.html",
"tmpl/footer.html",
"tmpl/edit.html",
"tmpl/view.html"))
rootPath = "journal"
)
func init() {
viper.AddConfigPath("config")
viper.SetConfigName("gornl")
viper.SetDefault("ServerPort", "8080")
viper.SetDefault("ServerPrefix", "journal")
viper.SetDefault("JournalPath", "journals")
}
func main() {
journals := newJournalDB()
prefix := "/" + viper.GetString("ServerPrefix") + "/"
port := ":" + viper.GetString("ServerPort")
http.Handle(prefix, journalHandler{&journals})
http.Handle(prefix+"static/",
http.StripPrefix(prefix+"static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(port, nil)
}
func renderTemplate(w http.ResponseWriter, tmpl string, j *Journal) {
err := templates.ExecuteTemplate(w, tmpl+".html", j)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}