Start migration to echo
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type FilterTransactionsRequest struct {
|
||||
@ -17,7 +18,7 @@ type FilterTransactionsRequest struct {
|
||||
ToDate time.Time `json:"to_date"`
|
||||
}
|
||||
|
||||
func (h *Handler) filteredTransactions(c *gin.Context) {
|
||||
func (h *Handler) filteredTransactions(c *echo.Context) {
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
if err != nil {
|
||||
@ -81,27 +82,24 @@ func (h *Handler) problematicTransactions(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, TransactionsResponse{nil, transactions})
|
||||
}
|
||||
|
||||
func (h *Handler) transactionsForAccount(c *gin.Context) {
|
||||
func (h *Handler) transactionsForAccount(c echo.Context) error {
|
||||
accountID := c.Param("accountid")
|
||||
accountUUID, err := uuid.Parse(accountID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
account, err := h.Service.GetAccount(c.Request.Context(), accountUUID)
|
||||
account, err := h.Service.GetAccount(c.Request().Context(), accountUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
|
||||
transactions, err := h.Service.GetTransactionsForAccount(c.Request.Context(), accountUUID)
|
||||
transactions, err := h.Service.GetTransactionsForAccount(c.Request().Context(), accountUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, TransactionsResponse{&account, transactions})
|
||||
return c.JSON(http.StatusOK, TransactionsResponse{&account, transactions})
|
||||
}
|
||||
|
||||
type TransactionsResponse struct {
|
||||
|
@ -11,7 +11,8 @@ import (
|
||||
"git.javil.eu/jacob1123/budgeteer"
|
||||
"git.javil.eu/jacob1123/budgeteer/bcrypt"
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Handler handles incoming requests.
|
||||
@ -24,10 +25,10 @@ type Handler struct {
|
||||
|
||||
// Serve starts the http server.
|
||||
func (h *Handler) Serve() {
|
||||
router := gin.Default()
|
||||
router := echo.New()
|
||||
h.LoadRoutes(router)
|
||||
|
||||
if err := router.Run(":1323"); err != nil {
|
||||
if err := router.Start(":1323"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -41,9 +42,9 @@ type SuccessResponse struct {
|
||||
}
|
||||
|
||||
// LoadRoutes initializes all the routes.
|
||||
func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
func (h *Handler) LoadRoutes(router *echo.Echo) {
|
||||
router.Use(enableCachingForStaticFiles())
|
||||
router.NoRoute(h.ServeStatic)
|
||||
router.Use(h.ServeStatic)
|
||||
|
||||
api := router.Group("/api/v1")
|
||||
|
||||
@ -81,11 +82,14 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeStatic(c *gin.Context) {
|
||||
h.ServeStaticFile(c, c.Request.URL.Path)
|
||||
func (h *Handler) ServeStatic(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
h.ServeStaticFile(c, c.Path())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
|
||||
func (h *Handler) ServeStaticFile(c echo.Context, fullPath string) {
|
||||
file, err := h.StaticFS.Open(fullPath)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
h.ServeStaticFile(c, path.Join("/", "/index.html"))
|
||||
@ -93,13 +97,13 @@ func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
stat, err := file.Stat()
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -109,16 +113,20 @@ func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
|
||||
}
|
||||
|
||||
if file, ok := file.(io.ReadSeeker); ok {
|
||||
http.ServeContent(c.Writer, c.Request, stat.Name(), stat.ModTime(), file)
|
||||
http.ServeContent(c.Response().Writer, c.Request(), stat.Name(), stat.ModTime(), file)
|
||||
} else {
|
||||
panic("File does not implement ReadSeeker")
|
||||
}
|
||||
}
|
||||
|
||||
func enableCachingForStaticFiles() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.RequestURI, "/static/") {
|
||||
c.Header("Cache-Control", "max-age=86400")
|
||||
func enableCachingForStaticFiles() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if strings.HasPrefix(c.Path(), "/static/") {
|
||||
c.Response().Header().Set("Cache-Control", "max-age=86400")
|
||||
}
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -26,8 +27,8 @@ func MustGetToken(c *gin.Context) budgeteer.Token { //nolint:ireturn
|
||||
panic("Token is not a valid Token")
|
||||
}
|
||||
|
||||
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, *ErrorResponse) { //nolint:ireturn
|
||||
tokenString := c.GetHeader(HeaderName)
|
||||
func (h *Handler) verifyLogin(c echo.Context) (budgeteer.Token, *ErrorResponse) { //nolint:ireturn
|
||||
tokenString := c.Request().Header.Get(HeaderName)
|
||||
if len(tokenString) <= len(Bearer) {
|
||||
return nil, &ErrorResponse{"no authorization header supplied"}
|
||||
}
|
||||
@ -41,16 +42,17 @@ func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, *ErrorResponse)
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (h *Handler) verifyLoginWithForbidden(c *gin.Context) {
|
||||
token, err := h.verifyLogin(c)
|
||||
if err != nil {
|
||||
// c.Header("WWW-Authenticate", "Bearer")
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, err)
|
||||
return
|
||||
}
|
||||
func (h *Handler) verifyLoginWithForbidden(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
token, err := h.verifyLogin(c)
|
||||
if err != nil {
|
||||
// c.Header("WWW-Authenticate", "Bearer")
|
||||
return echo.NewHTTPError(http.StatusForbidden, err)
|
||||
}
|
||||
|
||||
c.Set(ParamName, token)
|
||||
c.Next()
|
||||
c.Set(ParamName, token)
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
type loginInformation struct {
|
||||
@ -58,37 +60,35 @@ type loginInformation struct {
|
||||
User string `json:"user"`
|
||||
}
|
||||
|
||||
func (h *Handler) loginPost(c *gin.Context) {
|
||||
func (h *Handler) loginPost(c echo.Context) error {
|
||||
var login loginInformation
|
||||
err := c.BindJSON(&login)
|
||||
err := c.Bind(&login)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := h.Service.GetUserByUsername(c.Request.Context(), login.User)
|
||||
user, err := h.Service.GetUserByUsername(c.Request().Context(), login.User)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
if err = h.CredentialsVerifier.Verify(login.Password, user.Password); err != nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
token, err := h.TokenVerifier.CreateToken(&user)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
return err
|
||||
}
|
||||
|
||||
go h.UpdateLastLogin(user.ID)
|
||||
|
||||
budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID)
|
||||
budgets, err := h.Service.GetBudgetsForUser(c.Request().Context(), user.ID)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, LoginResponse{token, user, budgets})
|
||||
return c.JSON(http.StatusOK, LoginResponse{token, user, budgets})
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
@ -103,29 +103,25 @@ type registerInformation struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (h *Handler) registerPost(c *gin.Context) {
|
||||
func (h *Handler) registerPost(c echo.Context) error {
|
||||
var register registerInformation
|
||||
err := c.BindJSON(®ister)
|
||||
err := c.Bind(®ister)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"error parsing body"})
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusBadRequest, ErrorResponse{"error parsing body"})
|
||||
}
|
||||
|
||||
if register.Email == "" || register.Password == "" || register.Name == "" {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"e-mail, password and name are required"})
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusBadRequest, ErrorResponse{"e-mail, password and name are required"})
|
||||
}
|
||||
|
||||
_, err = h.Service.GetUserByUsername(c.Request.Context(), register.Email)
|
||||
_, err = h.Service.GetUserByUsername(c.Request().Context(), register.Email)
|
||||
if err == nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"email is already taken"})
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusBadRequest, ErrorResponse{"email is already taken"})
|
||||
}
|
||||
|
||||
hash, err := h.CredentialsVerifier.Hash(register.Password)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
createUser := postgres.CreateUserParams{
|
||||
@ -133,24 +129,24 @@ func (h *Handler) registerPost(c *gin.Context) {
|
||||
Password: hash,
|
||||
Email: register.Email,
|
||||
}
|
||||
user, err := h.Service.CreateUser(c.Request.Context(), createUser)
|
||||
user, err := h.Service.CreateUser(c.Request().Context(), createUser)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return err
|
||||
}
|
||||
|
||||
token, err := h.TokenVerifier.CreateToken(&user)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
return err
|
||||
}
|
||||
|
||||
go h.UpdateLastLogin(user.ID)
|
||||
|
||||
budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID)
|
||||
budgets, err := h.Service.GetBudgetsForUser(c.Request().Context(), user.ID)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, LoginResponse{token, user, budgets})
|
||||
return c.JSON(http.StatusOK, LoginResponse{token, user, budgets})
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateLastLogin(userID uuid.UUID) {
|
||||
|
Reference in New Issue
Block a user