aboutsummaryrefslogtreecommitdiff
path: root/cmd/app/serve/serve.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/app/serve/serve.go')
-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)
+}