Refactor
This commit is contained in:
parent
e426559cec
commit
ddaf0dcbfc
6 changed files with 42 additions and 40 deletions
|
|
@ -3,10 +3,10 @@ package main
|
|||
import (
|
||||
"cgnolink"
|
||||
"cgnolink/database"
|
||||
appserver "cgnolink/server"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -17,12 +17,11 @@ func main() {
|
|||
pool := database.Pool(conf)
|
||||
database.Migrate(pool)
|
||||
|
||||
server := cgnolink.NewServer(conf, pool)
|
||||
server := appserver.NewServer(conf, pool)
|
||||
|
||||
server.Logger.Fatal(server.Start(":8080"))
|
||||
}
|
||||
|
||||
func configureLogger() {
|
||||
zerolog.TimeFieldFormat = time.RFC3339Nano
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,20 +9,20 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
func redirectHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func redirectHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
link, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
return ctx.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
}
|
||||
|
||||
func creationHandler(c echo.Context, serv *PgService) error {
|
||||
func creationHandler(ctx echo.Context, serv Service) error {
|
||||
var model CreationModel
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
||||
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
}
|
||||
|
||||
|
|
@ -35,23 +35,23 @@ func creationHandler(c echo.Context, serv *PgService) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusCreated)
|
||||
return ctx.NoContent(http.StatusCreated)
|
||||
}
|
||||
|
||||
func retrievalByIdHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func retrievalByIdHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
l, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
return ctx.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
}
|
||||
|
||||
func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
||||
func allRetrievalHandler(ctx echo.Context, serv Service) error {
|
||||
limit := 20
|
||||
if v := c.QueryParam("limit"); v != "" {
|
||||
if v := ctx.QueryParam("limit"); v != "" {
|
||||
num, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid limit value.")
|
||||
|
|
@ -61,7 +61,7 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
|||
}
|
||||
|
||||
offset := 0
|
||||
if v := c.QueryParam("offset"); v != "" {
|
||||
if v := ctx.QueryParam("offset"); v != "" {
|
||||
num, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid offset value.")
|
||||
|
|
@ -80,14 +80,14 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
|||
models[i] = MapEntityToModel(v)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models)
|
||||
return ctx.JSON(http.StatusOK, models)
|
||||
}
|
||||
|
||||
func updateHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func updateHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
var model UpdateModel
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
||||
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
}
|
||||
|
||||
|
|
@ -118,46 +118,46 @@ func updateHandler(c echo.Context, serv *PgService) error {
|
|||
}
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func removalHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func removalHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
if err := serv.DeleteById(linkId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
return ctx.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
||||
serv := NewPgService(PgRepository{pool: pool})
|
||||
func AddHandlers(server *echo.Echo, pool *pgxpool.Pool) {
|
||||
serv := NewService(NewRepository(pool))
|
||||
|
||||
linksGroup := s.Group("/links")
|
||||
linksGroup := server.Group("/links")
|
||||
exactLinkGroup := linksGroup.Group("/:id")
|
||||
|
||||
linksGroup.POST("", func(ctx echo.Context) error {
|
||||
return creationHandler(ctx, &serv)
|
||||
return creationHandler(ctx, serv)
|
||||
})
|
||||
|
||||
linksGroup.GET("", func(ctx echo.Context) error {
|
||||
return allRetrievalHandler(ctx, &serv)
|
||||
return allRetrievalHandler(ctx, serv)
|
||||
})
|
||||
|
||||
exactLinkGroup.GET("", func(ctx echo.Context) error {
|
||||
return retrievalByIdHandler(ctx, &serv)
|
||||
return retrievalByIdHandler(ctx, serv)
|
||||
})
|
||||
|
||||
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
||||
return updateHandler(ctx, &serv)
|
||||
return updateHandler(ctx, serv)
|
||||
})
|
||||
|
||||
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
|
||||
return removalHandler(ctx, &serv)
|
||||
return removalHandler(ctx, serv)
|
||||
})
|
||||
|
||||
s.GET("/:id", func(ctx echo.Context) error {
|
||||
return redirectHandler(ctx, &serv)
|
||||
server.GET("/:id", func(ctx echo.Context) error {
|
||||
return redirectHandler(ctx, serv)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@ type PgRepository struct {
|
|||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewRepository(pool *pgxpool.Pool) Repository {
|
||||
return &PgRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *PgRepository) Save(link *Link) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
|
|
|||
|
|
@ -14,12 +14,12 @@ type Service interface {
|
|||
}
|
||||
|
||||
type PgService struct {
|
||||
rep PgRepository
|
||||
rep Repository
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewPgService(rep PgRepository) PgService {
|
||||
return PgService{
|
||||
func NewService(rep Repository) Service {
|
||||
return &PgService{
|
||||
rep: rep,
|
||||
cache: cache.New(cache.NoExpiration, 0),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package middleware
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
package cgnolink
|
||||
package server
|
||||
|
||||
import (
|
||||
apperrors "cgnolink/errors"
|
||||
"cgnolink/link"
|
||||
appmiddleware "cgnolink/middleware"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
|
@ -27,7 +26,7 @@ func NewServer(conf *koanf.Koanf, pool *pgxpool.Pool) *echo.Echo {
|
|||
func addMiddleware(s *echo.Echo, conf *koanf.Koanf) {
|
||||
s.Use(middleware.CORS())
|
||||
|
||||
s.Use(appmiddleware.Logger())
|
||||
s.Use(Logger())
|
||||
|
||||
s.Use(middleware.JWTWithConfig(middleware.JWTConfig{
|
||||
Skipper: func(ctx echo.Context) bool {
|
||||
Loading…
Add table
Add a link
Reference in a new issue