forked from keratin/authn-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (71 loc) · 1.62 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
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"github.com/keratin/authn-server/app"
"github.com/keratin/authn-server/app/data"
"github.com/keratin/authn-server/server"
"github.com/sirupsen/logrus"
"os"
"path"
)
// VERSION is a value injected at build time with ldflags
var VERSION string
func main() {
var cmd string
if len(os.Args) == 1 {
cmd = "server"
} else {
cmd = os.Args[1]
}
cfg, err := app.ReadEnv()
if err != nil {
fmt.Println(err)
fmt.Println("\nsee: https://github.com/keratin/authn-server/blob/master/docs/config.md")
return
}
if cmd == "server" {
serve(cfg)
} else if cmd == "migrate" {
migrate(cfg)
} else {
os.Stderr.WriteString(fmt.Sprintf("unexpected invocation\n"))
usage()
os.Exit(2)
}
}
func serve(cfg *app.Config) {
fmt.Println(fmt.Sprintf("~*~ Keratin AuthN v%s ~*~", VERSION))
// Default logger
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
logger.Level = logrus.DebugLevel
logger.Out = os.Stdout
app, err := app.NewApp(cfg, logger)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(fmt.Sprintf("AUTHN_URL: %s", cfg.AuthNURL))
fmt.Println(fmt.Sprintf("PORT: %d", cfg.ServerPort))
if app.Config.PublicPort != 0 {
fmt.Println(fmt.Sprintf("PUBLIC_PORT: %d", app.Config.PublicPort))
}
server.Server(app)
}
func migrate(cfg *app.Config) {
fmt.Println("Running migrations.")
err := data.MigrateDB(cfg.DatabaseURL)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Migrations complete.")
}
}
func usage() {
exe := path.Base(os.Args[0])
fmt.Println(fmt.Sprintf(`
Usage:
%s server - run the server (default)
%s migrate - run migrations
`, exe, exe))
}