-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
77 lines (60 loc) · 1.77 KB
/
app.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
package main
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type App struct {
service *Service
}
// Create a new app
func NewApp() (*App, error) {
rawAccessTokenExpiration := DefaultEnv("ACCESS_TOKEN_EXPIRATION", "15000")
accessTokenExpiration, err := strconv.Atoi(rawAccessTokenExpiration)
if err != nil {
// TODO: add logging
panic(err)
}
authenticator := NewUserServiceAuthenticator(
RequireEnv("SERVICE_MODEL_USER"),
DefaultEnv("ISSUER", "service.api.auth"),
accessTokenExpiration,
[]byte(RequireEnv("SIGNING_KEY")),
)
service := NewService(authenticator)
service.getParams = func(r *http.Request) map[string]string {
return mux.Vars(r)
}
return &App{
service: service,
}, nil
}
// Handles application shutdown (closes DB connection, etc)
// Make sure the process does not exit before this is called
func (app *App) Shutdown() {
// Noop
}
// Starts listening
func (app *App) Listen() error {
handler := app.CreateHandler()
return http.ListenAndServe(":80", handler)
}
// Creates and configures the router
func (app *App) CreateHandler() http.Handler {
router := mux.NewRouter()
router.HandleFunc("/authenticate", app.service.Authenticate).Methods("POST")
router.HandleFunc("/_status/healthz", app.HealthStatus).Methods("GET")
router.HandleFunc("/_status/readiness", app.ReadinessStatus).Methods("GET")
return router
}
// Checks if the app is up and running
func (app *App) HealthStatus(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
// Checks if the app is ready for accepting request (eg. database is available as well)
func (app *App) ReadinessStatus(w http.ResponseWriter, r *http.Request) {
// Todo add check for user model service
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}