aboutsummaryrefslogtreecommitdiff
path: root/cmd/app/serve
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-10-12 02:52:22 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-10-12 02:53:52 +0300
commitd05ea66f4bbcf0cc5c8908f3435c68de1b070fa1 (patch)
tree7c7a769206646f2b81a0eda0680f0be5033a4197 /cmd/app/serve
Начальная версияv0.0.1
Diffstat (limited to 'cmd/app/serve')
-rw-r--r--cmd/app/serve/serve.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/cmd/app/serve/serve.go b/cmd/app/serve/serve.go
new file mode 100644
index 0000000..fabdb7c
--- /dev/null
+++ b/cmd/app/serve/serve.go
@@ -0,0 +1,56 @@
+package serve
+
+import (
+ echosession "github.com/labstack/echo-contrib/session"
+ "github.com/labstack/echo/v4"
+ echomiddleware "github.com/labstack/echo/v4/middleware"
+ "github.com/uptrace/bun/extra/bundebug"
+ "github.com/urfave/cli/v2"
+ "go.neonxp.ru/framework/pkg/config"
+ "go.neonxp.ru/framework/pkg/db"
+ "go.neonxp.ru/framework/pkg/handler"
+ "go.neonxp.ru/framework/pkg/handler/user"
+ "go.neonxp.ru/framework/pkg/middleware"
+ "go.neonxp.ru/framework/pkg/middleware/session"
+ userservice "go.neonxp.ru/framework/pkg/service/user"
+ "go.neonxp.ru/framework/static"
+)
+
+func Run(c *cli.Context) error {
+ configFile := c.String("config")
+
+ cfg, err := config.New(configFile)
+ if err != nil {
+ return err
+ }
+
+ dbClient := db.New(cfg.DB)
+ dbClient.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(cfg.Debug)))
+
+ store, err := session.New(dbClient, cfg.Keypairs)
+ if err != nil {
+ return err
+ }
+
+ e := echo.New()
+ e.HTTPErrorHandler = handler.ErrorHandler
+ e.Use(
+ echomiddleware.Recover(),
+ echomiddleware.Logger(),
+ echomiddleware.RemoveTrailingSlash(),
+ echosession.Middleware(store),
+ middleware.Context("config", cfg),
+ middleware.User(),
+ )
+
+ userService := userservice.NewService(dbClient)
+
+ router := handler.Router{
+ "/user": user.NewHandler(userService),
+ }
+ router.Register(e)
+
+ e.StaticFS("/", static.FS)
+
+ return e.Start(cfg.Listen)
+}