-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (60 loc) · 1.55 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
package main
import (
"fmt"
"net"
"net/http"
"os"
"github.com/gomicro/ledger"
"github.com/gomicro/steward"
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
)
const (
app = "doorman"
)
var (
version string
config configuration
log *ledger.Ledger
)
type configuration struct {
Host string `default:"0.0.0.0"`
Port string `default:"4567"`
LogLevel string `default:"debug"`
}
func main() {
configure()
err := startService()
if err != nil {
log.Errorf("Something went wrong: %v", err.Error())
return
}
log.Info("Server stopping")
}
func configure() {
err := envconfig.Process(app, &config)
if err != nil {
fmt.Printf("failed to process env vars: %v\n", err.Error())
os.Exit(1)
}
log = ledger.New(os.Stdout, ledger.ParseLevel(config.LogLevel))
log.Debug("Logger configured")
if version == "" {
version = "local-dev"
}
steward.SetStatusEndpoint("/v1/status")
log.Info("Configuration complete")
}
func startService() error {
log.Infof("Starting service (%v)", version)
log.Infof("Listening on %v:%v", config.Host, config.Port)
http.Handle("/", registerEndpoints())
return http.ListenAndServe(net.JoinHostPort(config.Host, config.Port), nil)
}
func registerEndpoints() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/o/oauth2/auth", handleGetGoogleAuth).Methods("GET").Queries("client_id", "{client_id}", "redirect_uri", "{redirect_uri}", "response_type", "{response_type}")
r.HandleFunc("/o/oauth2/auth", handlePostGoogleAuth).Methods("POST")
r.HandleFunc("/oauth2/v3/userinfo", handleUserInfo)
return r
}