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