-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflakeweb.go
102 lines (77 loc) · 2.4 KB
/
snowflakeweb.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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/teddyking/snowflake/api"
"github.com/teddyking/snowflake/middleware"
"github.com/teddyking/snowflake/web/handler"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func init() {
configureLogging()
}
func main() {
log.Printf("starting snowflakeweb")
conn, err := grpc.Dial(configureServerAddress(), configureDialOptions()...)
if err != nil {
log.Fatalf("could not connect to server: %s", err.Error())
}
flakerService := api.NewFlakerClient(conn)
handler := handler.New(configureStaticDirPath(), flakerService)
log.Fatal(http.ListenAndServe(configureListenAddress(), handler))
}
func configureLogging() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
if os.Getenv("DEBUG") == "true" {
log.SetLevel(log.DebugLevel)
}
}
func configureServerAddress() string {
serverHost := os.Getenv("SERVERHOST")
if serverHost == "" {
serverHost = "0.0.0.0"
}
serverPort := os.Getenv("SERVERPORT")
if serverPort == "" {
serverPort = "2929"
}
serverAddress := fmt.Sprintf("%s:%s", serverHost, serverPort)
log.WithFields(log.Fields{"serverAddress": serverAddress}).Debug("configured serverAddress")
return serverAddress
}
func configureDialOptions() []grpc.DialOption {
dialOpts := []grpc.DialOption{grpc.WithInsecure()}
tlsCrtPath := os.Getenv("TLSCRTPATH")
if tlsCrtPath != "" {
creds, err := credentials.NewClientTLSFromFile(tlsCrtPath, "")
if err != nil {
log.Fatalf("error reading TLS creds from '%s': %s", tlsCrtPath, err.Error())
}
dialOpts = []grpc.DialOption{grpc.WithTransportCredentials(creds)}
log.WithFields(log.Fields{"tlsCrtPath": tlsCrtPath}).Debug("configured tls")
}
dialOpts = append(dialOpts, grpc.WithUnaryInterceptor(middleware.WithClientLogging))
return dialOpts
}
func configureListenAddress() string {
listenPort := os.Getenv("PORT")
if listenPort == "" {
listenPort = "2930"
}
listenAddress := fmt.Sprintf("0.0.0.0:%s", listenPort)
log.WithFields(log.Fields{"listenAddress": listenAddress}).Debug("listenAddress configured")
return listenAddress
}
func configureStaticDirPath() string {
staticDirPath := os.Getenv("STATICDIR")
if staticDirPath == "" {
staticDirPath = filepath.Join("web", "static")
}
log.WithFields(log.Fields{"staticDirPath": staticDirPath}).Debug("staticDirPath configured")
return staticDirPath
}