aboutsummaryrefslogtreecommitdiff
path: root/cmd/app/serve/serve.go
blob: fabdb7cbd7050b957a44da2020e8fecfcd9e9051 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
}