-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
116 lines (94 loc) · 2.72 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"crypto/tls"
"fmt"
"github.com/chris-sg/bst_api/common"
"github.com/chris-sg/bst_api/db"
"github.com/chris-sg/bst_api/ddr"
"github.com/chris-sg/bst_api/drs"
"github.com/chris-sg/bst_api/jobs"
"github.com/chris-sg/bst_api/utilities"
bst_models "github.com/chris-sg/bst_server_models"
"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"golang.org/x/crypto/acme/autocert"
"log"
"net/http"
"os"
"strings"
"time"
)
func main() {
utilities.LoadConfig()
utilities.PrepareMiddleware()
os.Setenv("GODEBUG", "http2debug=2")
if utilities.DbMigration {
db.GetMigrator().Create()
return
}
r := CreateApiRouter()
var certManager *autocert.Manager
certManager = &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(utilities.ServeHost),
Cache: autocert.DirCache("./cert_cache_api"),
}
srv := &http.Server{
Handler: r,
Addr: ":" + utilities.ServePort,
ReadHeaderTimeout: 60 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 90 * time.Second,
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
ServerName: utilities.ServeHost,
},
}
go func() {
// serve HTTP, which will redirect automatically to HTTPS
h := certManager.HTTPHandler(nil)
log.Fatal(http.ListenAndServe(":http", h))
}()
fmt.Println("api started")
jobs.StartJobs()
log.Fatal(srv.ListenAndServeTLS("", ""))
}
func CreateApiRouter() (r *mux.Router) {
r = mux.NewRouter()
apiRouter := mux.NewRouter()
apiRouter.Path("/runmigration").Handler(utilities.GetProtectionMiddleware().With(
negroni.Wrap(http.HandlerFunc(RunDbMigration)))).Methods(http.MethodPatch)
apiRouter.PathPrefix("/user").Handler(negroni.New(
negroni.Wrap(common.CreateUserRouter())))
apiRouter.PathPrefix("/ddr").Handler(negroni.New(
negroni.Wrap(ddr.CreateDdrRouter())))
apiRouter.PathPrefix("/drs").Handler(negroni.New(
negroni.Wrap(drs.CreateDrsRouter())))
common.AttachGeneralRoutes(r)
r.PathPrefix(utilities.ApiBase).Handler(utilities.GetCommonMiddleware().With(
negroni.Wrap(apiRouter),
))
return
}
func RunDbMigration(rw http.ResponseWriter, r *http.Request) {
requiredScopes := []string{"update:database"}
tokenMap := utilities.ProfileFromToken(r)
val, ok := tokenMap["sub"].(string)
if !ok {
utilities.RespondWithError(rw, bst_models.ErrorJwtProfile)
return
}
val = strings.ToLower(val)
if !utilities.UserHasScopes(val, requiredScopes) {
glog.Warningf(
"user %s tried to migrate db, but did not have required scopes %s",
val,
strings.Join(requiredScopes, ","))
utilities.RespondWithError(rw, bst_models.ErrorScope)
return
}
db.GetMigrator().Create()
utilities.RespondWithError(rw, bst_models.ErrorOK)
return
}