-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
227 lines (188 loc) · 5.58 KB
/
Copy pathmain.go
File metadata and controls
227 lines (188 loc) · 5.58 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"crypto/tls"
"embed"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
)
const (
flagUseLocal = "local"
flagPort = "port"
flagHostURL = "host"
flagIsDev = "isDev"
envUseLocal = "USE_LOCAL"
envPort = "HOST_PORT"
envHostURL = "HOST_URL"
envIsDEV = "IS_DEV"
webPath = "build/web"
storagePath = "shared"
defaultPort = 80
defaultLocal = false
defaultIsDev = false
defaultHost = ""
)
//go:embed build/web
var embeddedWeb embed.FS
type config struct {
port int
host []string
useLocal bool
isDev bool
}
func main() {
config := getConfig()
if _, err := os.Stat(storagePath); errors.Is(err, os.ErrNotExist) {
log.Printf("Storage path [%s] not found", storagePath)
} else {
log.Printf("Storage path [%s] found", storagePath)
}
handler := logWrapper(http.FileServer(getFileSystem(config.useLocal, webPath)))
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(config.host...),
Cache: autocert.DirCache(storagePath),
}
if !config.isDev {
if err := runProdServer(m, handler); err != nil {
log.Fatalf("Shutting down, unable to start secure server: %v", err)
}
}
var httpSrv *http.Server
if !config.isDev {
httpSrv = makeHTTPToHTTPSRedirectServer()
} else {
httpSrv = makeHTTPServer(handler)
}
if m != nil {
httpSrv.Handler = m.HTTPHandler(httpSrv.Handler)
}
httpSrv.Addr = fmt.Sprintf(":%d", config.port)
log.Printf("Starting HTTP server on %s\n", httpSrv.Addr)
err := httpSrv.ListenAndServe()
log.Printf("Server shutdown, reason: %v", err)
}
func runProdServer(m *autocert.Manager, h http.Handler) error {
httpsSrv := makeHTTPServer(h)
httpsSrv.Addr = ":443"
httpsSrv.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
go func() {
log.Printf("Starting HTTPS server on %s", httpsSrv.Addr)
err := httpsSrv.ListenAndServeTLS("", "")
if err != nil {
log.Fatalf("httpsSrv.ListendAndServeTLS() failed with %s", err)
}
}()
return nil
}
func getConfig() config {
c := config{
port: defaultPort,
useLocal: defaultLocal,
isDev: defaultIsDev,
}
// check for environment configs, ignore errors at this point
if port, exists := os.LookupEnv(envPort); exists {
c.port, _ = strconv.Atoi(port)
}
if host, exists := os.LookupEnv(envHostURL); exists {
if host != "" {
c.host = append(c.host, strings.Split(host, ",")...)
}
}
if _, exists := os.LookupEnv(envUseLocal); exists {
c.useLocal = true
}
if _, exists := os.LookupEnv(envIsDEV); exists {
c.isDev = true
}
flag.IntVar(&c.port, flagPort, c.port, "port to host the site")
flag.BoolVar(&c.useLocal, flagUseLocal, c.useLocal, "true to use the local file system instead of the embedded files")
var host string
flag.StringVar(&host, flagHostURL, "", "comma seperated host addresses to use when acquiring a certificate")
flag.BoolVar(&c.isDev, flagIsDev, c.isDev, "run in production mode, this will require a host url")
flag.Parse()
if host != "" {
c.host = append(c.host, strings.Split(host, ",")...)
}
log.Printf("-%s or %s: %d", flagPort, envPort, c.port)
log.Printf("-%s or %s: %t", flagUseLocal, envUseLocal, c.useLocal)
log.Printf("-%s or %s: %v", flagHostURL, envHostURL, c.host)
log.Printf("-%s or %s: %t", flagIsDev, envIsDEV, c.isDev)
if !c.isDev && len(c.host) <= 0 {
log.Fatal("Cannot run in production mode without a host url")
}
return c
}
// logWrapper wraps the http handler from the FileServer to log information
// about the request.
func logWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s requested %s", getIP(r), r.URL.Path)
h.ServeHTTP(w, r)
})
}
// getIP returns the IP address of the http.Request. If the request has a
// X-FORWARDED-FOR header that value is used over the RemoteAddr.
func getIP(r *http.Request) string {
// check if the IP was forwarded
if f := r.Header.Get("X-FORWARDED-FOR"); f != "" {
return f
}
return r.RemoteAddr
}
// getFileSystem returns the a FileSystem to use, either the embedded
// filesystem or the local FileSystem.
func getFileSystem(useLocal bool, path string) http.FileSystem {
if useLocal {
log.Println("using local file system")
return http.FS(os.DirFS(path))
}
log.Println("using embedded file system")
fsys, err := fs.Sub(embeddedWeb, path)
if err != nil {
panic(err)
}
return http.FS(fsys)
}
// makeServerFromMux creates a http server with the specified mux.
func makeServerFromMux(mux *http.ServeMux) *http.Server {
// Set timeouts so that a slow or malicious client doesn't
// hold resources forever.
return &http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
Handler: mux,
}
}
// makeHTTPServer creates a http server setting the passed in handler.
// as root '/'
func makeHTTPServer(h http.Handler) *http.Server {
mux := &http.ServeMux{}
mux.HandleFunc("/healthz", ServeHealth)
mux.HandleFunc("/", h.ServeHTTP)
return makeServerFromMux(mux)
}
// makeHTTPToHTTPSRedirectServer creates a port 80 to 443 redirect handler.
func makeHTTPToHTTPSRedirectServer() *http.Server {
handleRedirect := func(w http.ResponseWriter, r *http.Request) {
newURI := "https://" + r.Host + r.URL.String()
log.Printf("Redirecting %s%s to %s", r.Host, r.URL.String(), newURI)
http.Redirect(w, r, newURI, http.StatusFound)
}
mux := &http.ServeMux{}
mux.HandleFunc("/", handleRedirect)
return makeServerFromMux(mux)
}
func ServeHealth(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Healthy"))
}