-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.go
More file actions
59 lines (49 loc) · 1.86 KB
/
web.go
File metadata and controls
59 lines (49 loc) · 1.86 KB
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
package main
import (
"bytes"
"html/template"
"net/http"
"github.com/mkernel/corespace-telegram-boardcomputer/web"
tmpl "github.com/mkernel/corespace-telegram-boardcomputer/web_templates"
)
//This is made to work using https://github.com/mjibson/esc/
//go:generate esc -o web/encoded.go -pkg web web
//go:generate esc -o web_templates/encoded.go -pkg web_templates web_templates
//go:generate esc -o admin_templates/encoded.go -pkg admin_templates admin_templates
var templates *template.Template
func transactionSum(txs []transaction) float64 {
var sum float64 = 0
for _, tx := range txs {
sum += tx.Value
}
return sum
}
func setupHTTP() {
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(web.Dir(false, "/web/"))))
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
http.HandleFunc("/", httpHandler)
var fmap = template.FuncMap{
"sum": transactionSum,
}
templates = template.New("page")
templates.Funcs(fmap)
templates.Parse(tmpl.FSMustString(false, "/web_templates/page.html"))
templates.Parse(tmpl.FSMustString(false, "/web_templates/ships.html"))
templates.Parse(tmpl.FSMustString(false, "/web_templates/crew.html"))
templates.Parse(tmpl.FSMustString(false, "/web_templates/inventory.html"))
templates.Parse(tmpl.FSMustString(false, "/web_templates/transactions.html"))
templates.Parse(tmpl.FSMustString(false, "/web_templates/contacts.html"))
go http.ListenAndServe(":8181", nil)
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.WriteHeader(200)
var tmplbuf bytes.Buffer
var crews []crew
database.Preload("Members").Preload("Items").Preload("Contacts").Preload("Transactions").Find(&crews)
templates.ExecuteTemplate(&tmplbuf, "ships", crews)
templates.ExecuteTemplate(w, "page", template.HTML(tmplbuf.String()))
} else {
w.WriteHeader(404)
}
}