-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (45 loc) · 1.46 KB
/
server.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
package server
import (
"net/http"
"strconv"
"github.com/GerardSoleCa/PubKeyManager/server/handlers"
"github.com/GerardSoleCa/PubKeyManager/usecases"
"github.com/codegangsta/negroni"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/op/go-logging"
"github.com/GerardSoleCa/PubKeyManager/infrastructure"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
var glog = logging.MustGetLogger("server")
// Server struct
type Server struct {
KeyInteractor *usecases.KeyInteractor
UserInteractor *usecases.UserInteractor
Configuration *infrastructure.Configuration
}
//Start loads, starts and bootstraps the server
func (s Server) Start() {
glog.Debug("Loading server module...")
router := configureRouter()
n := setupContext(router)
store := sessions.NewCookieStore(securecookie.GenerateRandomKey(256))
handlers.ConfigureAuthHandler(router, s.UserInteractor, store)
handlers.ConfigureKeysRouter(router, s.KeyInteractor, store)
handlers.ConfigureStaticRouter(router)
glog.Debugf("Server listening on port %d", s.Configuration.Port)
glog.Fatal(http.ListenAndServe(":"+strconv.Itoa(s.Configuration.Port), context.ClearHandler(n)))
}
func configureRouter() (router *mux.Router) {
router = mux.NewRouter().StrictSlash(false)
router.KeepContext = true
router.StrictSlash(true)
return router
}
func setupContext(router *mux.Router) *negroni.Negroni {
n := negroni.Classic()
n.UseHandler(router)
context.ClearHandler(n)
return n
}