2024-10-06 17:04:37 +03:00
|
|
|
package serve
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/ssoda/captcha"
|
|
|
|
"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/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)))
|
|
|
|
|
|
|
|
h := handler.Handler{DB: db}
|
|
|
|
|
|
|
|
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(
|
|
|
|
middleware.Recover(),
|
|
|
|
middleware.Logger(),
|
|
|
|
middleware.RemoveTrailingSlash(),
|
|
|
|
)
|
2024-10-06 17:04:37 +03:00
|
|
|
|
|
|
|
e.GET("/", h.Index)
|
|
|
|
e.GET("/quote/:id", h.Quote)
|
2024-10-06 17:47:06 +03:00
|
|
|
e.GET("/random", h.Random)
|
2024-10-06 17:04:37 +03:00
|
|
|
e.GET("/add", h.AddQuote)
|
|
|
|
e.POST("/add", h.AddQuotePost)
|
|
|
|
e.GET("/add/success", h.AddQuoteSuccess)
|
|
|
|
e.GET("/captcha/*", echo.WrapHandler(captcha.Server(400, 65)))
|
|
|
|
|
2024-10-07 03:45:49 +03:00
|
|
|
adminMW := middleware.BasicAuth(func(u, p string, ctx echo.Context) (bool, error) {
|
|
|
|
return cfg.Admins[u] == p, nil
|
|
|
|
})
|
|
|
|
|
2024-10-06 17:04:37 +03:00
|
|
|
func(g *echo.Group) {
|
|
|
|
g.GET("/", h.Admin)
|
|
|
|
g.POST("/action", h.AdminAction)
|
2024-10-07 01:06:55 +03:00
|
|
|
g.GET("/export", h.AdminExport)
|
2024-10-07 03:45:49 +03:00
|
|
|
}(e.Group("/admin", adminMW))
|
2024-10-06 17:04:37 +03:00
|
|
|
|
|
|
|
e.StaticFS("/", static.FS)
|
|
|
|
|
|
|
|
return e.Start(cfg.Listen)
|
|
|
|
}
|