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
|
package server
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"time"
"go.alanpearce.eu/website/internal/builder"
cfg "go.alanpearce.eu/website/internal/config"
"go.alanpearce.eu/website/internal/vcs"
"go.alanpearce.eu/website/internal/website"
"go.alanpearce.eu/x/log"
"github.com/ardanlabs/conf/v3"
"github.com/osdevisnot/sorvor/pkg/livereload"
"gitlab.com/tozd/go/errors"
)
var (
CommitSHA = "local"
ShortSHA = "local"
serverHeader = fmt.Sprintf("website (%s)", ShortSHA)
)
type Config struct {
Root string `conf:"default:public"`
Redirect bool `conf:"default:true"`
ListenAddress string `conf:"default:localhost"`
Port int `conf:"default:8080,short:p"`
TLSPort int `conf:"default:8443"`
TLS bool `conf:"default:false"`
Development bool `conf:"default:false,flag:dev"`
ACMECA string `conf:"env:ACME_CA"`
ACMECACert string `conf:"env:ACME_CA_CERT"`
Domains string
}
type Server struct {
*http.Server
runtimeConfig *Config
config *cfg.Config
log *log.Logger
}
func applyDevModeOverrides(config *cfg.Config, runtimeConfig *Config) {
config.CSP.ScriptSrc = slices.Insert(config.CSP.ScriptSrc, 0, "'unsafe-inline'")
config.CSP.ConnectSrc = slices.Insert(config.CSP.ConnectSrc, 0, "'self'")
if runtimeConfig.Domains != "" {
config.Domains = strings.Split(runtimeConfig.Domains, ",")
} else {
config.Domains = []string{runtimeConfig.ListenAddress}
}
scheme := "http"
port := runtimeConfig.Port
if runtimeConfig.TLS {
scheme = "https"
port = runtimeConfig.TLSPort
}
config.BaseURL = cfg.URL{
URL: &url.URL{
Scheme: scheme,
Host: net.JoinHostPort(config.Domains[0], strconv.Itoa(port)),
},
}
}
func updateCSPHashes(config *cfg.Config, r *builder.Result) {
for i, h := range r.Hashes {
config.CSP.StyleSrc[i] = fmt.Sprintf("'%s'", h)
}
}
func serverHeaderHandler(wrappedHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", serverHeader)
wrappedHandler.ServeHTTP(w, r)
})
}
func rebuild(builderConfig *builder.IOConfig, config *cfg.Config, log *log.Logger) error {
r, err := builder.BuildSite(builderConfig, config, log.Named("builder"))
if err != nil {
return errors.WithMessage(err, "could not build site")
}
updateCSPHashes(config, r)
return nil
}
func New(runtimeConfig *Config, log *log.Logger) (*Server, error) {
builderConfig := &builder.IOConfig{
Destination: runtimeConfig.Root,
Development: runtimeConfig.Development,
}
if !runtimeConfig.Development {
vcsConfig := &vcs.Config{}
_, err := conf.Parse("VCS", vcsConfig)
if err != nil {
return nil, err
}
if vcsConfig.LocalPath != "" {
_, err = vcs.CloneOrUpdate(vcsConfig, log.Named("vcs"))
if err != nil {
return nil, err
}
err = os.Chdir(runtimeConfig.Root)
if err != nil {
return nil, err
}
builderConfig.Source = vcsConfig.LocalPath
publicDir := filepath.Join(runtimeConfig.Root, "public")
builderConfig.Destination = publicDir
runtimeConfig.Root = publicDir
} else {
log.Warn("in production mode without VCS configuration")
}
}
config, err := cfg.GetConfig(builderConfig.Source, log.Named("config"))
if err != nil {
return nil, errors.WithMessage(err, "error parsing configuration file")
}
if runtimeConfig.Development {
applyDevModeOverrides(config, runtimeConfig)
}
top := http.NewServeMux()
err = rebuild(builderConfig, config, log)
if err != nil {
return nil, err
}
fixupMIMETypes(log)
if runtimeConfig.Development {
liveReload := livereload.New()
top.Handle("/_/reload", liveReload)
liveReload.Start()
fw, err := NewFileWatcher(log.Named("watcher"))
if err != nil {
return nil, errors.WithMessage(err, "could not create file watcher")
}
for _, dir := range []string{"content", "static", "templates", "internal/builder"} {
err := fw.AddRecursive(dir)
if err != nil {
return nil, errors.WithMessagef(
err,
"could not add directory %s to file watcher",
dir,
)
}
}
err = fw.Add(".")
if err != nil {
return nil, errors.WithMessage(err, "could not add directory to file watcher")
}
go fw.Start(func(filename string) {
log.Info("rebuilding site", "changed_file", filename)
err := rebuild(builderConfig, config, log)
if err != nil {
log.Error("error rebuilding site", "error", err)
}
})
}
loggingMux := http.NewServeMux()
mux, err := website.NewMux(config, runtimeConfig.Root, log.Named("website"))
if err != nil {
return nil, errors.WithMessage(err, "could not create website mux")
}
if runtimeConfig.Redirect {
re := regexp.MustCompile(
"^(.*)\\." + strings.ReplaceAll(config.WildcardDomain, ".", `\.`) + "$",
)
replace := "${1}." + config.Domains[0]
loggingMux.Handle(config.BaseURL.Hostname()+"/", mux)
loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch true {
case slices.Contains(config.Domains, r.Host):
path, _ := website.CanonicalisePath(r.URL.Path)
http.Redirect(
w,
r,
config.BaseURL.JoinPath(path).String(),
http.StatusMovedPermanently,
)
case re.MatchString(r.Host):
url := config.BaseURL
url.Host = re.ReplaceAllString(r.Host, replace)
http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect)
case true:
http.NotFound(w, r)
}
})
} else {
loggingMux.Handle("/", mux)
}
top.Handle("/",
serverHeaderHandler(
wrapHandlerWithLogging(loggingMux, log),
),
)
top.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
return &Server{
Server: &http.Server{
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 2 * time.Minute,
IdleTimeout: 10 * time.Minute,
Handler: top,
},
log: log,
config: config,
runtimeConfig: runtimeConfig,
}, nil
}
func (s *Server) serve(tls bool) error {
if tls {
return s.serveTLS()
}
return s.serveTCP()
}
func (s *Server) Start() error {
if err := s.serve(s.runtimeConfig.TLS); err != http.ErrServerClosed {
return errors.WithMessage(err, "error creating/closing server")
}
return nil
}
func (s *Server) Stop() chan struct{} {
s.log.Debug("stop called")
idleConnsClosed := make(chan struct{})
go func() {
s.log.Debug("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := s.Server.Shutdown(ctx)
s.log.Debug("server shut down")
if err != nil {
// Error from closing listeners, or context timeout:
s.log.Warn("HTTP server Shutdown", "error", err)
}
close(idleConnsClosed)
}()
return idleConnsClosed
}
|