60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
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"
|
|
"sh.org.ru/pkg/config"
|
|
"sh.org.ru/pkg/db"
|
|
"sh.org.ru/pkg/handler"
|
|
"sh.org.ru/pkg/handler/add"
|
|
"sh.org.ru/pkg/handler/admin"
|
|
"sh.org.ru/pkg/handler/captcha"
|
|
"sh.org.ru/pkg/handler/feed"
|
|
"sh.org.ru/pkg/handler/quote"
|
|
"sh.org.ru/pkg/handler/rate"
|
|
"sh.org.ru/pkg/middleware"
|
|
"sh.org.ru/pkg/middleware/session"
|
|
"sh.org.ru/static"
|
|
)
|
|
|
|
func Run(c *cli.Context) error {
|
|
configFile := c.String("config")
|
|
cfg, err := config.New(configFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
db := db.New(cfg.DB)
|
|
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(cfg.Debug)))
|
|
|
|
store, err := session.New(db, 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),
|
|
)
|
|
|
|
router := handler.Router{
|
|
"/": quote.NewHandler(db),
|
|
"/add": add.NewHandler(db),
|
|
"/admin": admin.NewHandler(db, cfg),
|
|
"/captcha": captcha.NewHandler(),
|
|
"/rate": rate.NewHandler(db),
|
|
"/feed": feed.NewHandler(db, cfg),
|
|
}
|
|
router.Register(e)
|
|
|
|
e.StaticFS("/", static.FS)
|
|
|
|
return e.Start(cfg.Listen)
|
|
}
|