-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhandlers.go
158 lines (136 loc) · 3.8 KB
/
handlers.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
package main
import (
"embed"
_ "embed"
"io"
"net/http"
"path/filepath"
"strings"
"time"
)
//go:embed src/homepage.html
var homepageHTML []byte
//go:embed dist/homepage.js
var homepageJS []byte
//go:embed dist/homepage.css
var homepageCSS []byte
//go:embed dist/images
var staticImages embed.FS
func handleHome(w http.ResponseWriter, r *http.Request) {
// Handle static image files
if strings.HasPrefix(r.URL.Path, "/dist/images/") {
if !isProduction() {
// In development, serve directly from disk
http.ServeFile(w, r, r.URL.Path[1:]) // Remove leading slash
return
}
// In production, serve from embedded file system
imagePath := strings.TrimPrefix(r.URL.Path, "/")
file, err := staticImages.Open(imagePath)
if err != nil {
http.NotFound(w, r)
return
}
defer file.Close()
// Set content type based on file extension
ext := filepath.Ext(r.URL.Path)
switch strings.ToLower(ext) {
case ".jpg", ".jpeg":
w.Header().Set("Content-Type", "image/jpeg")
case ".png":
w.Header().Set("Content-Type", "image/png")
case ".gif":
w.Header().Set("Content-Type", "image/gif")
case ".svg":
w.Header().Set("Content-Type", "image/svg+xml")
case ".webp":
w.Header().Set("Content-Type", "image/webp")
default:
w.Header().Set("Content-Type", "application/octet-stream")
}
// Copy the file content to the response
io.Copy(w, file)
return
}
// If requesting static files from /dist/, serve them
if strings.HasPrefix(r.URL.Path, "/dist/") {
if !isProduction() {
// In development, serve directly from disk
http.ServeFile(w, r, r.URL.Path[1:]) // Remove leading slash
return
}
// In production, serve from embedded files
switch r.URL.Path {
case "/dist/homepage.js":
w.Header().Set("Content-Type", "application/javascript")
w.Write(homepageJS)
case "/dist/homepage.css":
w.Header().Set("Content-Type", "text/css")
w.Write(homepageCSS)
default:
http.NotFound(w, r)
}
return
}
// Only serve homepage HTML for the root path
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
// Serve homepage HTML
w.Header().Set("Content-Type", "text/html")
if !isProduction() {
// In development, serve directly from disk
http.ServeFile(w, r, "src/homepage.html")
return
}
// In production, serve from embedded file
log.Printf("homepageHTML: %s", homepageHTML)
w.Write(homepageHTML)
}
// this handles requests to https://npub1whatever.something-else.whatever-2.BASE_DOMAIN/
func handleSubdomain(subdomain string, w http.ResponseWriter, r *http.Request) {
params, err := paramsFromSubdomain(subdomain)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
renderModifiedHTML(w, params)
}
// this handles requests from arbitrary external domains
func handleMagicCNAME(w http.ResponseWriter, r *http.Request) {
cname := lookupCNAME(r.Host)
if cname == "" {
http.Error(w, "missing CNAME record for "+r.Host, 400)
return
}
subdomain, found := strings.CutSuffix(cname, "."+s.BaseDomain+".")
if !found {
http.Error(w, "invalid CNAME '"+cname+"' doesn't end with '"+s.BaseDomain+".'", 404)
return
}
params, err := paramsFromSubdomain(subdomain)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
// cache headers for 2 hours
w.Header().Set("Cache-Control", "public, max-age=7200")
w.Header().Set("Expires", time.Now().Add(2*time.Hour).Format(http.TimeFormat))
renderModifiedHTML(w, params)
}
// this is called by caddy to know if it should get a certificate for a domain or not
func handleCaddyAsk(w http.ResponseWriter, r *http.Request) {
domain := r.URL.Query().Get("domain")
cname := lookupCNAME(domain)
if cname == "" {
w.WriteHeader(400)
return
}
if !strings.HasSuffix(cname, "."+s.BaseDomain+".") {
w.WriteHeader(400)
return
}
log.Info().Str("domain", domain).Msg("allowing external domain")
return
}