migrated out more modules and refactored db system

This commit is contained in:
2026-01-01 21:56:21 +11:00
parent 03095448d6
commit 1e09acdc57
80 changed files with 462 additions and 4992 deletions

View File

@@ -1,23 +1,25 @@
package httpserver
import (
"database/sql"
"net/http"
"projectreshoot/internal/handler"
"projectreshoot/internal/middleware"
"projectreshoot/internal/view/page"
"projectreshoot/pkg/config"
"projectreshoot/pkg/db"
"github.com/rs/zerolog"
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/jwt"
)
// Add all the handled routes to the mux
func addRoutes(
mux *http.ServeMux,
logger *zerolog.Logger,
logger *hlog.Logger,
config *config.Config,
conn *db.SafeConn,
tokenGen *jwt.TokenGenerator,
conn *sql.DB,
staticFS *http.FileSystem,
) {
route := mux.Handle
@@ -39,17 +41,17 @@ func addRoutes(
// Login page and handlers
route("GET /login", loggedOut(handler.LoginPage(config.TrustedHost)))
route("POST /login", loggedOut(handler.LoginRequest(config, logger, conn)))
route("POST /login", loggedOut(handler.LoginRequest(config, logger, conn, tokenGen)))
// Register page and handlers
route("GET /register", loggedOut(handler.RegisterPage(config.TrustedHost)))
route("POST /register", loggedOut(handler.RegisterRequest(config, logger, conn)))
route("POST /register", loggedOut(handler.RegisterRequest(config, tokenGen, logger, conn)))
// Logout
route("POST /logout", handler.Logout(config, logger, conn))
route("POST /logout", handler.Logout(conn, tokenGen, logger))
// Reauthentication request
route("POST /reauthenticate", loggedIn(handler.Reauthenticate(logger, config, conn)))
route("POST /reauthenticate", loggedIn(handler.Reauthenticate(logger, config, conn, tokenGen)))
// Profile page
route("GET /profile", loggedIn(handler.ProfilePage()))

View File

@@ -1,6 +1,7 @@
package httpserver
import (
"database/sql"
"io/fs"
"net"
"net/http"
@@ -8,20 +9,21 @@ import (
"projectreshoot/internal/middleware"
"projectreshoot/pkg/config"
"projectreshoot/pkg/db"
"github.com/rs/zerolog"
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/jwt"
)
func NewServer(
config *config.Config,
logger *zerolog.Logger,
conn *db.SafeConn,
logger *hlog.Logger,
conn *sql.DB,
tokenGen *jwt.TokenGenerator,
staticFS *fs.FS,
maint *uint32,
) *http.Server {
fs := http.FS(*staticFS)
srv := createServer(config, logger, conn, &fs, maint)
srv := createServer(config, logger, conn, tokenGen, &fs, maint)
httpServer := &http.Server{
Addr: net.JoinHostPort(config.Host, config.Port),
Handler: srv,
@@ -35,8 +37,9 @@ func NewServer(
// Returns a new http.Handler with all the routes and middleware added
func createServer(
config *config.Config,
logger *zerolog.Logger,
conn *db.SafeConn,
logger *hlog.Logger,
conn *sql.DB,
tokenGen *jwt.TokenGenerator,
staticFS *http.FileSystem,
maint *uint32,
) http.Handler {
@@ -45,6 +48,7 @@ func createServer(
mux,
logger,
config,
tokenGen,
conn,
staticFS,
)
@@ -52,7 +56,7 @@ func createServer(
// Add middleware here, must be added in reverse order of execution
// i.e. First in list will get executed last during the request handling
handler = middleware.Logging(logger, handler)
handler = middleware.Authentication(logger, config, conn, handler, maint)
handler = middleware.Authentication(logger, config, conn, tokenGen, handler, maint)
// Gzip
handler = middleware.Gzip(handler, config.GZIP)