Мелкие правки админки
This commit is contained in:
parent
420e049415
commit
ffcc9eeb17
4 changed files with 63 additions and 12 deletions
|
@ -26,7 +26,6 @@ func Run(c *cli.Context) error {
|
|||
e := echo.New()
|
||||
|
||||
e.HTTPErrorHandler = handler.ErrorHandler
|
||||
|
||||
e.Use(
|
||||
middleware.Recover(),
|
||||
middleware.Logger(),
|
||||
|
@ -41,13 +40,15 @@ func Run(c *cli.Context) error {
|
|||
e.GET("/add/success", h.AddQuoteSuccess)
|
||||
e.GET("/captcha/*", echo.WrapHandler(captcha.Server(400, 65)))
|
||||
|
||||
adminMW := middleware.BasicAuth(func(u, p string, ctx echo.Context) (bool, error) {
|
||||
return cfg.Admins[u] == p, nil
|
||||
})
|
||||
|
||||
func(g *echo.Group) {
|
||||
g.GET("/", h.Admin)
|
||||
g.POST("/action", h.AdminAction)
|
||||
g.GET("/export", h.AdminExport)
|
||||
}(e.Group("/admin", middleware.BasicAuth(func(u, p string, ctx echo.Context) (bool, error) {
|
||||
return cfg.Admins[u] == p, nil
|
||||
})))
|
||||
}(e.Group("/admin", adminMW))
|
||||
|
||||
e.StaticFS("/", static.FS)
|
||||
|
||||
|
|
|
@ -2,13 +2,46 @@ package handler
|
|||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"sh.org.ru/pkg/tpl"
|
||||
)
|
||||
|
||||
func ErrorHandler(err error, c echo.Context) {
|
||||
if err := tpl.ErrorPage(err.Error()).Render(c.Request().Context(), c.Response()); err != nil {
|
||||
if c.Response().Committed {
|
||||
return
|
||||
}
|
||||
|
||||
he, ok := err.(*echo.HTTPError)
|
||||
if ok {
|
||||
if he.Internal != nil {
|
||||
if herr, ok := he.Internal.(*echo.HTTPError); ok {
|
||||
he = herr
|
||||
}
|
||||
}
|
||||
} else {
|
||||
he = &echo.HTTPError{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: http.StatusText(http.StatusInternalServerError),
|
||||
}
|
||||
}
|
||||
code := he.Code
|
||||
message, ok := he.Message.(string)
|
||||
if !ok {
|
||||
message = "Неизвестная ошибка"
|
||||
}
|
||||
|
||||
// Send response
|
||||
if c.Request().Method == http.MethodHead { // Issue #608
|
||||
err = c.NoContent(he.Code)
|
||||
} else {
|
||||
c.Response().WriteHeader(code)
|
||||
if err := tpl.ErrorPage(code, message).Render(c.Request().Context(), c.Response()); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package tpl
|
||||
|
||||
templ ErrorPage(err string) {
|
||||
import "strconv"
|
||||
|
||||
templ ErrorPage(code int, message string) {
|
||||
@Layout(HeaderParams{}) {
|
||||
<h1>Ошибка!</h1>
|
||||
<p>{ err }</p>
|
||||
<h1>Ошибка { strconv.Itoa(code) }!</h1>
|
||||
<p>{ message }</p>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ package tpl
|
|||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
func ErrorPage(err string) templ.Component {
|
||||
import "strconv"
|
||||
|
||||
func ErrorPage(code int, message string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
|
@ -41,19 +43,32 @@ func ErrorPage(err string) templ.Component {
|
|||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h1>Ошибка!</h1><p>")
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h1>Ошибка ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(err)
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(code))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/error.templ`, Line: 6, Col: 10}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/error.templ`, Line: 7, Col: 39}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("!</h1><p>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var4 string
|
||||
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(message)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/error.templ`, Line: 8, Col: 14}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</p>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
|
|
Loading…
Reference in a new issue