Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f7ff28c9a |
@@ -3,14 +3,20 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
|
_ "net/http/pprof"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go func() {
|
||||||
|
http.ListenAndServe("localhost:6060", nil)
|
||||||
|
}()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx, cancle := context.WithCancel(ctx)
|
ctx, cancle := context.WithCancel(ctx)
|
||||||
defer cancle()
|
defer cancle()
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type captureWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
code int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *captureWriter) WriteHeader(statusCode int) {
|
||||||
|
c.code = statusCode
|
||||||
|
c.ResponseWriter.WriteHeader(statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
cw := &captureWriter{
|
||||||
|
ResponseWriter: w,
|
||||||
|
code: 200,
|
||||||
|
}
|
||||||
|
next.ServeHTTP(cw, r)
|
||||||
|
slog.Info("request",
|
||||||
|
"method", r.Method,
|
||||||
|
"path", r.URL.Path,
|
||||||
|
"duration", time.Since(start),
|
||||||
|
"status", cw.code,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// cacheMiddleware adds Cache-Control headers to the response.
|
||||||
|
func cacheMiddleware(maxAge time.Duration) func(next http.Handler) http.Handler {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
|
||||||
|
w.Header().Set("Vary", "HX-Request,Accept-Language,Accept-Encoding")
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -61,15 +61,3 @@ func (s *Server) registerOther() (h http.Handler) {
|
|||||||
h = cacheMiddleware(time.Minute * 5)(h)
|
h = cacheMiddleware(time.Minute * 5)(h)
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
start := time.Now()
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
slog.Info("request",
|
|
||||||
"method", r.Method,
|
|
||||||
"path", r.URL.Path,
|
|
||||||
"duration", time.Since(start),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -38,14 +37,3 @@ func registerStatic(mux *http.ServeMux) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cacheMiddleware adds Cache-Control headers to the response.
|
|
||||||
func cacheMiddleware(maxAge time.Duration) func(next http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
|
|
||||||
w.Header().Set("Vary", "HX-Request,Accept-Language,Accept-Encoding")
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
+1976
File diff suppressed because it is too large
Load Diff
|
After Width: | Height: | Size: 112 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 439 KiB |
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user