-
Notifications
You must be signed in to change notification settings - Fork 5
/
application.go
69 lines (51 loc) · 1.74 KB
/
application.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
package application
import (
"net/http"
"strings"
"github.com/carbocation/interpose"
gorilla_mux "github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/spf13/viper"
"controllers/handlerssd"
"controllers/jwt"
)
// New is the constructor for Application struct.
func New(config *viper.Viper, passwd string) (*Application, error) {
handlerssd.InitLongpoll()
cookieStoreSecret := config.Get("cookie_secret").(string)
jwt.Setup(config)
app := &Application{}
app.config = config
app.sessionStore = sessions.NewCookieStore([]byte(cookieStoreSecret))
handlerssd.SetPasswd(strings.TrimSpace(passwd))
return app, nil
}
// Application is the application object that runs HTTP server.
type Application struct {
config *viper.Viper
sessionStore sessions.Store
}
func (app *Application) MiddlewareStruct() (*interpose.Middleware, error) {
middle := interpose.New()
middle.UseHandler(app.mux())
return middle, nil
}
func (app *Application) mux() *gorilla_mux.Router {
router := gorilla_mux.NewRouter()
router.Handle("/", http.HandlerFunc(handlerssd.GetHome)).Methods("GET")
router.Handle("/sd/login", http.HandlerFunc(handlerssd.PostLogin)).Methods("POST")
router.Handle("/sd/longpoll", http.HandlerFunc(
jwt.Validate("user", "/",
handlerssd.GetState))).Methods("GET")
router.Handle("/sd/post", http.HandlerFunc(
jwt.Validate("user", "/",
handlerssd.PostState))).Methods("POST")
router.Handle("/sd/longpoll2/{where}", http.HandlerFunc(
jwt.Validate("user", "/",
handlerssd.GetState))).Methods("GET")
router.Handle("/sd/post2/{where}", http.HandlerFunc(
jwt.Validate("user", "/",
handlerssd.PostState))).Methods("POST")
// router.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
return router
}