Simplified the database layer by removing custom interface wrappers and using standard library *sql.DB and *sql.Tx types directly. Changes: - Removed DBConnection and DBTransaction interfaces from database.go - Removed NewDBConnection() wrapper function - Updated TokenGenerator to use *sql.DB instead of DBConnection - Updated all validation and revocation methods to accept *sql.Tx - Updated TableManager to work with *sql.DB directly - Updated all tests to use db.Begin() instead of custom wrappers - Fixed GeneratorConfig.DB field (was DBConn) - Updated documentation in doc.go with correct API usage Benefits: - Simpler API with fewer abstractions - Works directly with database/sql standard library - Compatible with GORM (via gormDB.DB()) and Bun (share same *sql.DB) - Easier to understand and maintain - No unnecessary wrapper layers Breaking changes: - GeneratorConfig.DBConn renamed to GeneratorConfig.DB - Removed NewDBConnection() function - pass *sql.DB directly - ValidateAccess/ValidateRefresh now accept *sql.Tx instead of DBTransaction - Token.Revoke/CheckNotRevoked now accept *sql.Tx instead of DBTransaction 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package hws
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
)
|
|
|
|
type Middleware func(h http.Handler) http.Handler
|
|
type MiddlewareFunc func(w http.ResponseWriter, r *http.Request) (*http.Request, *HWSError)
|
|
|
|
// Server.AddMiddleware registers all the middleware.
|
|
// Middleware will be run in the order that they are provided.
|
|
func (server *Server) AddMiddleware(middleware ...Middleware) error {
|
|
if !server.routes {
|
|
return errors.New("Server.AddRoutes must be called before Server.AddMiddleware")
|
|
}
|
|
|
|
// RUN LOGGING MIDDLEWARE FIRST
|
|
server.server.Handler = logging(server.server.Handler, server.logger)
|
|
|
|
// LOOP PROVIDED MIDDLEWARE IN REVERSE order
|
|
for i := len(middleware); i > 0; i-- {
|
|
server.server.Handler = middleware[i-1](server.server.Handler)
|
|
}
|
|
|
|
// RUN GZIP
|
|
if server.GZIP {
|
|
server.server.Handler = addgzip(server.server.Handler)
|
|
}
|
|
// RUN TIMER MIDDLEWARE LAST
|
|
server.server.Handler = startTimer(server.server.Handler)
|
|
|
|
server.middleware = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// NewMiddleware returns a new Middleware for the server.
|
|
// A MiddlewareFunc is a function that takes in a http.ResponseWriter and http.Request,
|
|
// and returns a new request and optional HWSError.
|
|
// If a HWSError is returned, server.ThrowError will be called.
|
|
// If HWSError.RenderErrorPage is true, the request chain will be terminated and the error page rendered
|
|
func (server *Server) NewMiddleware(
|
|
middlewareFunc MiddlewareFunc,
|
|
) Middleware {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
newReq, herr := middlewareFunc(w, r)
|
|
if herr != nil {
|
|
server.ThrowError(w, r, *herr)
|
|
if herr.RenderErrorPage {
|
|
return
|
|
}
|
|
}
|
|
next.ServeHTTP(w, newReq)
|
|
})
|
|
}
|
|
}
|