-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
376 lines (321 loc) · 12.6 KB
/
main.go
File metadata and controls
376 lines (321 loc) · 12.6 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
whisper implements a simple web server aimed at small websites.
# Conventions
In general, whisper serves static content from the location it's found - making it easy to structure your site how you want.
There is special handling for certain content like Markdown files.
See the example folder for a sample site layout. In general, whisper uses conventions instead of configuration files.
Conventions used by this server include:
* The template folder holds HTML templates, using Go's html/template package. These templates are used for rendering content but never served directly.
* A sitemap.txt can be created as a template. See the example for details.
* The default page for a folder is a Markdown file called index.md.
* An optional whisper.cfg file holds settings should you want to preserve them.
* Files 404.md and 500.md can be provided for custom errors.
# Markdown
Web pages are generally written in Markdown and use HTML templates to render into the site. The default template to use is called "default"; you must
have a "default" template and an "image" template. Templates are stored in the "template" folder.
NOTE: If no "template" folder is found, then default templates are loaded named "default" and "image". You probably don't want these because they are
extremely basic, but it's okay for just messing around and viewing Markdown locally.
Markdown may contain front matter which is in TOML format. The front matter is delimited by "+++"" at the start and end. For example:
+++
# This is my front matter
title = "My glorious page"
+++
# This is my Heading
This is my [Markdown](https://en.wikipedia.org/wiki/Markdown).
Front matter may include:
Name | Type | Description
-------------|------------------|------------------------------------------
title | string | Title of page
date | time | Publish date
tags | array of strings | Tags for the articles (not used yet)
template | string | Override the template to render this file
redirect | duration | Provide redirect info (not automated)
originalfile | string | Name of the base Markdown or image file
Front matter is used for sorting and constructing lists of articles.
# Templates
whisper uses standard Go templates from the "html/template" package. Templates are passed the following data:
// FrontMatter holds data scraped from a Markdown page.
type FrontMatter struct {
Title string `toml:"title"` // Title of this page
Date time.Time `toml:"date"` // Date the article appears
Template string `toml:"template"` // The name of the template to use
Tags []string `toml:"tags"` // Tags to assign to this article
Redirect string `toml:"redirect"` // Issue a redirect to another location
OriginalFile string `toml:"originalfile"` // The original file (markdown or image)
}
// PageInfo has information about the current page.
type PageInfo struct {
Path string // path from URL
Filename string // end portion (file) from URL
}
// data is what is passed to markdown templates.
type data struct {
FrontMatter FrontMatter // front matter from Markdown file or defaults
Page PageInfo // information aboout current page
Content template.HTML // rendered Markdown
}
Page is information about the current page, and FrontMatter is the front-matter from the current Markdown file.
Content contains the HTML version of the Markdown file.
Functions are added to the template for your convenience.
Function | Description
----------------------------------|------------
dir(path string) []File | Return the contents of the given folder, excluding special files and subfolders.
sortbyname([]File) []File | Sort by name (reverse)
sortbytime([]File) []File | Sort by time (reverse)
match(string, ...string) bool | Match string against file patterns
filter([]File, ...string) []File | Filter list against file patterns
join(parts ...string) string | The same as path.Join
ext(path string) string | The same as path.Ext
prev([]File, string) *File | Find the previous file based on Filename
next([]File, string) *File | Find the next file based on Filename
reverse([]File) []File | Reverse the list
trimsuffix(string, string) string | The same as strings.TrimSuffix
trimprefix(string, string) string | The same as strings.TrimPrefix
trimspace(string) string | The same as strings.TrimSpace
markdown(string) template.HTML | Render Markdown file into HTML
frontmatter(string) *FrontMatter | Read front matter from file
now() time.Time | Current time
File is defined as:
// File holds data about a page endpoint.
type File struct {
FrontMatter FrontMatter
Filename string
}
If File is not a Markdown file, then FrontMatter.Title is set to the file name and FrontMatter.Date is set to the modification
time. The array is sorted by reverse date (most recent items first).
Note that FrontMatter.OriginalFile is very useful because, for image templates, it will hold the name of the image file. You probably
want to use it in the template.
# Image Templates
Folders named "photos", "images", "pictures", "cartoons", "toons", "sketches", "artwork", "drawings", "videos", or "movies" use a special handler that can
serve media using an HTML template called "image" or "video".
# Non-Goals
It's not a goal to make templates reusable. I expect templates need editing for new sites.
It's not a goal to automate creation of the menu.
It's not a goal to be a fully-featured server. I run https://caddyserver.com/ in front of it.
# More Detail
For more information take a look at the virtual package, which implements a virtual filesystem that handles rendering markdown into HTML.
*/
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/NYTimes/gziphandler"
"github.com/ancientlore/flagenv"
"github.com/ancientlore/whisper/cachefs"
"github.com/ancientlore/whisper/virtual"
"github.com/ancientlore/whisper/web"
"github.com/golang/groupcache"
)
// main is where it all begins. 😀
func main() {
// Setup flags
var (
fPort = flag.Int("port", 8080, "Port to listen on.")
fReadTimeout = flag.Duration("readtimeout", 10*time.Second, "HTTP server read timeout.")
fReadHeaderTimeout = flag.Duration("readheadertimeout", 5*time.Second, "HTTP server read header timeout.")
fWriteTimeout = flag.Duration("writetimeout", 30*time.Second, "HTTP server write timeout.")
fIdleTimeout = flag.Duration("idletimeout", 60*time.Second, "HTTP server keep-alive timeout.")
fRoot = flag.String("root", ".", "Root of web site.")
fCacheSize = flag.Int("cachesize", 0, "Cache size in MB.")
fCacheDuration = flag.Duration("cacheduration", 0, "How long to cache content.")
fTemplateReload = flag.Duration("templatereload", 10*time.Minute, "How often to reload templates.")
fExpires = flag.Duration("expires", 0, "Default cache-control max-age header.")
fStaticExpires = flag.Duration("staticexpires", 0, "Default cache-control max-age header for static content.")
fWaitForFiles = flag.Bool("wait", false, "Wait for files to appear in root folder before starting up.")
fLogger = flag.String("logger", "", "Select JSON or text logger.")
)
flag.Parse()
flagenv.Parse("")
// Pick logger
switch strings.ToLower(*fLogger) {
case "":
case "text":
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil)))
case "json":
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
default:
slog.Error("Invalid logger type: select \"text\" or \"json\"")
os.Exit(1)
}
// If requested, wait for files to show up in root folder, up to 60 seconds
if *fWaitForFiles {
err := waitForFiles(*fRoot)
if err != nil {
slog.Error("Unable to wait for files", "error", err)
os.Exit(2)
}
}
// Setup groupcache (in this example with no peers)
groupcache.RegisterPeerPicker(func() groupcache.PeerPicker { return groupcache.NoPeers{} })
// Open root
root, err := os.OpenRoot(*fRoot)
if err != nil {
slog.Error("Unable to open root folder", "error", err)
os.Exit(3)
}
defer root.Close()
// Create the virtual file system
// virtualFileSystem, err := virtual.New(os.DirFS(*fRoot))
virtualFileSystem, err := virtual.New(root.FS())
if err != nil {
slog.Error("Unable to create virtual file system", "error", err)
os.Exit(4)
}
defer virtualFileSystem.Close()
virtualFileSystem.ReloadTemplates(*fTemplateReload)
// get the config
cfg, err := virtualFileSystem.Config()
if err != nil {
slog.Error("Cannot load config", "error", err)
os.Exit(5)
}
// Apply config overrides
if *fExpires != 0 {
cfg.Expires = virtual.Duration(*fExpires)
}
if *fStaticExpires != 0 {
cfg.StaticExpires = virtual.Duration(*fStaticExpires)
}
if *fCacheSize != 0 {
cfg.CacheSize = *fCacheSize
}
if *fCacheDuration != 0 {
cfg.CacheDuration = virtual.Duration(*fCacheDuration)
}
if cfg.CacheSize <= 0 {
cfg.CacheSize = 1 // need a default
}
slog.Info("Expirations", "normal", cfg.Expires, "static", cfg.StaticExpires)
slog.Info("Cache", "size", fmt.Sprintf("%dMB", cfg.CacheSize), "duration", cfg.CacheDuration.String())
// Create the cached file system
cachedFileSystem := cachefs.New(virtualFileSystem, &cachefs.Config{GroupName: "whisper", SizeInBytes: int64(cfg.CacheSize) * 1024 * 1024, Duration: time.Duration(cfg.CacheDuration)})
// create handler
handler := web.HeaderHandler(
web.ExpiresHandler(
gziphandler.GzipHandler(
web.ErrorHandler(
http.FileServer(
http.FS(cachedFileSystem),
),
cachedFileSystem,
),
),
time.Duration(cfg.Expires),
time.Duration(cfg.StaticExpires),
),
cfg.Headers)
// Create HTTP server
var srv = http.Server{
Addr: fmt.Sprintf(":%d", *fPort),
ReadTimeout: *fReadTimeout,
WriteTimeout: *fWriteTimeout,
ReadHeaderTimeout: *fReadHeaderTimeout,
IdleTimeout: *fIdleTimeout,
Handler: handler,
}
// Start cache monitor
monc := stats("whisper")
// Create signal handler for graceful shutdown
go func() {
sigint := make(chan os.Signal, 1)
// interrupt signal sent from terminal
signal.Notify(sigint, os.Interrupt)
// sigterm signal sent from kubernetes
signal.Notify(sigint, syscall.SIGTERM)
<-sigint
// We received an interrupt signal, shut down.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
// Error from closing listeners, or context timeout:
slog.Error("HTTP server Shutdown", "error", err)
}
close(monc)
}()
// Listen for requests
slog.Info("Listening for requests")
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
slog.Error("HTTP server", "error", err)
} else {
slog.Info("Goodbye.")
}
}
type logStats groupcache.CacheStats
func (s logStats) LogValue() slog.Value {
return slog.GroupValue(
slog.Int64("items", s.Items),
slog.Int64("bytes", s.Bytes),
slog.Int64("gets", s.Gets),
slog.Int64("hits", s.Hits),
slog.Int64("evictions", s.Evictions),
)
}
func stats(groupName string) chan<- bool {
c := make(chan bool)
g := groupcache.GetGroup(groupName)
if g != nil {
go func() {
t := time.NewTicker(5 * time.Minute)
for {
select {
case _, ok := <-c:
if !ok {
return
}
case <-t.C:
sh := g.CacheStats(groupcache.HotCache)
sm := g.CacheStats(groupcache.MainCache)
slog.Info("Cache Stats", "hot", logStats(sh), "main", logStats(sm))
}
}
}()
}
return c
}
func waitForFiles(pathname string) error {
foundFiles := false
for i := 0; i < 60; i++ {
d, err := os.ReadDir(pathname)
if err != nil {
slog.Warn("os.Dir", "error", err)
} else if len(d) > 0 {
var dir []string
var hasError bool
for _, entry := range d {
if entry.IsDir() {
dir = append(dir, entry.Name()+"/")
} else {
dir = append(dir, entry.Name())
if entry.Name() == "cpln-error.txt" {
hasError = true
errData, err := os.ReadFile(path.Join(pathname, "cpln-error.txt"))
slog.Warn("cpln-error.txt", "cpln-error", errData, "error", err)
}
}
}
slog.Info("Found", "files", dir)
if !hasError {
foundFiles = true
break
}
}
if i%10 == 0 {
slog.Info("Waiting for files...")
}
time.Sleep(time.Second)
}
if !foundFiles {
return fmt.Errorf("no files found in %q", pathname)
}
return nil
}