generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_server.go
75 lines (62 loc) · 1.85 KB
/
run_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cmd
import (
"net/http"
"time"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
"github.com/phyber/negroni-gzip/gzip"
"github.com/resonatecoop/id/log"
"github.com/resonatecoop/id/services"
"github.com/unrolled/secure"
"github.com/urfave/negroni"
"gopkg.in/tylerb/graceful.v1"
)
// RunServer runs the app
func RunServer(configBackend string) error {
cnf, db, err := initConfigDB(true, true, configBackend)
if err != nil {
return err
}
defer db.Close()
// start the services
if err := services.Init(cnf, db); err != nil {
return err
}
defer services.Close()
secureMiddleware := secure.New(secure.Options{
FrameDeny: false, // already set in web/render.go
ContentTypeNosniff: true,
BrowserXssFilter: true,
IsDevelopment: cnf.IsDevelopment,
})
// Start a classic negroni app
app := negroni.New()
app.Use(negroni.NewRecovery())
app.Use(negroni.NewLogger())
app.Use(gzip.Gzip(gzip.DefaultCompression))
app.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNext))
app.Use(negroni.NewStatic(http.Dir("public")))
// Create a router instance
router := mux.NewRouter()
// Add routes
services.HealthService.RegisterRoutes(router, "/v1")
services.OauthService.RegisterRoutes(router, "/v1/oauth")
services.WebHookService.RegisterRoutes(router, "/webhook")
webRoutes := mux.NewRouter()
services.WebService.RegisterRoutes(webRoutes, "/web")
CSRF := csrf.Protect(
[]byte(cnf.CSRF.Key),
csrf.Path("/"),
csrf.SameSite(csrf.SameSiteLaxMode),
csrf.TrustedOrigins([]string{cnf.CSRF.Origins}),
)
router.PathPrefix("").Handler(negroni.New(
negroni.Wrap(CSRF(webRoutes)),
))
// Set the router
app.UseHandler(router)
log.INFO.Printf("Starting server on localhost%v", cnf.Port)
// Run the server on port 8080 by default, gracefully stop on SIGTERM signal
graceful.Run(cnf.Port, 5*time.Second, app)
return nil
}