aboutsummaryrefslogblamecommitdiff
path: root/cmd/app/serve/serve.go
blob: fabdb7cbd7050b957a44da2020e8fecfcd9e9051 (plain) (tree)























































                                                                                     
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)
}