Compare commits

...

3 commits

Author SHA1 Message Date
Lilian Jónsdóttir 177556399d more middleware 2024-01-24 23:43:27 -08:00
Lilian Jónsdóttir bedf6cb8e9 actual request logger middleware
instead of log calls in every handler
2024-01-24 23:42:40 -08:00
Lilian Jónsdóttir 108ba4d8c2 move middleware to its own file
in anticipation of maybe adding more
2024-01-24 23:33:08 -08:00
5 changed files with 31 additions and 7 deletions

View file

@ -11,9 +11,12 @@ import (
"git.burning.moe/celediel/burning.moe/internal/render"
)
// App wide config data and such
var app config.AppConfig
func main() {
// Initialise app and config
app := config.Initialise()
app = config.Initialise()
// Initialise handlers and renderer
handlers.Initialise(&app)

22
cmd/web/middleware.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"net/http"
"github.com/go-chi/chi/v5/middleware"
)
// Middleware is a slice of Middleware (aka func(n http.Handler) http.Handler {})
var Middleware []func(next http.Handler) http.Handler = []func(next http.Handler) http.Handler{
// chi's recommended list
middleware.RequestID,
middleware.RealIP,
// plus custom request logger
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
app.Logger.Info("REQUEST", "url", r.URL, "ip", r.RemoteAddr, "useragent", r.UserAgent())
next.ServeHTTP(w, r)
})
},
middleware.Recoverer,
}

View file

@ -4,7 +4,6 @@ import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"git.burning.moe/celediel/burning.moe/internal/config"
"git.burning.moe/celediel/burning.moe/internal/handlers"
@ -16,7 +15,9 @@ func routes(app *config.AppConfig) http.Handler {
mux := chi.NewRouter()
// Import some middleware
mux.Use(middleware.Recoverer)
for _, mw := range Middleware {
mux.Use(mw)
}
// Setup static file server
app.Logger.Debug("Setting up /static file server")

View file

@ -36,6 +36,8 @@ type ConfigDatabase struct {
// Initialises the app wide AppConfig, loads values from environment, and set up the Logger
func Initialise() AppConfig {
app := *defaults
// Setup logger
app.Logger = log.NewWithOptions(os.Stderr, log.Options{
ReportTimestamp: true,
TimeFormat: time.TimeOnly,

View file

@ -44,8 +44,6 @@ func Initialise(a *config.AppConfig) {
// HomeHandler handles /, generating data from Handlers
func HomeHandler(w http.ResponseWriter, r *http.Request) {
app.Logger.Info("Got request for homepage")
page := "home.page.tmpl"
d := models.TemplateData{}
@ -80,7 +78,6 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
// makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl
func makeBasicHandler(name string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
app.Logger.Infof("Got request for %s page", name)
pageName := name + ".page.tmpl"
render.RenderTemplate(w, pageName)
}
@ -95,7 +92,6 @@ func makeLinksHandler(name string) func(w http.ResponseWriter, r *http.Request)
app.Logger.Error(fmt.Sprintf("couldn't get %s from cache", page), "err", err)
}
app.Logger.Infof("Got request for %s links page", name)
data, err := td.LoadTemplateData(name)
if err != nil {
app.Logger.Fatal("couldn't load template data for "+name, "err", err)