going-further/api/common.go
2024-04-22 21:53:20 -04:00

39 lines
944 B
Go

package api
import (
"log/slog"
"net/http"
)
func versionHandleFunc(log *slog.Logger, version string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Debug("version handler")
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
}
func staticHandleFunc() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
}
}
func sessionMiddleware(sessions SessionProvider, log *slog.Logger) func(http.HandlerFunc) http.HandlerFunc {
return func(h http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("FURTHUR_SESS")
if err != nil {
log.Warn("error loading session")
http.NotFound(w, r)
return
}
if !sessions.Valid(c.Value) {
log.Warn("user provided invalid session")
http.NotFound(w, r)
return
}
h(w, r)
})
}
}