@@ -40,10 +40,11 @@ import (
4040//
4141// See also [Server] which manages instances and enables reloading them.
4242type Instance struct {
43+ id int64
44+ handler http.Handler
45+
4346 config Config
44- id int64
4547
46- router * http.ServeMux
4748 files map [string ]* fileInfo
4849 templates * template.Template
4950 funcs template.FuncMap
@@ -199,6 +200,17 @@ func (config *Config) Instance(cfgs ...Option) (*Instance, *InstanceStats, []Ins
199200 slog .Int ("staticFilesAlternateEncodings" , build .StaticFilesAlternateEncodings ),
200201 ))
201202
203+ if ! config .CrossOrigin .Disabled {
204+ handler := http .NewCrossOriginProtection ()
205+ for _ , origin := range config .CrossOrigin .TrustedOrigins {
206+ handler .AddTrustedOrigin (origin )
207+ }
208+ for _ , pattern := range config .CrossOrigin .InsecureBypassPatterns {
209+ handler .AddInsecureBypassPattern (pattern )
210+ }
211+ build .handler = handler .Handler (build .router )
212+ }
213+
202214 return build .Instance , build .InstanceStats , build .routes , nil
203215}
204216
@@ -251,7 +263,7 @@ func (instance *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request) {
251263 log := instance .config .Logger .With (slog .Group ("serve" ,
252264 slog .String ("requestid" , rid ),
253265 ))
254- log .LogAttrs (r . Context () , slog .LevelDebug , "serving request" ,
266+ log .LogAttrs (ctx , slog .LevelDebug , "serving request" ,
255267 slog .String ("user-agent" , r .Header .Get ("User-Agent" )),
256268 slog .String ("method" , r .Method ),
257269 slog .String ("requestPath" , r .URL .Path ),
@@ -261,7 +273,7 @@ func (instance *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request) {
261273 r = r .WithContext (ctx )
262274 metrics := httpsnoop .CaptureMetrics (instance .router , w , r )
263275
264- log .LogAttrs (r . Context () , levelDebug2 , "request served" ,
276+ log .LogAttrs (ctx , levelDebug2 , "request served" ,
265277 slog .Group ("response" ,
266278 slog .Duration ("duration" , metrics .Duration ),
267279 slog .Int ("statusCode" , metrics .Code ),
0 commit comments