2023-05-20 20:15:57 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-03-22 23:11:14 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-05-01 22:48:11 +02:00
|
|
|
"github.com/labstack/echo-contrib/session"
|
2023-03-22 23:11:14 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Admin() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid credentials")
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
|
|
|
|
func Index() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2023-05-16 13:49:02 +02:00
|
|
|
addHeaders(c)
|
2023-05-06 00:09:13 +02:00
|
|
|
|
2023-05-16 13:49:02 +02:00
|
|
|
if sess, _ := session.Get(setting.SessionCookieName(), c); sess != nil {
|
|
|
|
username := sess.Values["username"]
|
|
|
|
if username != nil {
|
|
|
|
return c.Redirect(http.StatusFound, "/home")
|
|
|
|
}
|
2023-05-06 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
csrf := c.Get("csrf").(string)
|
|
|
|
|
2023-05-11 05:01:19 +02:00
|
|
|
err := c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index.tmpl",
|
2023-04-13 00:07:08 +02:00
|
|
|
page{
|
2023-05-03 02:18:29 +02:00
|
|
|
AppName: setting.AppName(),
|
2023-04-13 00:07:08 +02:00
|
|
|
AppVer: appver,
|
|
|
|
Title: "Welcome!",
|
|
|
|
CSRF: csrf,
|
2023-05-03 02:18:29 +02:00
|
|
|
DevelMode: setting.IsDevel(),
|
2023-04-13 00:07:08 +02:00
|
|
|
Current: "home",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-05-11 05:01:19 +02:00
|
|
|
c.Logger().Errorf("error: %q", err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
2023-04-13 00:07:08 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 05:01:19 +02:00
|
|
|
return err
|
2023-04-13 00:07:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:33:38 +02:00
|
|
|
func Healthz() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2023-06-04 11:32:57 +02:00
|
|
|
return c.JSON(http.StatusOK, struct{ Status string }{Status: "OK"})
|
2023-05-13 22:33:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 23:50:40 +02:00
|
|
|
func addHeaders(c echo.Context) {
|
|
|
|
c.Response().Writer.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// experimental global redirect handler?
|
|
|
|
// http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// loc, err := url.QueryUnescape(r.URL.Query().Get("loc"))
|
|
|
|
// if err != nil {
|
|
|
|
// http.Error(w, fmt.Sprintf("invalid redirect: %q", r.URL.Query().Get("loc")), http.StatusBadRequest)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// http.Redirect(w, r, loc, http.StatusFound)
|
|
|
|
// })
|