-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
118 lines (99 loc) · 2.84 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
package main
import (
"embed"
"encoding/json"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
var (
//go:embed build/* build/_app/immutable/chunks/*
embedded embed.FS
staticPath string = "build"
prefix string = "/admin"
indexPath string = "index.html"
listenAddr string = ":3000"
publicEnvPrefix string = "PUBLIC_"
)
func main() {
sub, err := fs.Sub(embedded, staticPath)
if err != nil {
log.Fatalf("Failed to get embedded frontend subdirectory: %s\n", err.Error())
}
publicEnv := make(map[string]string)
for _, env := range os.Environ() {
pair := strings.SplitN(env, "=", 2)
if len(pair) != 2 {
continue
}
key, value := pair[0], pair[1]
if strings.HasPrefix(key, publicEnvPrefix) {
publicEnv[key] = value
}
}
publicEnvString, err := json.Marshal(publicEnv)
if err != nil {
log.Fatalf("Failed to parse public environment config: %s\n", err.Error())
}
fileServer := http.FileServer(http.FS(sub))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path, err := filepath.Abs(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if path == "/healthz" {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
}
if path == "/_app/env.js" {
w.Header().Set("Content-Type", "application/javascript")
w.WriteHeader(http.StatusOK)
w.Write([]byte("export const env=" + string(publicEnvString)))
return
}
path = filepath.Join(staticPath, path)
_, err = embedded.Open(path)
if os.IsNotExist(err) {
index, err := embedded.ReadFile(filepath.Join(staticPath, indexPath))
if err != nil {
log.Printf("Failed to read file: %s\n", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(index)
return
}
if err != nil {
log.Printf("Failed to open file: %s\n", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
fileServer.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(listenAddr, loggingMiddleware(http.StripPrefix(prefix, handler))))
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
lrw := &loggingResponseWriter{ResponseWriter: w}
next.ServeHTTP(lrw, r)
log.Printf("%s \"%s %s %s\" %d %v \"%s\"", r.RemoteAddr, r.Method, r.URL.Path, r.Proto, lrw.statusCode, time.Since(startTime), r.UserAgent())
})
}
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
}
func (lrw *loggingResponseWriter) WriteHeader(statusCode int) {
lrw.statusCode = statusCode
lrw.ResponseWriter.WriteHeader(statusCode)
}