-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (109 loc) · 2.6 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"os/signal"
// "strconv"
"syscall"
rice "github.com/GeertJohan/go.rice"
"github.com/rs/cors"
"github.com/sebest/xff"
"github.com/urfave/negroni"
)
// Declare globals here
var (
addr string
// diskCriticalBytes int
)
var (
canary = "blank"
)
var appCon *appContext
func init() {
// Assign globals here
addr = os.Getenv("ADDR")
if addr == "" {
addr = ":http"
}
// if i, err := strconv.Atoi(os.Getenv("DISK_CRITICAL_BYTES")); err != nil {
// diskCriticalBytes = 104900000 // 100mb
// } else {
// diskCriticalBytes = i
// }
// these have to be here for rice to work
_, _ = rice.FindBox("static")
cfg := rice.Config{LocateOrder: []rice.LocateMethod{
rice.LocateWorkingDirectory,
rice.LocateFS,
rice.LocateAppended,
}}
staticBox := cfg.MustFindBox("static")
var appOptions []appOption
appCon = newAppContext(
staticBox,
appOptions...,
)
}
func main() {
n := negroni.New()
xffh, err := xff.Default()
if err != nil {
panic(err)
}
n.Use(xffh)
n.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT"},
AllowedHeaders: []string{"accept", "accept-encoding", "authorization", "content-type"},
AllowCredentials: true,
}))
n.UseHandler(appCon)
fallbackHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
n.ServeHTTP(rw, r)
})
if os.Getenv("TLS") == "true" {
fallbackHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
n.ServeHTTP(rw, r)
})
}
s := http.Server{Addr: addr, Handler: fallbackHandler}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
go func() {
sig := <-ch
fmt.Printf("\nshutting down in response to signal: %s", sig.String())
if err := s.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
fmt.Printf("Starting server on port %s", addr)
if os.Getenv("TLS") == "true" {
s2 := &http.Server{
Handler: n,
Addr: ":https",
TLSConfig: &tls.Config{},
}
ch2 := make(chan os.Signal, 1)
signal.Notify(ch2, syscall.SIGTERM)
go func() {
sig := <-ch2
fmt.Printf("\nshutting down in response to signal: %s", sig.String())
if err := s2.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
fmt.Println("INFO Listening on 443 with TLS")
go func() {
if err := s2.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
panic(err)
}
}()
}
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(err)
}
fmt.Println("successfully shut down")
}