From 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 5 Aug 2023 19:26:29 +0200 Subject: ref: Move net/http I was young and naive, fasthttp does not fit my needs and make development slower. I'll move to net/http since it has a wider support and will spare some time on implementation detail things (like CSRF). It will allow me to reduce a bit of the code since there may be lib for handling cookie encryption and auth in general. --- pkg/view/auth.go | 77 +++++++++++++++++++++++++++----------------------- pkg/view/filesystem.go | 16 ++++++----- pkg/view/media.go | 53 +++++++++++++++++----------------- pkg/view/settings.go | 57 ++++++++++++++++++------------------- 4 files changed, 104 insertions(+), 99 deletions(-) (limited to 'pkg/view') diff --git a/pkg/view/auth.go b/pkg/view/auth.go index 631cfb3..2a4b95e 100644 --- a/pkg/view/auth.go +++ b/pkg/view/auth.go @@ -2,8 +2,7 @@ package view import ( "encoding/base64" - - "github.com/valyala/fasthttp" + "net/http" "git.sr.ht/~gabrielgio/img/pkg/ext" "git.sr.ht/~gabrielgio/img/pkg/service" @@ -20,71 +19,77 @@ func NewAuthView(userController *service.AuthController) *AuthView { } } -func (v *AuthView) LoginView(ctx *fasthttp.RequestCtx) error { - templates.WritePageTemplate(ctx, &templates.LoginPage{}) +func (v *AuthView) LoginView(w http.ResponseWriter, r *http.Request) error { + templates.WritePageTemplate(w, &templates.LoginPage{}) return nil } -func (v *AuthView) Logout(ctx *fasthttp.RequestCtx) error { - cook := fasthttp.Cookie{} - cook.SetKey("auth") - cook.SetValue("") - cook.SetMaxAge(-1) - cook.SetHTTPOnly(true) - cook.SetSameSite(fasthttp.CookieSameSiteDefaultMode) - ctx.Response.Header.SetCookie(&cook) +func (v *AuthView) Logout(w http.ResponseWriter, r *http.Request) error { + cook := http.Cookie{ + Name: "auth", + Value: "", + MaxAge: -1, + HttpOnly: true, + SameSite: http.SameSiteDefaultMode, + } + http.SetCookie(w, &cook) - ctx.Redirect("/", 307) + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) return nil } -func (v *AuthView) Login(ctx *fasthttp.RequestCtx) error { - username := ctx.FormValue("username") - password := ctx.FormValue("password") +func (v *AuthView) Login(w http.ResponseWriter, r *http.Request) error { + var ( + username = []byte(r.FormValue("username")) + password = []byte(r.FormValue("password")) + ) - auth, err := v.userController.Login(ctx, username, password) + auth, err := v.userController.Login(r.Context(), username, password) if err != nil { return err } base64Auth := base64.StdEncoding.EncodeToString(auth) - cook := fasthttp.Cookie{} - cook.SetKey("auth") - cook.SetValue(base64Auth) - cook.SetHTTPOnly(true) - cook.SetSameSite(fasthttp.CookieSameSiteDefaultMode) - ctx.Response.Header.SetCookie(&cook) + cook := http.Cookie{ + Name: "auth", + Value: base64Auth, + HttpOnly: true, + SameSite: http.SameSiteDefaultMode, + } + http.SetCookie(w, &cook) - redirect := string(ctx.FormValue("redirect")) + redirect := r.FormValue("redirect") if redirect == "" { - ctx.Redirect("/", 307) + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } else { - ctx.Redirect(redirect, 307) + http.Redirect(w, r, redirect, http.StatusTemporaryRedirect) } return nil } -func Index(ctx *fasthttp.RequestCtx) { - ctx.Redirect("/login", 307) +func Index(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) } -func (v *AuthView) InitialRegisterView(ctx *fasthttp.RequestCtx) error { - templates.WritePageTemplate(ctx, &templates.RegisterPage{}) +func (v *AuthView) InitialRegisterView(w http.ResponseWriter, r *http.Request) error { + templates.WritePageTemplate(w, &templates.RegisterPage{}) return nil } -func (v *AuthView) InitialRegister(ctx *fasthttp.RequestCtx) error { - username := ctx.FormValue("username") - password := ctx.FormValue("password") - path := ctx.FormValue("path") +func (v *AuthView) InitialRegister(w http.ResponseWriter, r *http.Request) error { + var ( + username = []byte(r.FormValue("username")) + password = []byte(r.FormValue("password")) + path = []byte(r.FormValue("path")) + ) - err := v.userController.InitialRegister(ctx, username, password, path) + err := v.userController.InitialRegister(r.Context(), username, password, path) if err != nil { return err } - ctx.Redirect("/login", 307) + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) return nil } diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go index 6a01117..d49ad4f 100644 --- a/pkg/view/filesystem.go +++ b/pkg/view/filesystem.go @@ -1,7 +1,7 @@ package view import ( - "github.com/valyala/fasthttp" + "net/http" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" @@ -31,21 +31,23 @@ func NewFileSystemView( } } -func (self *FileSystemView) Index(ctx *fasthttp.RequestCtx) error { - pathValue := string(ctx.FormValue("path")) - token := ext.GetTokenFromCtx(ctx) +func (self *FileSystemView) Index(w http.ResponseWriter, r *http.Request) error { + var ( + pathValue = r.FormValue("path") + token = ext.GetTokenFromCtx(w, r) + ) - page, err := self.fsService.GetPage(ctx, token.UserID, pathValue) + page, err := self.fsService.GetPage(r.Context(), token.UserID, pathValue) if err != nil { return err } - settings, err := self.settings.Load(ctx) + settings, err := self.settings.Load(r.Context()) if err != nil { return err } - templates.WritePageTemplate(ctx, &templates.FilePage{ + templates.WritePageTemplate(w, &templates.FilePage{ Page: page, ShowMode: settings.ShowMode, ShowOwner: settings.ShowOwner, diff --git a/pkg/view/media.go b/pkg/view/media.go index 6e34fd6..c7d84ec 100644 --- a/pkg/view/media.go +++ b/pkg/view/media.go @@ -1,10 +1,9 @@ package view import ( + "net/http" "strconv" - "github.com/valyala/fasthttp" - "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" "git.sr.ht/~gabrielgio/img/templates" @@ -18,12 +17,12 @@ type ( } ) -func getPagination(ctx *fasthttp.RequestCtx) *repository.Pagination { +func getPagination(w http.ResponseWriter, r *http.Request) *repository.Pagination { var ( size int page int - sizeStr = string(ctx.FormValue("size")) - pageStr = string(ctx.FormValue("page")) + sizeStr = r.FormValue("size") + pageStr = r.FormValue("page") ) if sizeStr == "" { @@ -60,22 +59,22 @@ func NewMediaView( } } -func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { - p := getPagination(ctx) - token := ext.GetTokenFromCtx(ctx) +func (self *MediaView) Index(w http.ResponseWriter, r *http.Request) error { + p := getPagination(w, r) + token := ext.GetTokenFromCtx(w, r) - userPath, err := self.userRepository.GetPathFromUserID(ctx, token.UserID) + userPath, err := self.userRepository.GetPathFromUserID(r.Context(), token.UserID) if err != nil { return err } p.Path = userPath - medias, err := self.mediaRepository.List(ctx, p) + medias, err := self.mediaRepository.List(r.Context(), p) if err != nil { return err } - settings, err := self.settingsRepository.Load(ctx) + settings, err := self.settingsRepository.Load(r.Context()) if err != nil { return err } @@ -89,43 +88,43 @@ func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { Settings: settings, } - templates.WritePageTemplate(ctx, page) + templates.WritePageTemplate(w, page) return nil } -func (self *MediaView) GetImage(ctx *fasthttp.RequestCtx) error { - pathHash := string(ctx.FormValue("path_hash")) +func (self *MediaView) GetImage(w http.ResponseWriter, r *http.Request) error { + pathHash := r.FormValue("path_hash") - media, err := self.mediaRepository.Get(ctx, pathHash) + media, err := self.mediaRepository.Get(r.Context(), pathHash) if err != nil { return err } - ctx.Response.Header.SetContentType(media.MIMEType) - fasthttp.ServeFileUncompressed(ctx, media.Path) + w.Header().Set("Content-Type", media.MIMEType) + http.ServeFile(w, r, media.Path) return nil } -func (self *MediaView) GetThumbnail(ctx *fasthttp.RequestCtx) error { - pathHash := string(ctx.FormValue("path_hash")) +func (self *MediaView) GetThumbnail(w http.ResponseWriter, r *http.Request) error { + pathHash := r.FormValue("path_hash") - path, err := self.mediaRepository.GetThumbnailPath(ctx, pathHash) + path, err := self.mediaRepository.GetThumbnailPath(r.Context(), pathHash) if err != nil { - ctx.Redirect("/media/image?path_hash="+pathHash, 307) + http.Redirect(w, r, "/media/image?path_hash="+pathHash, http.StatusTemporaryRedirect) // nolint: nilerr return nil } - ctx.Request.Header.SetContentType("image/jpeg") - fasthttp.ServeFileUncompressed(ctx, path) + w.Header().Set("Content-Type", "image/jpeg") + http.ServeFile(w, r, path) return nil } func (self *MediaView) SetMyselfIn(r *ext.Router) { - r.GET("/media", self.Index) - r.POST("/media", self.Index) + r.GET("/media/", self.Index) + r.POST("/media/", self.Index) - r.GET("/media/image", self.GetImage) - r.GET("/media/thumbnail", self.GetThumbnail) + r.GET("/media/image/", self.GetImage) + r.GET("/media/thumbnail/", self.GetThumbnail) } diff --git a/pkg/view/settings.go b/pkg/view/settings.go index 5131362..bf2dca6 100644 --- a/pkg/view/settings.go +++ b/pkg/view/settings.go @@ -1,10 +1,9 @@ package view import ( + "net/http" "strconv" - "github.com/valyala/fasthttp" - "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" "git.sr.ht/~gabrielgio/img/pkg/service" @@ -29,18 +28,18 @@ func NewSettingsView( } } -func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { - s, err := self.settingsRepository.Load(ctx) +func (self *SettingsView) Index(w http.ResponseWriter, r *http.Request) error { + s, err := self.settingsRepository.Load(r.Context()) if err != nil { return err } - users, err := self.userController.List(ctx) + users, err := self.userController.List(r.Context()) if err != nil { return err } - templates.WritePageTemplate(ctx, &templates.SettingsPage{ + templates.WritePageTemplate(w, &templates.SettingsPage{ Settings: s, Users: users, }) @@ -48,22 +47,22 @@ func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { return nil } -func (self *SettingsView) User(ctx *fasthttp.RequestCtx) error { - id := string(ctx.FormValue("userId")) +func (self *SettingsView) User(w http.ResponseWriter, r *http.Request) error { + id := r.FormValue("userId") idValue, err := ParseUint(id) if err != nil { return err } if idValue == nil { - templates.WritePageTemplate(ctx, &templates.UserPage{}) + templates.WritePageTemplate(w, &templates.UserPage{}) } else { - user, err := self.userController.Get(ctx, *idValue) + user, err := self.userController.Get(r.Context(), *idValue) if err != nil { return err } - templates.WritePageTemplate(ctx, &templates.UserPage{ + templates.WritePageTemplate(w, &templates.UserPage{ ID: idValue, Username: user.Username, Path: user.Path, @@ -74,13 +73,13 @@ func (self *SettingsView) User(ctx *fasthttp.RequestCtx) error { return nil } -func (self *SettingsView) UpsertUser(ctx *fasthttp.RequestCtx) error { +func (self *SettingsView) UpsertUser(w http.ResponseWriter, r *http.Request) error { var ( - username = string(ctx.FormValue("username")) - password = ctx.FormValue("password") - path = string(ctx.FormValue("path")) - isAdmin = string(ctx.FormValue("isAdmin")) == "on" - id = string(ctx.FormValue("userId")) + username = r.FormValue("username") + password = []byte(r.FormValue("password")) + path = r.FormValue("path") + isAdmin = r.FormValue("isAdmin") == "on" + id = r.FormValue("userId") ) idValue, err := ParseUint(id) @@ -88,18 +87,18 @@ func (self *SettingsView) UpsertUser(ctx *fasthttp.RequestCtx) error { return err } - err = self.userController.Upsert(ctx, idValue, username, "", password, isAdmin, path) + err = self.userController.Upsert(r.Context(), idValue, username, "", password, isAdmin, path) if err != nil { return err } - ctx.Redirect("/settings", 307) + http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect) return nil } -func (self *SettingsView) Delete(ctx *fasthttp.RequestCtx) error { +func (self *SettingsView) Delete(w http.ResponseWriter, r *http.Request) error { var ( - id = string(ctx.FormValue("userId")) + id = r.FormValue("userId") ) idValue, err := ParseUint(id) @@ -108,24 +107,24 @@ func (self *SettingsView) Delete(ctx *fasthttp.RequestCtx) error { } if idValue != nil { - err = self.userController.Delete(ctx, *idValue) + err = self.userController.Delete(r.Context(), *idValue) if err != nil { return err } } - ctx.Redirect("/settings", 307) + http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect) return nil } -func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { +func (self *SettingsView) Save(w http.ResponseWriter, r *http.Request) error { var ( - showMode = string(ctx.FormValue("showMode")) == "on" - showOwner = string(ctx.FormValue("showOwner")) == "on" - preloadVideoMetadata = string(ctx.FormValue("preloadVideoMetadata")) == "on" + showMode = r.FormValue("showMode") == "on" + showOwner = r.FormValue("showOwner") == "on" + preloadVideoMetadata = r.FormValue("preloadVideoMetadata") == "on" ) - err := self.settingsRepository.Save(ctx, &repository.Settings{ + err := self.settingsRepository.Save(r.Context(), &repository.Settings{ ShowMode: showMode, ShowOwner: showOwner, PreloadVideoMetadata: preloadVideoMetadata, @@ -134,7 +133,7 @@ func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { return err } - return self.Index(ctx) + return self.Index(w, r) } func (self *SettingsView) SetMyselfIn(r *ext.Router) { -- cgit v1.2.3