aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-02 18:22:34 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-02 18:22:34 +0200
commit1168ac60d3a22c0d354f291fd657ad302df6ec05 (patch)
treefc492948f751db0a72ec4a5ea1231b35d78c5b66
parentdfca0f5bbebfe11d9c5d8bf6584bd1bf80d62274 (diff)
feat(backend): use httplog
-rw-r--r--backend/data.go6
-rw-r--r--backend/router.go34
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--main.go4
5 files changed, 41 insertions, 7 deletions
diff --git a/backend/data.go b/backend/data.go
index e1a8478..bc74ba5 100644
--- a/backend/data.go
+++ b/backend/data.go
@@ -25,13 +25,11 @@ func (d *data) handleGeneric(w http.ResponseWriter, name string) {
},
}).ParseFS(templates, fmt.Sprintf("templates/%s.html", name), "templates/base.html")
if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- return
+ panic(err)
}
err = t.ExecuteTemplate(w, "base.html", d)
if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- return
+ panic(err)
}
}
diff --git a/backend/router.go b/backend/router.go
index b1efea8..8ed63fa 100644
--- a/backend/router.go
+++ b/backend/router.go
@@ -3,22 +3,52 @@ package backend
import (
"embed"
"io/fs"
+ "log/slog"
"net/http"
+ "os"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
+ "github.com/go-chi/httplog/v3"
)
+const Version = "0.1.0"
+
//go:embed templates
var templates embed.FS
-func NewRouter() *chi.Mux {
+func NewRouter(debug bool) *chi.Mux {
+ logFormat := httplog.SchemaECS.Concise(!debug)
+
+ logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
+ ReplaceAttr: logFormat.ReplaceAttr,
+ })).With(
+ slog.String("app", "anhgelus/small-web"),
+ slog.String("version", Version),
+ )
+
+ logLevel := slog.LevelWarn
+ if debug {
+ logLevel = slog.LevelDebug
+ }
+
r := chi.NewRouter()
r.Use(middleware.Timeout(30 * time.Second))
- r.Use(middleware.Logger)
+ r.Use(httplog.RequestLogger(logger, &httplog.Options{
+ Level: logLevel,
+ // Set log output to Elastic Common Schema (ECS) format.
+ Schema: logFormat,
+ RecoverPanics: true,
+ Skip: func(req *http.Request, respStatus int) bool {
+ return respStatus == http.StatusNotFound || respStatus == http.StatusMethodNotAllowed
+ },
+ // Optionally, log selected request/response headers explicitly.
+ LogRequestHeaders: []string{"Origin"},
+ LogResponseHeaders: []string{},
+ }))
return r
}
diff --git a/go.mod b/go.mod
index a5ede82..35e75ed 100644
--- a/go.mod
+++ b/go.mod
@@ -6,3 +6,5 @@ require (
github.com/go-chi/chi/v5 v5.2.3
github.com/joho/godotenv v1.5.1
)
+
+require github.com/go-chi/httplog/v3 v3.2.2 // indirect
diff --git a/go.sum b/go.sum
index fe271b4..abad55a 100644
--- a/go.sum
+++ b/go.sum
@@ -1,4 +1,6 @@
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
+github.com/go-chi/httplog/v3 v3.2.2 h1:G0oYv3YYcikNjijArHFUlqfR78cQNh9fGT43i6StqVc=
+github.com/go-chi/httplog/v3 v3.2.2/go.mod h1:N/J1l5l1fozUrqIVuT8Z/HzNeSy8TF2EFyokPLe6y2w=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
diff --git a/main.go b/main.go
index 11ebbe7..e1c13bb 100644
--- a/main.go
+++ b/main.go
@@ -24,6 +24,7 @@ var (
configFile = "config.toml"
port = 8000
publicDir = "public"
+ dev = false
)
func init() {
@@ -49,12 +50,13 @@ func init() {
publicDir = v
}
flag.StringVar(&publicDir, "public", publicDir, "public directory")
+ flag.BoolVar(&dev, "dev", false, "development mode")
}
func main() {
flag.Parse()
- r := backend.NewRouter()
+ r := backend.NewRouter(dev)
backend.HandleHome(r)