-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
81 lines (67 loc) · 1.89 KB
/
http.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
package main
// Main HTTP server with Martini
import (
"github.com/campadrenalin/go-deje"
djlogic "github.com/campadrenalin/go-deje/logic"
"github.com/codegangsta/martini"
"github.com/martini-contrib/encoder"
"github.com/martini-contrib/render"
"net/http"
)
type Page struct {
Nav string
Data interface{}
Demo bool
}
type Handler func(render.Render)
func make_handler(tmpl string) Handler {
return func(r render.Render) {
r.HTML(200, tmpl, Page{Nav: tmpl})
}
}
// Middleware
func EncoderMiddleware(c martini.Context, w http.ResponseWriter) {
c.MapTo(encoder.JsonEncoder{}, (*encoder.Encoder)(nil))
w.Header().Set("Content-Type", "application/json; charset=utf-8")
}
func DocMiddleware(c martini.Context, req *http.Request, dc *deje.DEJEController, r render.Render) {
location, err := get_location(req)
if err != nil {
r.HTML(500, "error", Page{Data: err})
return
}
doc := dc.GetDocument(*location)
c.Map(doc)
}
func do_open(doc djlogic.Document, r render.Render) {
r.HTML(200, "console", Page{Data: doc})
}
// Events graph
func do_events(doc djlogic.Document, r render.Render, req *http.Request) {
demo_form_value := get_form(req, "demo")
demo := (len(demo_form_value) > 0 && demo_form_value != "0")
page := Page{
Data: doc,
Demo: demo,
}
r.HTML(200, "events", page, render.HTMLOptions{Layout: ""})
}
func do_notfound(r render.Render) {
r.HTML(404, "404", Page{})
}
func run_http(controller *deje.DEJEController) {
m := martini.Classic()
m.Map(controller)
m.Use(martini.Static("static"))
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
m.Get("/", make_handler("root"))
m.Get("/about", make_handler("about"))
m.Get("/help", make_handler("help"))
m.Get("/open", DocMiddleware, do_open)
m.Get("/events", DocMiddleware, do_events)
m.Get("/api/events", DocMiddleware, EncoderMiddleware, do_events_json)
m.NotFound(do_notfound)
http.ListenAndServe(":3000", m)
}