From 2f45c415e0a2a674742447e55f9b46484bcbd62a Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 15 Feb 2022 12:37:23 +0000 Subject: [PATCH] Move init of StaticFS and rename some vars --- cmd/budgeteer/main.go | 10 ++++++++++ http/account_test.go | 16 ++++++++-------- http/http.go | 7 ------- http/session.go | 4 ++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cmd/budgeteer/main.go b/cmd/budgeteer/main.go index cb57d20..ea6a673 100644 --- a/cmd/budgeteer/main.go +++ b/cmd/budgeteer/main.go @@ -1,13 +1,17 @@ package main import ( + "io/fs" "log" + netHttp "net/http" + "git.javil.eu/jacob1123/budgeteer/bcrypt" "git.javil.eu/jacob1123/budgeteer/config" "git.javil.eu/jacob1123/budgeteer/http" "git.javil.eu/jacob1123/budgeteer/jwt" "git.javil.eu/jacob1123/budgeteer/postgres" + "git.javil.eu/jacob1123/budgeteer/web" ) func main() { @@ -21,10 +25,16 @@ func main() { log.Fatalf("Failed connecting to DB: %v", err) } + static, err := fs.Sub(web.Static, "dist") + if err != nil { + panic("couldn't open static files") + } + h := &http.Handler{ Service: q, TokenVerifier: &jwt.TokenVerifier{}, CredentialsVerifier: &bcrypt.Verifier{}, + StaticFS: netHttp.FS(static), } h.Serve() diff --git a/http/account_test.go b/http/account_test.go index df8a430..adbb97b 100644 --- a/http/account_test.go +++ b/http/account_test.go @@ -32,8 +32,8 @@ func TestListTimezonesHandler(t *testing.T) { CredentialsVerifier: &bcrypt.Verifier{}, } - rr := httptest.NewRecorder() - c, engine := gin.CreateTestContext(rr) + recorder := httptest.NewRecorder() + c, engine := gin.CreateTestContext(recorder) h.LoadRoutes(engine) t.Run("RegisterUser", func(t *testing.T) { @@ -45,12 +45,12 @@ func TestListTimezonesHandler(t *testing.T) { h.registerPost(c) - if rr.Code != http.StatusOK { - t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK) + if recorder.Code != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", recorder.Code, http.StatusOK) } var response LoginResponse - err = json.NewDecoder(rr.Body).Decode(&response) + err = json.NewDecoder(recorder.Body).Decode(&response) if err != nil { t.Error(err.Error()) t.Error("Error registering") @@ -62,12 +62,12 @@ func TestListTimezonesHandler(t *testing.T) { t.Run("GetTransactions", func(t *testing.T) { c.Request, err = http.NewRequest(http.MethodGet, "/account/accountid/transactions", nil) - if rr.Code != http.StatusOK { - t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK) + if recorder.Code != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", recorder.Code, http.StatusOK) } var response TransactionsResponse - err = json.NewDecoder(rr.Body).Decode(&response) + err = json.NewDecoder(recorder.Body).Decode(&response) if err != nil { t.Error(err.Error()) t.Error("Error retreiving list of transactions.") diff --git a/http/http.go b/http/http.go index ccb53a3..2881030 100644 --- a/http/http.go +++ b/http/http.go @@ -11,7 +11,6 @@ import ( "git.javil.eu/jacob1123/budgeteer" "git.javil.eu/jacob1123/budgeteer/bcrypt" "git.javil.eu/jacob1123/budgeteer/postgres" - "git.javil.eu/jacob1123/budgeteer/web" "github.com/gin-gonic/gin" ) @@ -36,12 +35,6 @@ func (h *Handler) Serve() { // LoadRoutes initializes all the routes func (h *Handler) LoadRoutes(router *gin.Engine) { - static, err := fs.Sub(web.Static, "dist") - if err != nil { - panic("couldn't open static files") - } - h.StaticFS = http.FS(static) - router.Use(enableCachingForStaticFiles()) router.NoRoute(h.ServeStatic) diff --git a/http/session.go b/http/session.go index 9bd0f75..421ea4c 100644 --- a/http/session.go +++ b/http/session.go @@ -135,7 +135,7 @@ func (h *Handler) registerPost(c *gin.Context) { c.AbortWithError(http.StatusInternalServerError, err) } - t, err := h.TokenVerifier.CreateToken(&user) + token, err := h.TokenVerifier.CreateToken(&user) if err != nil { c.AbortWithError(http.StatusUnauthorized, err) } @@ -147,7 +147,7 @@ func (h *Handler) registerPost(c *gin.Context) { return } - c.JSON(http.StatusOK, LoginResponse{t, user, budgets}) + c.JSON(http.StatusOK, LoginResponse{token, user, budgets}) } func (h *Handler) UpdateLastLogin(userID uuid.UUID) {