-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
100 lines (91 loc) · 2.66 KB
/
middleware.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
package main
import (
"fmt"
"net/http"
"github.com/dys2p/ordersystem/html"
"github.com/julienschmidt/httprouter"
)
type HandlerErrFunc func(http.ResponseWriter, *http.Request) error
func auth(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if sessionManager.Exists(r.Context(), "username") {
f(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
}
}
func client(f HandlerErrFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
var msg string
if err == ErrNotFound {
msg = "Die Seite wurde nicht gefunden, die Aktion ist nicht erlaubt oder du bist nicht angemeldet."
} else {
msg = fmt.Sprintf("Interner Fehler: %s", err.Error())
}
html.ClientError.Execute(w, struct {
AuthorizedCollID string
Msg string
}{
sessionCollID(r),
msg,
})
}
}
}
// requires authentication
func clientWithCollection(f func(w http.ResponseWriter, r *http.Request, coll *Collection) error) http.HandlerFunc {
return client(
func(w http.ResponseWriter, r *http.Request) error {
var collID = httprouter.ParamsFromContext(r.Context()).ByName("collid")
if len(collID) > 10 {
collID = collID[:10]
}
if sessionManager.GetString(r.Context(), "coll-id") != collID {
return ErrNotFound // not logged in, or into another collection
}
var coll, err = db.ReadColl(collID)
if err != nil {
return err
}
return f(w, r, coll)
},
)
}
func store(f HandlerErrFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
html.StoreError.Execute(w, err.Error())
}
}
}
func storeWithCollection(f func(w http.ResponseWriter, r *http.Request, coll *Collection) error) http.HandlerFunc {
return store(
func(w http.ResponseWriter, r *http.Request) error {
var collID = httprouter.ParamsFromContext(r.Context()).ByName("collid")
if len(collID) > 10 {
collID = collID[:10]
}
var coll, err = db.ReadColl(collID)
if err != nil {
return err
}
return f(w, r, coll)
},
)
}
func storeWithTask(f func(w http.ResponseWriter, r *http.Request, coll *Collection, task *Task) error) http.HandlerFunc {
return storeWithCollection(
func(w http.ResponseWriter, r *http.Request, coll *Collection) error {
// select task from collection, don't get it from the database (then we had to verifiy that it belongs to the given collection)
var taskID = httprouter.ParamsFromContext(r.Context()).ByName("taskid")
for _, t := range coll.Tasks {
if t.ID == taskID {
return f(w, r, coll, t)
}
}
return ErrNotFound
},
)
}