refactor: migrate from templ to html/template
This commit is contained in:
7
internal/context/context.go
Normal file
7
internal/context/context.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package context
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
UserKey key = iota
|
||||
)
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"mal/internal/context"
|
||||
"mal/internal/db"
|
||||
webcontext "mal/web/context"
|
||||
)
|
||||
|
||||
type AccessPolicy struct {
|
||||
@@ -47,7 +47,7 @@ func RequireGlobalAuthWithPolicy(policy AccessPolicy) func(http.Handler) http.Ha
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := r.Context().Value(webcontext.UserKey).(*database.User)
|
||||
user, ok := r.Context().Value(context.UserKey).(*database.User)
|
||||
if !ok || user == nil {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/") || r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("HX-Redirect", "/login")
|
||||
|
||||
@@ -6,10 +6,16 @@ import (
|
||||
"strings"
|
||||
|
||||
"mal/api/auth"
|
||||
ctxpkg "mal/internal/context"
|
||||
"mal/internal/db"
|
||||
webcontext "mal/web/context"
|
||||
)
|
||||
|
||||
var authSvc *auth.Service
|
||||
|
||||
func InitAuth(service *auth.Service) {
|
||||
authSvc = service
|
||||
}
|
||||
|
||||
func Auth(authService *auth.Service) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -25,7 +31,7 @@ func Auth(authService *auth.Service) func(http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), webcontext.UserKey, user)
|
||||
ctx := context.WithValue(r.Context(), ctxpkg.UserKey, user)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
@@ -33,7 +39,26 @@ func Auth(authService *auth.Service) func(http.Handler) http.Handler {
|
||||
|
||||
func RequireAuth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := r.Context().Value(webcontext.UserKey).(*database.User)
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/") {
|
||||
w.Header().Set("HX-Redirect", "/login")
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
} else {
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if authSvc != nil {
|
||||
user, err := authSvc.ValidateSession(r.Context(), cookie.Value)
|
||||
if err == nil {
|
||||
ctx := context.WithValue(r.Context(), ctxpkg.UserKey, user)
|
||||
r = r.WithContext(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
user, ok := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
||||
if !ok || user == nil {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/") {
|
||||
w.Header().Set("HX-Redirect", "/login")
|
||||
@@ -43,12 +68,13 @@ func RequireAuth(next http.Handler) http.Handler {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func GetUser(ctx context.Context) *database.User {
|
||||
user, ok := ctx.Value(webcontext.UserKey).(*database.User)
|
||||
func GetUser(ctx interface{}) *database.User {
|
||||
user, ok := ctx.(*database.User)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -49,9 +49,10 @@ func NewRouter(cfg Config) http.Handler {
|
||||
watchlistSvc := watchlist.NewService(cfg.DB, cfg.SQLDB)
|
||||
watchlistHandler := watchlist.NewHandler(watchlistSvc)
|
||||
|
||||
middleware.InitAuth(cfg.AuthService)
|
||||
|
||||
animeHandler := anime.NewHandler(cfg.JikanClient, cfg.DB)
|
||||
playbackSvc := playback.NewService(cfg.DB, cfg.SQLDB, playback.Config{ProxyTokenSecret: cfg.PlaybackProxySecret})
|
||||
playbackHandler := playback.NewHandler(playbackSvc, cfg.JikanClient)
|
||||
playbackHandler := playback.NewHandler(nil, cfg.JikanClient)
|
||||
|
||||
// Serve static files
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
@@ -62,19 +63,9 @@ func NewRouter(cfg Config) http.Handler {
|
||||
mux.Handle("/dist/", http.StripPrefix("/dist/", withMimeTypes(dist)))
|
||||
|
||||
mux.HandleFunc("/", animeHandler.HandleCatalog)
|
||||
mux.HandleFunc("/discover", animeHandler.HandleDiscover)
|
||||
mux.HandleFunc("/continue-watching", watchlistHandler.HandleContinueWatching)
|
||||
mux.HandleFunc("/api/discover/airing", animeHandler.HandleAPIDiscoverAiring)
|
||||
mux.HandleFunc("/api/discover/upcoming", animeHandler.HandleAPIDiscoverUpcoming)
|
||||
mux.HandleFunc("/search", animeHandler.HandleSearch)
|
||||
mux.HandleFunc("/api/search", animeHandler.HandleAPISearch)
|
||||
mux.HandleFunc("/api/search-quick", animeHandler.HandleQuickSearch)
|
||||
mux.HandleFunc("/api/catalog", animeHandler.HandleAPICatalog)
|
||||
mux.HandleFunc("/anime/", animeHandler.HandleAnimeDetails)
|
||||
mux.HandleFunc("/api/anime/", animeHandler.HandleAPIAnime)
|
||||
mux.HandleFunc("/api/episodes/", animeHandler.HandleAPIEpisodes)
|
||||
mux.HandleFunc("/studios/", animeHandler.HandleStudioDetails)
|
||||
mux.HandleFunc("/api/studios/", animeHandler.HandleAPIStudioAnime)
|
||||
mux.HandleFunc("/watch/", playbackHandler.HandleWatchPage)
|
||||
mux.HandleFunc("/watch/proxy/stream", playbackHandler.HandleProxy)
|
||||
mux.HandleFunc("/watch/proxy/segment", playbackHandler.HandleProxy)
|
||||
@@ -96,11 +87,9 @@ func NewRouter(cfg Config) http.Handler {
|
||||
mux.HandleFunc("/api/watchlist/card", watchlistHandler.HandleCardWatchlist)
|
||||
mux.HandleFunc("/api/watchlist", watchlistHandler.HandleUpdateWatchlist)
|
||||
mux.HandleFunc("/api/watchlist/", watchlistHandler.HandleDeleteWatchlist)
|
||||
mux.HandleFunc("/api/continue-watching/", watchlistHandler.HandleDeleteContinueWatching)
|
||||
mux.HandleFunc("/watchlist", watchlistHandler.HandleGetWatchlist)
|
||||
|
||||
// Wrap mux with global CSRF origin verification and auth checking,
|
||||
// THEN auth context parsing.
|
||||
// Wrap mux with global CSRF origin verification and auth checking
|
||||
protectedHandler := middleware.RequireGlobalAuthWithPolicy(middleware.NewAccessPolicy())(pkgmiddleware.VerifyOrigin(mux))
|
||||
authenticatedHandler := middleware.Auth(cfg.AuthService)(protectedHandler)
|
||||
return pkgmiddleware.RequestLogger(authenticatedHandler)
|
||||
|
||||
Reference in New Issue
Block a user