made htmx logging a flag that can be toggled
This commit is contained in:
@@ -3,7 +3,7 @@ testdata_dir = "testdata"
|
|||||||
tmp_dir = "tmp"
|
tmp_dir = "tmp"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
args_bin = []
|
args_bin = ["--htmxlog"]
|
||||||
bin = "./tmp/main"
|
bin = "./tmp/main"
|
||||||
cmd = "go build -o ./tmp/main ./cmd/oslstats"
|
cmd = "go build -o ./tmp/main ./cmd/oslstats"
|
||||||
delay = 1000
|
delay = 1000
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
func setupHttpServer(
|
func setupHttpServer(
|
||||||
staticFS *fs.FS,
|
staticFS *fs.FS,
|
||||||
config *config.Config,
|
cfg *config.Config,
|
||||||
logger *hlog.Logger,
|
logger *hlog.Logger,
|
||||||
bun *bun.DB,
|
bun *bun.DB,
|
||||||
store *store.Store,
|
store *store.Store,
|
||||||
@@ -27,7 +27,7 @@ func setupHttpServer(
|
|||||||
return nil, errors.New("No filesystem provided")
|
return nil, errors.New("No filesystem provided")
|
||||||
}
|
}
|
||||||
fs := http.FS(*staticFS)
|
fs := http.FS(*staticFS)
|
||||||
httpServer, err := hws.NewServer(config.HWS)
|
httpServer, err := hws.NewServer(cfg.HWS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "hws.NewServer")
|
return nil, errors.Wrap(err, "hws.NewServer")
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ func setupHttpServer(
|
|||||||
"/static/favicon.ico",
|
"/static/favicon.ico",
|
||||||
}
|
}
|
||||||
|
|
||||||
auth, err := setupAuth(config.HWSAuth, logger, bun, httpServer, ignoredPaths)
|
auth, err := setupAuth(cfg.HWSAuth, logger, bun, httpServer, ignoredPaths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "setupAuth")
|
return nil, errors.Wrap(err, "setupAuth")
|
||||||
}
|
}
|
||||||
@@ -57,14 +57,14 @@ func setupHttpServer(
|
|||||||
return nil, errors.Wrap(err, "httpServer.LoggerIgnorePaths")
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "addRoutes")
|
return nil, errors.Wrap(err, "addRoutes")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = addMiddleware(httpServer, auth)
|
err = addMiddleware(httpServer, auth, cfg.Flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "httpServer.AddMiddleware")
|
return nil, errors.Wrap(err, "addMiddleware")
|
||||||
}
|
}
|
||||||
|
|
||||||
return httpServer, nil
|
return httpServer, nil
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"git.haelnorr.com/h/golib/hws"
|
"git.haelnorr.com/h/golib/hws"
|
||||||
"git.haelnorr.com/h/golib/hwsauth"
|
"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/internal/db"
|
||||||
|
"git.haelnorr.com/h/oslstats/pkg/contexts"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
@@ -12,13 +17,26 @@ import (
|
|||||||
func addMiddleware(
|
func addMiddleware(
|
||||||
server *hws.Server,
|
server *hws.Server,
|
||||||
auth *hwsauth.Authenticator[*db.User, bun.Tx],
|
auth *hwsauth.Authenticator[*db.User, bun.Tx],
|
||||||
|
flags *config.Flags,
|
||||||
) error {
|
) error {
|
||||||
|
|
||||||
err := server.AddMiddleware(
|
err := server.AddMiddleware(
|
||||||
auth.Authenticate(),
|
auth.Authenticate(),
|
||||||
|
htmxLog(flags.HTMXLog),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "server.AddMiddleware")
|
return errors.Wrap(err, "server.AddMiddleware")
|
||||||
}
|
}
|
||||||
return nil
|
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/env v0.9.1
|
||||||
git.haelnorr.com/h/golib/ezconf v0.1.1
|
git.haelnorr.com/h/golib/ezconf v0.1.1
|
||||||
git.haelnorr.com/h/golib/hlog v0.10.4
|
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
|
git.haelnorr.com/h/golib/hwsauth v0.5.2
|
||||||
github.com/a-h/templ v0.3.977
|
github.com/a-h/templ v0.3.977
|
||||||
github.com/joho/godotenv v1.5.1
|
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/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 h1:vpCsV/OddjIYx8F48U66WxojjmhEbeLGQAOBG4ViSRQ=
|
||||||
git.haelnorr.com/h/golib/hlog v0.10.4/go.mod h1:+wJ8vecQY/JITTXKmI3JfkHiUGyMs7N6wooj2wuWZbc=
|
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.2 h1:OSwCwVxDerermyuoxrAYgt+i1zzwF/0Baoy8zmCxtuQ=
|
||||||
git.haelnorr.com/h/golib/hws v0.3.1/go.mod h1:6ZlRKnt8YMpv5XcMXmyBGmD1/euvBo3d1azEvHJjOLo=
|
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 h1:K4McXMEHtI5o4fAL3AZrmaMkwORNqSTV3MM6BExNKag=
|
||||||
git.haelnorr.com/h/golib/hwsauth v0.5.2/go.mod h1:NOonrVU/lX8lzuV77eDEiTwBjn7RrzYVcSdXUJWeHmQ=
|
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=
|
git.haelnorr.com/h/golib/jwt v0.10.1 h1:1Adxt9H3Y4fWFvFjWpvg/vSFhbgCMDMxgiE3m7KvDMI=
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type Flags struct {
|
|||||||
ShowEnv bool
|
ShowEnv bool
|
||||||
GenEnv string
|
GenEnv string
|
||||||
EnvFile string
|
EnvFile string
|
||||||
|
HTMXLog bool
|
||||||
|
|
||||||
// Database reset (destructive)
|
// Database reset (destructive)
|
||||||
ResetDB bool
|
ResetDB bool
|
||||||
@@ -33,6 +34,7 @@ func SetupFlags() (*Flags, error) {
|
|||||||
showEnv := flag.Bool("showenv", false, "Print all environment variable values and their documentation")
|
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)")
|
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")
|
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)
|
// Database reset (destructive)
|
||||||
resetDB := flag.Bool("reset-db", false, "⚠️ DESTRUCTIVE: Drop and recreate all tables (dev only)")
|
resetDB := flag.Bool("reset-db", false, "⚠️ DESTRUCTIVE: Drop and recreate all tables (dev only)")
|
||||||
@@ -76,6 +78,7 @@ func SetupFlags() (*Flags, error) {
|
|||||||
ShowEnv: *showEnv,
|
ShowEnv: *showEnv,
|
||||||
GenEnv: *genEnv,
|
GenEnv: *genEnv,
|
||||||
EnvFile: *envfile,
|
EnvFile: *envfile,
|
||||||
|
HTMXLog: *htmxlog,
|
||||||
ResetDB: *resetDB,
|
ResetDB: *resetDB,
|
||||||
MigrateUp: *migrateUp,
|
MigrateUp: *migrateUp,
|
||||||
MigrateRollback: *migrateRollback,
|
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/popup"
|
||||||
import "git.haelnorr.com/h/oslstats/internal/view/component/nav"
|
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/internal/view/component/footer"
|
||||||
|
import "git.haelnorr.com/h/oslstats/pkg/contexts"
|
||||||
|
|
||||||
// Global page layout. Includes HTML document settings, header tags
|
// Global page layout. Includes HTML document settings, header tags
|
||||||
// navbar and footer
|
// navbar and footer
|
||||||
templ Global(title string) {
|
templ Global(title string) {
|
||||||
|
{{ htmxLogging := contexts.HTMXLog(ctx) }}
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html
|
<html
|
||||||
lang="en"
|
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 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 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 src="https://unpkg.com/alpinejs" defer></script>
|
||||||
<script>
|
if htmxLogging {
|
||||||
// uncomment this line to enable logging of htmx events
|
<script>
|
||||||
// htmx.logAll();
|
htmx.logAll();
|
||||||
</script>
|
</script>
|
||||||
|
}
|
||||||
<script>
|
<script>
|
||||||
const bodyData = {
|
const bodyData = {
|
||||||
showError500: false,
|
showError500: false,
|
||||||
|
|||||||
@@ -1,7 +1,19 @@
|
|||||||
package contexts
|
package contexts
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
type Key string
|
type Key string
|
||||||
|
|
||||||
func (c Key) String() string {
|
func (c Key) String() string {
|
||||||
return "oslstats context key " + string(c)
|
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