-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
72 lines (65 loc) · 1.81 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/jmoiron/sqlx"
"github.com/uc-cdis/arborist/arborist"
"github.com/uc-cdis/go-authutils/authutils"
)
func main() {
var jwkEndpointEnv string = os.Getenv("JWKS_ENDPOINT")
// Parse flags:
// - port (to serve on)
// - jwks (endpoint to get keys for JWT validation)
var port *uint = flag.Uint("port", 80, "port on which to expose the API")
var jwkEndpoint *string = flag.String(
"jwks",
jwkEndpointEnv,
"endpoint from which the application can fetch a JWKS",
)
var dbUrl *string = flag.String(
"db",
"",
"URL to connect to database: postgresql://user:password@netloc:port/dbname\n"+
"can also be specified through the postgres\n"+
"environment variables. If using the commandline argument, add\n"+
"?sslmode=disable",
)
flag.Parse()
if *jwkEndpoint == "" {
print("WARNING: no $JWKS_ENDPOINT or --jwks specified; endpoints requiring JWT validation will error\n")
}
// if database URL is not provided it can use environment variables
db, err := sqlx.Open("postgres", *dbUrl)
if err != nil {
panic(err)
}
defer db.Close()
logFlags := log.Ldate | log.Ltime
logger := log.New(os.Stdout, "", logFlags)
jwtApp := authutils.NewJWTApplication(*jwkEndpoint)
arboristServer, err := arborist.NewServer().
WithLogger(logger).
WithJWTApp(jwtApp).
WithDB(db).
Init()
if err != nil {
panic(err)
}
addr := fmt.Sprintf(":%d", *port)
router := arboristServer.MakeRouter(os.Stdout)
httpLogger := log.New(os.Stdout, "", log.LstdFlags)
httpServer := &http.Server{
Addr: addr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
ErrorLog: httpLogger,
Handler: router,
}
httpLogger.Println("arborist serving at", httpServer.Addr)
httpLogger.Fatal(httpServer.ListenAndServe())
}