-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1,127 changed files
with
670 additions
and
474,513 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.