-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
89 lines (72 loc) · 1.61 KB
/
server.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
package main
import (
"encoding/json"
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/render"
"io/ioutil"
"log"
"net/http"
)
type Template struct {
Handlers []string
Tmpl string
Title string
}
type ServerList map[string]string
type Config struct {
Servers ServerList
Templates []Template
}
func GetConfig() Config {
bytes_data, err := ioutil.ReadFile("./config.json")
if err != nil {
log.Fatal(err)
}
var conf Config
err = json.Unmarshal(bytes_data, &conf)
if err != nil {
log.Fatal(err)
}
return conf
}
type ServerInfo struct {
Ip string
Name string
}
func getServerInfo(req *http.Request, s ServerList) ServerInfo {
info := ServerInfo{Ip: "0.0.0.0", Name: "dev"}
if hlist, ok := req.Header["Server-Ip"]; ok {
info.Ip = hlist[0]
}
if server_name, ok := s[info.Ip]; ok {
info.Name = server_name
}
return info
}
func handleTemplate(r render.Render, req *http.Request, t Template, s ServerList) {
tmpl_data := make(map[string]interface{})
tmpl_data["title"] = t.Title
tmpl_data["server"] = getServerInfo(req, s)
r.HTML(200, t.Tmpl, tmpl_data)
}
func setupTemplate(m *martini.ClassicMartini, t Template) {
handler := func(r render.Render, req *http.Request, s ServerList) {
handleTemplate(r, req, t, s)
}
for _, name := range t.Handlers {
m.Get("/"+name, handler)
}
}
func NewMartiniServer() *martini.ClassicMartini {
conf := GetConfig()
m := martini.Classic()
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
m.Use(martini.Static("root"))
m.Map(conf.Servers)
for _, t := range conf.Templates {
setupTemplate(m, t)
}
return m
}