Skip to content

Commit

Permalink
Did smth, I forgot)
Browse files Browse the repository at this point in the history
  • Loading branch information
Imomali1 committed Feb 7, 2023
1 parent aeac5be commit e8aa568
Show file tree
Hide file tree
Showing 1,127 changed files with 670 additions and 474,513 deletions.
9 changes: 9 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package api

import "github.com/Imomali1/todo-app/internal/handlers"

type Handler struct {
AuthHandler handlers.AuthHandler
ListHandler handlers.ListHandler
ItemHandler handlers.ItemHandler
}
95 changes: 0 additions & 95 deletions api/router.go

This file was deleted.

71 changes: 71 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package api

import (
"github.com/Imomali1/todo-app/config"
"github.com/Imomali1/todo-app/core"
"github.com/Imomali1/todo-app/internal/handlers"
"github.com/Imomali1/todo-app/pkg/cache"
"github.com/Imomali1/todo-app/pkg/middleware"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"os"
)

type Options struct {
Conf config.Configs
Cache cache.ICache
DB core.PostgresDB
}

func InitRoutes(option *Options) *gin.Engine {
gin.DefaultWriter = os.Stdout
gin.DefaultErrorWriter = os.Stderr
gin.SetMode(gin.ReleaseMode)

router := gin.New()

router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(cors.Default())

h := &Handler{
AuthHandler: *handlers.NewAuthHandler(&handlers.AuthHandlerConfig{
Conf: option.Conf,
Cache: option.Cache,
}),
ListHandler: *handlers.NewListHandler(&handlers.ListHandlerConfig{
Conf: option.Conf,
Cache: option.Cache,
}),
ItemHandler: *handlers.NewItemHandler(&handlers.ItemHandlerConfig{
Conf: option.Conf,
Cache: option.Cache,
}),
}

// No auth required endpoints
api := router.Group("/api")
{
api.POST("/auth/register", h.AuthHandler.Register)
api.POST("/auth/login", h.AuthHandler.Login)
}

// authorized endpoints
authorized := router.Group("/api/v1")
authorized.Use(middleware.IsAuthorized(option.Cache))
{
authorized.POST("/user/lists", h.ListHandler.CreateList)
authorized.GET("/user/lists", h.ListHandler.GetLists)
authorized.GET("/user/lists/:id", h.ListHandler.GetListById)
authorized.PUT("/user/lists/:id", h.ListHandler.UpdateList)
authorized.DELETE("/user/lists/:id", h.ListHandler.DeleteList)

authorized.POST("/user/lists/:id/items", h.ItemHandler.CreateItem)
authorized.GET("/user/lists/:id/items", h.ItemHandler.GetItems)
authorized.GET("/user/lists/:id/items/:item_id", h.ItemHandler.GetItem)
authorized.PUT("/user/lists/:id/items/:item_id", h.ItemHandler.UpdateItem)
authorized.DELETE("/user/lists/:id/items/:item_id", h.ItemHandler.DeleteItem)
}

return router
}
50 changes: 50 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"context"
"github.com/Imomali1/todo-app/api"
"github.com/Imomali1/todo-app/config"
redis "github.com/Imomali1/todo-app/pkg/cache"
"github.com/Imomali1/todo-app/pkg/server"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
"time"
)

func Run() {
log.SetFormatter(new(log.JSONFormatter))

conf := config.Load()

cache := redis.NewClient(conf)

opt := &api.Options{
Conf: conf,
Cache: *cache,
}

srv := server.NewServer(conf, api.InitRoutes(opt))

go func() {
if err := srv.Run(); err != nil {
log.Errorf("Cannot run server: %s\n", err.Error())
}
}()

log.Info("Server started...")

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit

const timeout = 5 * time.Second

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

if err := srv.Stop(ctx); err != nil {
log.Error("failed to stop server: " + err.Error())
}
}
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type PostgresConfigs struct {
DB string
}

func Load() *Configs {
cfg := &Configs{}
func Load() Configs {
cfg := Configs{}

cfg.HTTP.Host = cast.ToString(getOrReturnDefault("HTTP_HOST", "localhost"))
cfg.HTTP.Port = cast.ToInt(getOrReturnDefault("HTTP_PORT", 8080))
Expand Down
6 changes: 3 additions & 3 deletions core/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/Imomali1/todo-app/config"
)

type PostgresClient struct {
type PostgresDB struct {
DB *sql.DB
}

func InitDB(conf config.Configs) (*PostgresClient, error) {
func InitDB(conf config.Configs) (*PostgresDB, error) {
psqlConn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
conf.Postgres.Host, conf.Postgres.Port, conf.Postgres.User, conf.Postgres.Password, conf.Postgres.DB)

Expand All @@ -28,7 +28,7 @@ func InitDB(conf config.Configs) (*PostgresClient, error) {
return nil, err
}

return &PostgresClient{
return &PostgresDB{
DB: db,
}, nil
}
44 changes: 0 additions & 44 deletions internal/handler/item.go

This file was deleted.

51 changes: 0 additions & 51 deletions internal/handler/list.go

This file was deleted.

36 changes: 0 additions & 36 deletions internal/handler/user.go

This file was deleted.

Loading

0 comments on commit e8aa568

Please sign in to comment.