This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
/
main.go
161 lines (140 loc) · 4.1 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"aletheiaware.com/netgo"
"aletheiaware.com/netgo/handler"
"crypto/tls"
"embed"
"errors"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"path"
"strings"
"time"
)
//go:embed assets
var embeddedFS embed.FS
func main() {
// Configure Logging
logFile, err := netgo.SetupLogging()
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
log.Println("Log File:", logFile.Name())
// Create Multiplexer
mux := http.NewServeMux()
handler.AttachHealthHandler(mux)
// Handle Static Assets
staticFS, err := fs.Sub(embeddedFS, path.Join("assets", "static"))
if err != nil {
log.Fatal(err)
}
//handler.AttachStaticFSHandler(mux, staticFS, false, fmt.Sprintf("public, max-age=%d", 60*60*24*7*52)) // 52 week max-age
handler.AttachStaticFSHandler(mux, staticFS, false, "no-cache")
// Parse Templates
templateFS, err := fs.Sub(embeddedFS, path.Join("assets", "template"))
if err != nil {
log.Fatal(err)
}
templates, err := template.ParseFS(templateFS, "*.go.html")
if err != nil {
log.Fatal(err)
}
AttachAssetHandlers(mux)
puzzles, ok := os.LookupEnv("PUZZLE_DIRECTORY")
if !ok {
puzzles = "puzzles"
}
if err := os.MkdirAll(puzzles, os.ModePerm); err != nil {
log.Fatal(err)
}
log.Println("Puzzles Directory:", puzzles)
mux.Handle("/daily.json", handler.Log(handler.Compress(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(puzzles, time.Now().UTC().Format("2006-01-02")+".json"))
}))))
mux.Handle("/daily", handler.Log(handler.Compress(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := struct {
Live bool
}{
Live: netgo.IsLive(),
}
if err := templates.ExecuteTemplate(w, "daily.go.html", data); err != nil {
log.Println(err)
return
}
}))))
// Handle favicon.ico
mux.Handle("/favicon.ico", handler.Log(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/daily.gif", http.StatusFound)
})))
// Handle robots.txt
mux.Handle("/robots.txt", handler.Log(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/robots.txt", http.StatusFound)
})))
// Handle sitemap.txt
mux.Handle("/sitemap.txt", handler.Log(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/sitemap.txt", http.StatusFound)
})))
mux.Handle("/", handler.Compress(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if p := strings.TrimSuffix(r.URL.Path, "index.html"); p != "/" {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL, r.Header, "not found")
http.NotFound(w, r)
return
}
netgo.LogRequest(r)
data := struct {
Live bool
Date string
}{
Live: netgo.IsLive(),
Date: time.Now().UTC().Format("2006-01-02"),
}
if err := templates.ExecuteTemplate(w, "index.go.html", data); err != nil {
log.Println(err)
return
}
})))
if netgo.IsSecure() {
host, ok := os.LookupEnv("HOST")
if !ok {
log.Fatal(errors.New("Missing HOST environment variable"))
}
routeMap := make(map[string]bool)
routes, ok := os.LookupEnv("ROUTES")
if ok {
for _, route := range strings.Split(routes, ",") {
routeMap[route] = true
}
}
// Redirect HTTP Requests to HTTPS
go http.ListenAndServe(":80", http.HandlerFunc(netgo.HTTPSRedirect(host, routeMap)))
certificates, ok := os.LookupEnv("CERTIFICATE_DIRECTORY")
if !ok {
certificates = "certificates"
}
log.Println("Certificate Directory:", certificates)
// Serve HTTPS Requests
config := &tls.Config{MinVersion: tls.VersionTLS12}
server := &http.Server{
Addr: ":443",
Handler: mux,
TLSConfig: config,
ReadTimeout: time.Hour,
ReadHeaderTimeout: time.Hour,
WriteTimeout: time.Hour,
IdleTimeout: time.Hour,
}
if err := server.ListenAndServeTLS(path.Join(certificates, "fullchain.pem"), path.Join(certificates, "privkey.pem")); err != nil {
log.Fatal(err)
}
} else {
// Serve HTTP Requests
log.Println("HTTP Server Listening on :80")
if err := http.ListenAndServe(":80", mux); err != nil {
log.Fatal(err)
}
}
}