-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
271 lines (239 loc) · 8.34 KB
/
main.go
File metadata and controls
271 lines (239 loc) · 8.34 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/syumai/workers"
"github.com/syumai/workers/cloudflare/r2"
"github.com/uji/ujiprog.com/ogimage"
)
var bucket *r2.Bucket
func init() {
var err error
bucket, err = r2.NewBucket("STATIC_BUCKET")
if err != nil {
panic(err)
}
}
func main() {
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Write([]byte(`User-agent: *
Allow: /
Allow: /articles/
Allow: /feed.xml
Allow: /avator.jpg
Sitemap: https://ujiprog.com/sitemap.xml`))
})
http.HandleFunc("/sitemap.xml", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://ujiprog.com/</loc>
<lastmod>2026-01-24</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://ujiprog.com/feed.xml</loc>
<lastmod>2026-01-24</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
</urlset>`))
})
http.HandleFunc("/articles.json", func(w http.ResponseWriter, req *http.Request) {
obj, err := bucket.Get("articles.json")
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=3600")
io.Copy(w, obj.Body)
})
http.HandleFunc("/style.css", func(w http.ResponseWriter, req *http.Request) {
obj, err := bucket.Get("style.css")
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "text/css; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=604800")
io.Copy(w, obj.Body)
})
http.HandleFunc("/article.css", func(w http.ResponseWriter, req *http.Request) {
obj, err := bucket.Get("article.css")
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "text/css; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=604800")
io.Copy(w, obj.Body)
})
http.HandleFunc("/main.js", func(w http.ResponseWriter, req *http.Request) {
obj, err := bucket.Get("main.js")
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=604800")
io.Copy(w, obj.Body)
})
http.HandleFunc("/article.js", func(w http.ResponseWriter, req *http.Request) {
obj, err := bucket.Get("article.js")
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=604800")
io.Copy(w, obj.Body)
})
http.HandleFunc("/feed.xml", feedHandler)
http.HandleFunc("/articles/", articlesHandler)
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
// Redirect unknown paths to root
if req.URL.Path != "/" {
http.Redirect(w, req, "/", http.StatusFound)
return
}
obj, err := bucket.Get("index.html")
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Security-Policy", "default-src 'self'; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' blob: data:; script-src 'self'; object-src 'none'; base-uri 'self'; frame-src https://platform.twitter.com https://syndication.twitter.com; frame-ancestors 'none';")
w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
w.Header().Set("Cache-Control", "public, max-age=300")
io.Copy(w, obj.Body)
})
workers.Serve(nil) // use http.DefaultServeMux
}
// OGMeta represents OG image metadata for an article
type OGMeta struct {
Title string `json:"title"`
}
// OGMetaData maps article slug to OG metadata
type OGMetaData map[string]OGMeta
func articlesHandler(w http.ResponseWriter, req *http.Request) {
// Extract the path after /articles/
path := strings.TrimPrefix(req.URL.Path, "/articles/")
if path == "" {
http.NotFound(w, req)
return
}
// Handle OG image requests dynamically
if strings.HasSuffix(path, ".png") {
handleOGImage(w, req, bucket, path)
return
}
// Default to HTML - add .html extension if not present
var r2Key string
if !strings.HasSuffix(path, ".html") {
r2Key = "articles/" + path + ".html"
} else {
r2Key = "articles/" + path
}
obj, err := bucket.Get(r2Key)
if err != nil || obj == nil {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Security-Policy", "default-src 'self'; style-src 'self' https://fonts.googleapis.com; font-src https://fonts.gstatic.com; img-src 'self' https: data: blob:; script-src 'self' https://platform.twitter.com; frame-src https://platform.twitter.com https://syndication.twitter.com;")
w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
io.Copy(w, obj.Body)
}
// handleOGImage generates OG images dynamically
func handleOGImage(w http.ResponseWriter, req *http.Request, bucket *r2.Bucket, path string) {
// Extract article slug from path (e.g., "my-article.png" -> "my-article")
slug := strings.TrimSuffix(path, ".png")
// Load OG metadata
ogMetaObj, err := bucket.Get("og-meta.json")
if err != nil || ogMetaObj == nil {
http.Error(w, "OG metadata not found", http.StatusInternalServerError)
return
}
ogMetaData, err := io.ReadAll(ogMetaObj.Body)
if err != nil {
http.Error(w, "Failed to read OG metadata", http.StatusInternalServerError)
return
}
var ogMeta OGMetaData
if err := json.Unmarshal(ogMetaData, &ogMeta); err != nil {
http.Error(w, "Failed to parse OG metadata", http.StatusInternalServerError)
return
}
// Find the title for this article
meta, ok := ogMeta[slug]
if !ok {
http.NotFound(w, req)
return
}
// Load template image
templateObj, err := bucket.Get("templates/blog-ogp-tmpl.png")
if err != nil || templateObj == nil {
http.Error(w, "Template not found", http.StatusInternalServerError)
return
}
templateData, err := io.ReadAll(templateObj.Body)
if err != nil {
http.Error(w, "Failed to read template", http.StatusInternalServerError)
return
}
// Load fonts
asciiFontObj, err := bucket.Get("fonts/DMSans-Bold.ttf")
if err != nil || asciiFontObj == nil {
http.Error(w, "ASCII font not found", http.StatusInternalServerError)
return
}
asciiFontData, err := io.ReadAll(asciiFontObj.Body)
if err != nil {
http.Error(w, "Failed to read ASCII font", http.StatusInternalServerError)
return
}
japaneseFontObj, err := bucket.Get("fonts/NotoSansJP-Bold.ttf")
if err != nil || japaneseFontObj == nil {
http.Error(w, "Japanese font not found", http.StatusInternalServerError)
return
}
japaneseFontData, err := io.ReadAll(japaneseFontObj.Body)
if err != nil {
http.Error(w, "Failed to read Japanese font", http.StatusInternalServerError)
return
}
// Create OG image generator
generator, err := ogimage.NewGenerator(templateData, asciiFontData, japaneseFontData, 56)
if err != nil {
http.Error(w, "Failed to create OG generator: "+err.Error(), http.StatusInternalServerError)
return
}
// Generate OG image
var buf bytes.Buffer
if err := generator.Generate(meta.Title, &buf); err != nil {
http.Error(w, "Failed to generate OG image: "+err.Error(), http.StatusInternalServerError)
return
}
// Set response headers
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
w.Write(buf.Bytes())
}