-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
232 lines (209 loc) · 5.92 KB
/
server.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
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
228
229
230
231
232
// icbm is the Internet Connected Beverage Monitor (server side).
package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/NYTimes/gziphandler"
corsMid "github.com/rs/cors"
)
// AssetFS holds the contents of the static/** and template/** folders.
//go:embed static template
var AssetFS embed.FS
// cors takes a Handler and a list of allowed origin domains and returns a new
// Handler that enforces CORS to those sources.
func cors(next http.Handler, origins ...string) http.Handler {
c := corsMid.New(corsMid.Options{
AllowedOrigins: origins,
AllowCredentials: true,
MaxAge: 300,
})
return c.Handler(next)
}
func assetSrv(path string) http.Handler {
fsys, err := fs.Sub(AssetFS, path)
if err != nil {
return http.NotFoundHandler()
}
return maybeCompress(http.FileServer(http.FS(fsys)))
}
func fileSrv(path string) http.Handler {
return maybeCompress(http.FileServer(http.Dir(path)))
}
func maybeCompress(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch strings.ToLower(filepath.Ext(r.URL.Path)) {
case ".html", ".css", ".tsv", ".txt", ".js", ".md":
h = noCache(gziphandler.GzipHandler(h))
}
h.ServeHTTP(w, r)
})
}
var (
noCacheHeaders = map[string]string{
"Expires": time.Unix(0, 0).Format(time.RFC1123),
"Cache-Control": "no-cache, no-store, no-transform, must-revalidate, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}
etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
)
// noCache configures the return headers to request not caching the results.
// per https://github.com/go-chi/chi/blob/master/middleware/nocache.go
func noCache(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Delete any ETag headers.
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
// Set our headers.
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
}
h.ServeHTTP(w, r)
})
}
var willServeFor = []string{
"http://lunarville.org",
"http://www.lunarville.org",
"https://lunarville.org",
"https://www.lunarville.org",
"https://api.evq.io",
"https://icbm.api.evq.io",
"http://localhost:*",
"https://icbm.fly.dev",
}
// Routes returns the mappings for handling web requests.
func Routes() *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/", assetSrv("static"))
// mux.HandleFunc("/", BeverageStatus("Lunarville"))
mux.Handle("/b/", http.StripPrefix("/b/", http.HandlerFunc(tapStatus)))
mux.HandleFunc("/bev", BeverageStatus("Lunarville"))
mux.HandleFunc("/bevbeta", BeverageStatus("Lunarville-beta"))
mux.HandleFunc("/icbm/v1", icbmUpdate)
mux.Handle("/data/", http.StripPrefix("/data/", cors(fileSrv("/data"), willServeFor...)))
mux.Handle("/static/", http.StripPrefix("/static/", assetSrv("static")))
mux.HandleFunc("/version", icbmVersion)
return mux
}
func serve(httpaddr string) *http.Server {
logger := log.New(FilteredHTTPLogger(os.Stderr), "", log.LstdFlags)
srv := &http.Server{
Addr: httpaddr,
ErrorLog: logger,
Handler: Routes(),
}
startHTTP := func(srv *http.Server) {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
logger.Print(err)
}
}
go startHTTP(srv)
return srv
}
func processSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGUSR1)
for {
sig := <-c
if sig == syscall.SIGINT {
return
}
}
}
func shutdown(srv *http.Server) {
// Attempt a graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
go srv.Shutdown(ctx)
}
// superfly returns whether we're running on a fly.io instance.
func superfly() bool {
return os.Getenv("FLY_ALLOC_ID") != ""
}
// platform returns the platform name and details (for fly.io) or the
// hostname of the hosting virtual machine.
func platform() string {
if superfly() {
var x string
peers, _ := net.LookupHost("icbm.internal")
regions, _ := net.LookupTXT("regions.icbm.internal")
siblings, _ := net.LookupTXT("_apps.internal")
x += fmt.Sprintf("host: %s.fly.dev\n", os.Getenv("FLY_APP_NAME"))
x += fmt.Sprintf("listen: %s\n", *httpaddr)
x += fmt.Sprintf("id: %s\n", os.Getenv("FLY_ALLOC_ID"))
x += fmt.Sprintf("region: %s\n", os.Getenv("FLY_REGION"))
x += fmt.Sprintf("peers: %s\n", peers)
x += fmt.Sprintf("regions: %s\n", regions)
x += fmt.Sprintf("siblings: %s\n", siblings)
return x
}
hostname, _ := os.Hostname()
return fmt.Sprintf("%s, %s\n", hostname, *httpaddr)
}
// getLogin looks for a validated API key and returns the credentials
// or nil on failure.
func getLogin(w http.ResponseWriter, r *http.Request) *User {
apikey := r.Header.Get("x-icbm-api-key")
creds, found := users[apikey]
if !found || !creds.Valid {
// metrics.BadLogins++
return nil
}
// metrics.APILogins++
return &creds
}
// User is a simple on-off scheme per API client.
type User struct {
Username string
Valid bool
}
var users = map[string]User{} // The key is the API key.
// Load the .env file if it exists and set the valid environment variables.
func loadDotEnv() {
if f, err := os.ReadFile(".env"); err == nil {
for _, line := range strings.Split(string(f), "\n") {
if k, v, found := strings.Cut(line, "="); found {
v = strings.TrimFunc(v, func(r rune) bool {
return r == '"' || r == '\''
})
os.Setenv(k, v)
}
}
}
}
func loadUserList() error {
loadDotEnv()
userdb := os.Getenv("ICBMUserDb")
j := strings.NewReader(userdb)
dec := json.NewDecoder(j)
return dec.Decode(&users)
}
func init() {
if err := loadUserList(); err != nil {
log.Println("Could not load user database, server will be read-only:", err)
} else {
log.Printf("User database loaded, %d entries\n", len(users))
}
}