about summary refs log tree commit diff
path: root/nix/templates/goapp/frontend/log.go
diff options
context:
space:
mode:
authorEmile <git@emile.space>2025-02-12 22:05:22 +0100
committerEmile <git@emile.space>2025-02-12 22:05:22 +0100
commit7152205c80f059d3649e1830fb4dfc46d1fc158f (patch)
treeb0d9a9aea760fabd61ee5cb06814d45cac5bb629 /nix/templates/goapp/frontend/log.go
parent4d08790c43b2d0720ef43b657a651a7c541d30d2 (diff)
template: the goapp docker package should now (in theory) build
It's quite a weird way to pull out the packge name from the attribute
set of defined packages, yet it kind of works.

I can't test it, as docker doesn't want to run Mach-O binaries, but
kicking this into hydra should result in some nice builds.
Diffstat (limited to 'nix/templates/goapp/frontend/log.go')
-rw-r--r--nix/templates/goapp/frontend/log.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/nix/templates/goapp/frontend/log.go b/nix/templates/goapp/frontend/log.go
new file mode 100644
index 0000000..5af719a
--- /dev/null
+++ b/nix/templates/goapp/frontend/log.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+	"net/http"
+	"os"
+
+	"github.com/gorilla/handlers"
+)
+
+// Defines a middleware containing a logfile
+//
+// This is done to combine gorilla/handlers with gorilla/mux middlewares to
+// just use r.Use(logger.Middleware) once instead of adding this to all
+// handlers manually (Yes, I'm really missing macros in Go...)
+type loggingMiddleware struct {
+	logFile *os.File
+}
+
+func (l *loggingMiddleware) Middleware(next http.Handler) http.Handler {
+	return handlers.LoggingHandler(l.logFile, next)
+}
+
+func authMiddleware(next http.Handler) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		session, _ := globalState.sessions.Get(r, "session")
+		username := session.Values["username"]
+
+		if username == nil {
+			http.Redirect(w, r, "/login", http.StatusSeeOther)
+		} else {
+			next.ServeHTTP(w, r)
+		}
+	})
+}