-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (96 loc) · 3.26 KB
/
Copy pathmain.go
File metadata and controls
114 lines (96 loc) · 3.26 KB
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
// Package main starts a Sibyl server
package main
import (
"fmt"
"math"
"net/http"
"os"
"strings"
rice "github.com/GeertJohan/go.rice"
"github.com/gorilla/handlers"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/synacor/sibyl/server"
)
const defaultPort = 5000
var maxPort = int(math.Pow(2, 16) - 1)
var s *server.Server
func main() {
// reminder, that viper will only look at the first config file it sees
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/sibyl")
viper.SetEnvPrefix("sib")
viper.BindEnv("port")
viper.BindEnv("tls_port")
viper.BindEnv("log_level")
viper.SetDefault("log_level", "info")
viper.SetDefault("port", defaultPort)
if err := viper.ReadInConfig(); err != nil {
// viper requires a config file to be present for some reason. this will check for that error
// and silently ignore it
if _, isConfigFileNotFoundError := err.(viper.ConfigFileNotFoundError); !isConfigFileNotFoundError {
panic(err)
}
}
configureLogger()
tbox := rice.MustFindBox("templates")
sbox := rice.MustFindBox("static")
s = server.New(tbox, sbox)
mux := s.ServeMux()
done := make(chan bool, 1)
go serve(mux)
go s.ListenForEvents(done)
<-done
}
func configureLogger() {
levelStr := viper.GetString("log_level")
level, err := log.ParseLevel(levelStr)
if err != nil {
log.Fatalf("level %s does not exist", level)
}
log.SetLevel(level)
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
}
func serve(mux *http.ServeMux) {
port := viper.GetInt("port")
tlsPort := viper.GetInt("tls_port")
forceTLS := viper.GetBool("force_tls")
tlsPrivateKeyFile := viper.GetString("tls_private_key")
tlsPublicKeyFile := viper.GetString("tls_public_key")
if port <= 0 || port > maxPort {
log.Fatalf("PORT must be 0 < PORT <= %d", maxPort)
} else if tlsPort > 0 && port == tlsPort {
log.Fatalf("PORT cannot equal TLS_PORT")
} else if tlsPort > maxPort {
log.Fatalf("TLS_PORT must be 0 < TLS_PORT <= %d", maxPort)
} else if tlsPort > 0 && (tlsPublicKeyFile == "" || tlsPrivateKeyFile == "") {
log.Fatal("must supply TLS_PRIVATE_KEY and TLS_PUBLIC_KEY if TLS_PORT specified")
}
if tlsPort > 0 {
go func() {
pstr := fmt.Sprintf(":%d", tlsPort)
log.WithFields(log.Fields{"pid": os.Getpid()}).Printf("Listening on %s", pstr)
log.Fatal(http.ListenAndServeTLS(pstr, tlsPublicKeyFile, tlsPrivateKeyFile, handlers.CombinedLoggingHandler(os.Stdout, mux)))
}()
}
pstr := fmt.Sprintf(":%d", port)
log.WithFields(log.Fields{"pid": os.Getpid()}).Printf("Listening on %s", pstr)
log.Fatal(http.ListenAndServe(pstr, handlers.CombinedLoggingHandler(os.Stdout, maybeRedirectToTLS(tlsPort, forceTLS, mux))))
}
// maybeRedirectToTLS is middleware for optionally redirecting the user to the TLS version based on arguments passed to the application.
func maybeRedirectToTLS(tlsPort int, forceTLS bool, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if forceTLS && tlsPort > 0 {
hostname := strings.Split(r.Host, ":")[0]
if tlsPort != 443 {
hostname += fmt.Sprintf(":%d", tlsPort)
}
url := "https://" + hostname + r.URL.String()
http.Redirect(w, r, url, http.StatusMovedPermanently)
return
}
h.ServeHTTP(w, r)
})
}