2024-10-06 17:04:37 +03:00
|
|
|
package serve
|
|
|
|
|
|
|
|
import (
|
2024-10-08 03:43:08 +03:00
|
|
|
echosession "github.com/labstack/echo-contrib/session"
|
2024-10-06 17:04:37 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-10-08 03:43:08 +03:00
|
|
|
echomiddleware "github.com/labstack/echo/v4/middleware"
|
2024-10-06 17:04:37 +03:00
|
|
|
"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"
|
2024-10-08 03:43:08 +03:00
|
|
|
"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"
|
2024-10-06 17:04:37 +03:00
|
|
|
"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)))
|
|
|
|
|
2024-10-08 03:43:08 +03:00
|
|
|
store, err := session.New(db, cfg.Keypairs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-06 17:04:37 +03:00
|
|
|
|
|
|
|
e := echo.New()
|
2024-10-06 17:47:06 +03:00
|
|
|
e.HTTPErrorHandler = handler.ErrorHandler
|
2024-10-07 01:06:55 +03:00
|
|
|
e.Use(
|
2024-10-08 03:43:08 +03:00
|
|
|
echomiddleware.Recover(),
|
|
|
|
echomiddleware.Logger(),
|
|
|
|
echomiddleware.RemoveTrailingSlash(),
|
|
|
|
echosession.Middleware(store),
|
|
|
|
middleware.Context("config", cfg),
|
2024-10-07 01:06:55 +03:00
|
|
|
)
|
2024-10-06 17:04:37 +03:00
|
|
|
|
2024-10-08 03:43:08 +03:00
|
|
|
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)
|
2024-10-06 17:04:37 +03:00
|
|
|
|
|
|
|
e.StaticFS("/", static.FS)
|
|
|
|
|
|
|
|
return e.Start(cfg.Listen)
|
|
|
|
}
|