-
Notifications
You must be signed in to change notification settings - Fork 1
/
appTodo.go
66 lines (55 loc) · 1.49 KB
/
appTodo.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
package main
import (
"log"
"net/http"
"webstack/config"
"webstack/data"
"webstack/metier/todos"
"webstack/metier/users"
"webstack/web"
_ "github.com/go-sql-driver/mysql"
)
type FuncHandler struct {
HandlerFunc func(w http.ResponseWriter, r *http.Request)
}
func (h FuncHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.HandlerFunc(w, r)
}
var userAuth = web.TokenInfo{
CookieName: web.COOKIE_NAME,
PrivateKey: web.SECRET_KEY,
Auth: web.Auth{
Name: "user",
IsRequired: true,
},
}
func main() {
addhandler := FuncHandler{HandlerFunc: web.HandleAddTodo}
delhandler := FuncHandler{HandlerFunc: web.HandleDeleteTodo}
modhandler := FuncHandler{HandlerFunc: web.HandleModifyTodo}
todoshandler := FuncHandler{HandlerFunc: web.HandleGetTodos}
cfg := config.GetConfig()
msql, err := data.OpenDb(cfg)
if err != nil {
log.Fatal(err)
}
defer data.CloseDb()
err = todos.Init(msql)
if err != nil {
log.Fatal(err)
}
err = users.Init(msql)
if err != nil {
log.Fatal(err)
}
fs := http.FileServer(http.Dir(cfg.StaticDir))
http.Handle("/", fs)
http.HandleFunc("/add", web.WrapAuth(addhandler, userAuth))
http.HandleFunc("/delete", web.WrapAuth(delhandler, userAuth))
http.HandleFunc("/modify", web.WrapAuth(modhandler, userAuth))
http.HandleFunc("/todos", web.WrapAuth(todoshandler, userAuth))
http.HandleFunc("/signin", web.HandleSignin)
http.HandleFunc("/login", web.HandleLogin)
http.HandleFunc("/logout", web.HandleLogout)
http.ListenAndServe(cfg.Port, nil)
}