made htmx logging a flag that can be toggled
This commit is contained in:
@@ -3,7 +3,7 @@ testdata_dir = "testdata"
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
args_bin = []
|
||||
args_bin = ["--htmxlog"]
|
||||
bin = "./tmp/main"
|
||||
cmd = "go build -o ./tmp/main ./cmd/oslstats"
|
||||
delay = 1000
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
func setupHttpServer(
|
||||
staticFS *fs.FS,
|
||||
config *config.Config,
|
||||
cfg *config.Config,
|
||||
logger *hlog.Logger,
|
||||
bun *bun.DB,
|
||||
store *store.Store,
|
||||
@@ -27,7 +27,7 @@ func setupHttpServer(
|
||||
return nil, errors.New("No filesystem provided")
|
||||
}
|
||||
fs := http.FS(*staticFS)
|
||||
httpServer, err := hws.NewServer(config.HWS)
|
||||
httpServer, err := hws.NewServer(cfg.HWS)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "hws.NewServer")
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func setupHttpServer(
|
||||
"/static/favicon.ico",
|
||||
}
|
||||
|
||||
auth, err := setupAuth(config.HWSAuth, logger, bun, httpServer, ignoredPaths)
|
||||
auth, err := setupAuth(cfg.HWSAuth, logger, bun, httpServer, ignoredPaths)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "setupAuth")
|
||||
}
|
||||
@@ -57,14 +57,14 @@ func setupHttpServer(
|
||||
return nil, errors.Wrap(err, "httpServer.LoggerIgnorePaths")
|
||||
}
|
||||
|
||||
err = addRoutes(httpServer, &fs, config, bun, auth, store, discordAPI)
|
||||
err = addRoutes(httpServer, &fs, cfg, bun, auth, store, discordAPI)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "addRoutes")
|
||||
}
|
||||
|
||||
err = addMiddleware(httpServer, auth)
|
||||
err = addMiddleware(httpServer, auth, cfg.Flags)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "httpServer.AddMiddleware")
|
||||
return nil, errors.Wrap(err, "addMiddleware")
|
||||
}
|
||||
|
||||
return httpServer, nil
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/golib/hwsauth"
|
||||
"git.haelnorr.com/h/oslstats/internal/config"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/pkg/contexts"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
@@ -12,13 +17,26 @@ import (
|
||||
func addMiddleware(
|
||||
server *hws.Server,
|
||||
auth *hwsauth.Authenticator[*db.User, bun.Tx],
|
||||
flags *config.Flags,
|
||||
) error {
|
||||
|
||||
err := server.AddMiddleware(
|
||||
auth.Authenticate(),
|
||||
htmxLog(flags.HTMXLog),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "server.AddMiddleware")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func htmxLog(htmxlog bool) hws.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), contexts.HTMXLogKey, htmxlog)
|
||||
req := r.WithContext(ctx)
|
||||
next.ServeHTTP(w, req)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
||||
git.haelnorr.com/h/golib/env v0.9.1
|
||||
git.haelnorr.com/h/golib/ezconf v0.1.1
|
||||
git.haelnorr.com/h/golib/hlog v0.10.4
|
||||
git.haelnorr.com/h/golib/hws v0.3.1
|
||||
git.haelnorr.com/h/golib/hws v0.3.2
|
||||
git.haelnorr.com/h/golib/hwsauth v0.5.2
|
||||
github.com/a-h/templ v0.3.977
|
||||
github.com/joho/godotenv v1.5.1
|
||||
|
||||
4
go.sum
4
go.sum
@@ -6,8 +6,8 @@ git.haelnorr.com/h/golib/ezconf v0.1.1 h1:4euTSDb9jvuQQkVq+x5gHoYPYyUZPWxoOSlWCI
|
||||
git.haelnorr.com/h/golib/ezconf v0.1.1/go.mod h1:rETDcjpcEyyeBgCiZSU617wc0XycwZSC5+IAOtXmwP8=
|
||||
git.haelnorr.com/h/golib/hlog v0.10.4 h1:vpCsV/OddjIYx8F48U66WxojjmhEbeLGQAOBG4ViSRQ=
|
||||
git.haelnorr.com/h/golib/hlog v0.10.4/go.mod h1:+wJ8vecQY/JITTXKmI3JfkHiUGyMs7N6wooj2wuWZbc=
|
||||
git.haelnorr.com/h/golib/hws v0.3.1 h1:uFXAT8SuKs4VACBdrkmZ+dJjeBlSPgCKUPt8zGCcwrI=
|
||||
git.haelnorr.com/h/golib/hws v0.3.1/go.mod h1:6ZlRKnt8YMpv5XcMXmyBGmD1/euvBo3d1azEvHJjOLo=
|
||||
git.haelnorr.com/h/golib/hws v0.3.2 h1:OSwCwVxDerermyuoxrAYgt+i1zzwF/0Baoy8zmCxtuQ=
|
||||
git.haelnorr.com/h/golib/hws v0.3.2/go.mod h1:6ZlRKnt8YMpv5XcMXmyBGmD1/euvBo3d1azEvHJjOLo=
|
||||
git.haelnorr.com/h/golib/hwsauth v0.5.2 h1:K4McXMEHtI5o4fAL3AZrmaMkwORNqSTV3MM6BExNKag=
|
||||
git.haelnorr.com/h/golib/hwsauth v0.5.2/go.mod h1:NOonrVU/lX8lzuV77eDEiTwBjn7RrzYVcSdXUJWeHmQ=
|
||||
git.haelnorr.com/h/golib/jwt v0.10.1 h1:1Adxt9H3Y4fWFvFjWpvg/vSFhbgCMDMxgiE3m7KvDMI=
|
||||
|
||||
@@ -12,6 +12,7 @@ type Flags struct {
|
||||
ShowEnv bool
|
||||
GenEnv string
|
||||
EnvFile string
|
||||
HTMXLog bool
|
||||
|
||||
// Database reset (destructive)
|
||||
ResetDB bool
|
||||
@@ -33,6 +34,7 @@ func SetupFlags() (*Flags, error) {
|
||||
showEnv := flag.Bool("showenv", false, "Print all environment variable values and their documentation")
|
||||
genEnv := flag.String("genenv", "", "Generate a .env file with all environment variables (specify filename)")
|
||||
envfile := flag.String("envfile", ".env", "Specify a .env file to use for the configuration")
|
||||
htmxlog := flag.Bool("htmxlog", false, "Run the server with all HTMX events logged")
|
||||
|
||||
// Database reset (destructive)
|
||||
resetDB := flag.Bool("reset-db", false, "⚠️ DESTRUCTIVE: Drop and recreate all tables (dev only)")
|
||||
@@ -76,6 +78,7 @@ func SetupFlags() (*Flags, error) {
|
||||
ShowEnv: *showEnv,
|
||||
GenEnv: *genEnv,
|
||||
EnvFile: *envfile,
|
||||
HTMXLog: *htmxlog,
|
||||
ResetDB: *resetDB,
|
||||
MigrateUp: *migrateUp,
|
||||
MigrateRollback: *migrateRollback,
|
||||
|
||||
@@ -3,10 +3,12 @@ package layout
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/component/popup"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/component/nav"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/component/footer"
|
||||
import "git.haelnorr.com/h/oslstats/pkg/contexts"
|
||||
|
||||
// Global page layout. Includes HTML document settings, header tags
|
||||
// navbar and footer
|
||||
templ Global(title string) {
|
||||
{{ htmxLogging := contexts.HTMXLog(ctx) }}
|
||||
<!DOCTYPE html>
|
||||
<html
|
||||
lang="en"
|
||||
@@ -27,10 +29,11 @@ templ Global(title string) {
|
||||
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/persist@3.x.x/dist/cdn.min.js"></script>
|
||||
<script src="https://unpkg.com/alpinejs" defer></script>
|
||||
<script>
|
||||
// uncomment this line to enable logging of htmx events
|
||||
// htmx.logAll();
|
||||
</script>
|
||||
if htmxLogging {
|
||||
<script>
|
||||
htmx.logAll();
|
||||
</script>
|
||||
}
|
||||
<script>
|
||||
const bodyData = {
|
||||
showError500: false,
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
package contexts
|
||||
|
||||
import "context"
|
||||
|
||||
type Key string
|
||||
|
||||
func (c Key) String() string {
|
||||
return "oslstats context key " + string(c)
|
||||
}
|
||||
|
||||
var HTMXLogKey Key = Key("htmxlog")
|
||||
|
||||
func HTMXLog(ctx context.Context) bool {
|
||||
htmxlog, ok := ctx.Value(HTMXLogKey).(bool)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return htmxlog
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user