-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
57 lines (51 loc) · 1.49 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
package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/cisco-gve/tviewer/controller"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
templates := populateTemplates()
controller.BasePath = os.Getenv("GOPATH") + "/src/github.com/cisco-gve/tviewer"
controller.Startup(templates, r)
log.Println("Listening in http://0.0.0.0:9090/web/")
http.ListenAndServe(":9090", r)
}
func populateTemplates() map[string]*template.Template {
result := make(map[string]*template.Template)
basePath := os.Getenv("GOPATH") + "/src/github.com/cisco-gve/tviewer/templates"
layout := template.Must(template.ParseFiles(basePath + "/_layout.html"))
template.Must(
layout.ParseFiles(basePath + "/_default_menu.html"))
dir, err := os.Open(basePath + "/content")
if err != nil {
panic("Failed to open template blocks directory: " + err.Error())
}
fis, err := dir.Readdir(-1)
if err != nil {
panic("Failed to read contents of content directory: " + err.Error())
}
for _, fi := range fis {
f, err := os.Open(basePath + "/content/" + fi.Name())
if err != nil {
panic("Failed to open template '" + fi.Name() + "'")
}
content, err := ioutil.ReadAll(f)
if err != nil {
panic("Failed to read content from file '" + fi.Name() + "'")
}
f.Close()
tmpl := template.Must(layout.Clone())
_, err = tmpl.Parse(string(content))
if err != nil {
panic("Failed to parse contents of '" + fi.Name() + "' as template")
}
result[fi.Name()] = tmpl
}
return result
}