-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
110 lines (90 loc) · 3.13 KB
/
handler.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"html/template"
"io"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
)
type Handler struct {
db *gorm.DB
router *mux.Router
config Config
tpls map[string]*template.Template
}
type ContextKey uint32
const (
KeyAccount ContextKey = iota
KeyMessage
)
func NewHandler(db *gorm.DB, config Config) Handler {
h := Handler{
db: db,
config: config,
}
h.loadTemplates()
h.newRouter()
return h
}
func (h *Handler) newRouter() {
r := mux.NewRouter()
r.Use(h.identify)
r.Use(logger)
r.HandleFunc("/api/records", h.auth(h.newRecord, Self, None)).Methods("POST")
r.HandleFunc("/api/records/{id}", h.auth(h.deleteRecord, Self, None)).Methods("DELETE")
r.HandleFunc("/api/accounts/{id}", h.auth(h.deleteAccount, None, All)).Methods("DELETE")
r.HandleFunc("/api/accounts/{id}", h.auth(h.updateAccountAuthority, None, Group)).Methods("PUT")
r.HandleFunc("/api/accounts", h.auth(h.findAccountByClassAndNumber, Self, Self)).Methods("GET")
r.HandleFunc("/api/stats", h.auth(h.statsList, Group, None))
r.HandleFunc("/api/login", h.login)
r.HandleFunc("/new", h.auth(h.newRecordPage, Group, None))
r.HandleFunc("/list", h.auth(h.listRecordsPage, Self, None))
r.HandleFunc("/accounts", h.auth(h.listAccountsPage, Self, Self))
r.HandleFunc("/stats", h.auth(h.stats, Group, None))
r.HandleFunc("/import", h.auth(h.importPage, None, All)).Methods("GET")
r.HandleFunc("/import", h.auth(h.importHandler, None, All)).Methods("POST")
r.HandleFunc("/export", h.auth(h.exportCSV, Group, None))
r.HandleFunc("/accounts/{id}", h.auth(h.profile, Self, Self))
r.HandleFunc("/login", h.page("login.htm")).Methods("GET")
r.HandleFunc("/login", h.login).Methods("POST")
r.HandleFunc("/doc/{title}", h.doc)
r.HandleFunc("/", h.index).Methods("GET")
r.HandleFunc("/", h.auth(h.newSelfRecord, Self, None)).Methods("POST")
r.Handle("/reset", h.auth(h.resetPage, None, Self)).Methods("GET")
r.Handle("/reset", h.auth(h.resetPassword, None, Self)).Methods("POST")
r.HandleFunc("/logout", logout)
r.HandleFunc("/register", h.auth(h.registerPage, None, All)).Methods("GET")
r.HandleFunc("/register", h.auth(h.register, None, All)).Methods("POST")
r.HandleFunc("/manifest.json", serveFile("manifest.json"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
h.router = r
}
func serveFile(file string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, file)
}
}
func (h Handler) Router() *mux.Router {
return h.router
}
func logger(next http.Handler) http.Handler {
return handlers.CustomLoggingHandler(os.Stdout, next, logFormat)
}
func logFormat(writer io.Writer, params handlers.LogFormatterParams) {
acct, ok := session(params.Request)
if !ok {
acct.ID = "-"
}
_, err := fmt.Fprintf(writer, `%s - %s %s "%s %s %s" %d %d`,
params.Request.RemoteAddr, acct.ID, params.TimeStamp.Format("2006-01-02 03:04:05"),
params.Request.Method, params.URL.RequestURI(), params.Request.Proto,
params.StatusCode, params.Size,
)
fmt.Fprintln(writer)
if err != nil {
fmt.Fprintln(writer, err)
}
}