-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstance.go
295 lines (259 loc) · 8.27 KB
/
instance.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
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
package xtemplate
import (
"bytes"
"context"
"fmt"
"html/template"
"io/fs"
"log/slog"
"maps"
"net/http"
"net/http/httptest"
"os"
"reflect"
"regexp"
"slices"
"strings"
"sync/atomic"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/felixge/httpsnoop"
"github.com/google/uuid"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go/jetstream"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/svg"
)
// Instance is a configured, immutable, xtemplate request handler ready to
// execute templates and serve static files in response to http requests.
//
// The only way to create a valid Instance is to call the [Config.Instance]
// method. Configuration of an Instance is intended to be immutable. Instead of
// mutating a running Instance, build a new Instance from a modified Config and
// swap them.
//
// See also [Server] which manages instances and enables reloading them.
type Instance struct {
config Config
id int64
router *http.ServeMux
files map[string]*fileInfo
templates *template.Template
funcs template.FuncMap
natsServer *server.Server
natsClient *jetstream.JetStream
bufferDot dot
flusherDot dot
}
// Instance creates a new *Instance from the given config
func (config *Config) Instance(cfgs ...Option) (*Instance, *InstanceStats, []InstanceRoute, error) {
start := time.Now()
build := &builder{
Instance: &Instance{
config: *config.Defaults(),
id: nextInstanceIdentity.Add(1),
},
InstanceStats: &InstanceStats{},
}
if _, err := build.config.Options(cfgs...); err != nil {
return nil, nil, nil, err
}
build.config.Logger = build.config.Logger.With(slog.Int64("instance", build.id))
build.config.Logger.Info("initializing")
if build.config.TemplatesFS == nil {
build.config.TemplatesFS = os.DirFS(build.config.TemplatesDir)
}
{
build.funcs = template.FuncMap{}
maps.Copy(build.funcs, xtemplateFuncs)
maps.Copy(build.funcs, sprig.HtmlFuncMap())
for _, extra := range build.config.FuncMaps {
maps.Copy(build.funcs, extra)
}
}
build.files = make(map[string]*fileInfo)
build.router = http.NewServeMux()
build.templates = template.New(".").Delims(build.config.LDelim, build.config.RDelim).Funcs(build.funcs)
if config.Minify {
m := minify.New()
m.Add("text/css", &css.Minifier{})
m.Add("image/svg+xml", &svg.Minifier{})
m.Add("text/html", &html.Minifier{
TemplateDelims: [...]string{build.config.LDelim, build.config.RDelim},
})
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), &js.Minifier{})
build.m = m
}
if err := fs.WalkDir(build.config.TemplatesFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
if strings.HasSuffix(path, build.config.TemplateExtension) {
err = build.addTemplateHandler(path)
} else {
err = build.addStaticFileHandler(path)
}
return err
}); err != nil {
return nil, nil, nil, fmt.Errorf("error scanning files: %w", err)
}
dcInstance := dotXProvider{build.Instance}
dcReq := dotReqProvider{}
dcResp := dotRespProvider{}
dcFlush := dotFlushProvider{}
var dot []DotConfig
{
names := map[string]int{}
for _, d := range build.config.Databases {
dot = append(dot, &d)
names[d.FieldName()] += 1
}
for _, d := range build.config.Flags {
dot = append(dot, &d)
names[d.FieldName()] += 1
}
for _, d := range build.config.Directories {
dot = append(dot, &d)
names[d.FieldName()] += 1
}
for _, d := range build.config.Nats {
dot = append(dot, &d)
names[d.FieldName()] += 1
}
for _, d := range build.config.CustomProviders {
dot = append(dot, d)
names[d.FieldName()] += 1
}
for name, count := range names {
if count > 1 {
return nil, nil, nil, fmt.Errorf("dot field name '%s' is used %d times", name, count)
}
}
for _, d := range dot {
err := d.Init(build.config.Ctx)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to initialize dot field '%s': %w", d.FieldName(), err)
}
}
}
build.bufferDot = makeDot(slices.Concat([]DotConfig{dcInstance, dcReq}, dot, []DotConfig{dcResp}))
build.flusherDot = makeDot(slices.Concat([]DotConfig{dcInstance, dcReq}, dot, []DotConfig{dcFlush}))
{
// Invoke all initilization templates, aka any template whose name starts
// with "INIT ".
makeDot := func() (*reflect.Value, error) {
w, r := httptest.NewRecorder(), httptest.NewRequest("", "/", nil)
return build.bufferDot.value(build.config.Ctx, w, r)
}
cleanup := build.bufferDot.cleanup
buf := new(bytes.Buffer)
for _, tmpl := range build.templates.Templates() {
buf.Reset()
if strings.HasPrefix(tmpl.Name(), "INIT ") {
val, err := makeDot()
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to initialize dot value: %w", err)
}
err = tmpl.Execute(buf, *val)
if err = cleanup(val, err); err != nil {
return nil, nil, nil, fmt.Errorf("template initializer '%s' failed: %w", tmpl.Name(), err)
}
// TODO: output buffer somewhere?
build.config.Logger.Debug("executed initializer", slog.String("template_name", tmpl.Name()), slog.Int("rendered_len", buf.Len()))
build.TemplateInitializers += 1
}
}
}
build.config.Logger.Info("instance loaded",
slog.Duration("load_time", time.Since(start)),
slog.Group("stats",
slog.Int("routes", build.Routes),
slog.Int("templateFiles", build.TemplateFiles),
slog.Int("templateDefinitions", build.TemplateDefinitions),
slog.Int("templateInitializers", build.TemplateInitializers),
slog.Int("staticFiles", build.StaticFiles),
slog.Int("staticFilesAlternateEncodings", build.StaticFilesAlternateEncodings),
))
return build.Instance, build.InstanceStats, build.routes, nil
}
// Counter to assign a unique id to each instance of xtemplate created when
// calling Config.Instance(). This is intended to help distinguish logs from
// multiple instances in a single process.
var nextInstanceIdentity atomic.Int64
// Id returns the id of this instance which is unique in the current
// process. This differentiates multiple instances, as the instance id
// is attached to all logs generated by the instance with the attribute name
// `xtemplate.instance`.
func (x *Instance) Id() int64 {
return x.id
}
var (
levelDebug2 slog.Level = slog.LevelDebug + 2
)
func (instance *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request) {
select {
case <-instance.config.Ctx.Done():
instance.config.Logger.Error("received request after xtemplate instance cancelled", slog.String("method", r.Method), slog.String("path", r.URL.Path))
http.Error(w, "server stopped", http.StatusInternalServerError)
return
default:
}
ctx := r.Context()
rid := GetRequestId(ctx)
if rid == "" {
rid = uuid.NewString()
ctx = context.WithValue(ctx, requestIdKey, rid)
}
log := instance.config.Logger.With(slog.Group("serve",
slog.String("requestid", rid),
))
log.LogAttrs(r.Context(), slog.LevelDebug, "serving request",
slog.String("user-agent", r.Header.Get("User-Agent")),
slog.String("method", r.Method),
slog.String("requestPath", r.URL.Path),
)
ctx = context.WithValue(ctx, loggerKey, log)
r = r.WithContext(ctx)
metrics := httpsnoop.CaptureMetrics(instance.router, w, r)
log.LogAttrs(r.Context(), levelDebug2, "request served",
slog.Group("response",
slog.Duration("duration", metrics.Duration),
slog.Int("statusCode", metrics.Code),
slog.Int64("bytes", metrics.Written),
// Uncomment after release with this commit: https://github.com/golang/go/commit/a523152ea1df8d39d923ed90d19662896eff0607
// slog.String("pattern", r.Pattern),
))
}
type requestIdType struct{}
var requestIdKey = requestIdType{}
func GetRequestId(ctx context.Context) string {
// xtemplate request id
if av := ctx.Value(requestIdKey); av != nil {
if v, ok := av.(string); ok {
return v
}
}
// caddy request id
if v := ctx.Value("vars"); v != nil {
if mv, ok := v.(map[string]any); ok {
if anyrid, ok := mv["uuid"]; ok {
if rid, ok := anyrid.(string); ok {
return rid
}
}
}
}
return ""
}
type loggerType struct{}
var loggerKey = loggerType{}
func GetLogger(ctx context.Context) *slog.Logger {
log, ok := ctx.Value(loggerKey).(*slog.Logger)
if !ok {
return slog.Default()
}
return log
}