2022-03-29 20:53:43 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/api"
|
2022-09-09 20:00:04 +08:00
|
|
|
"github.com/usememos/memos/common"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
2022-03-29 20:53:43 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerSystemRoutes(g *echo.Group) {
|
|
|
|
g.GET("/ping", func(c echo.Context) error {
|
2022-06-22 19:16:31 +08:00
|
|
|
data := s.Profile
|
|
|
|
|
2022-03-29 20:53:43 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-06-22 19:16:31 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(data)); err != nil {
|
2022-03-29 20:53:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose system profile").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-15 10:57:54 +08:00
|
|
|
|
|
|
|
g.GET("/status", func(c echo.Context) error {
|
2022-08-07 09:23:46 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-07-08 22:16:18 +08:00
|
|
|
hostUserType := api.Host
|
|
|
|
hostUserFind := api.UserFind{
|
|
|
|
Role: &hostUserType,
|
2022-05-15 10:57:54 +08:00
|
|
|
}
|
2022-08-07 09:23:46 +08:00
|
|
|
hostUser, err := s.Store.FindUser(ctx, &hostUserFind)
|
2022-09-09 20:00:04 +08:00
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2022-07-08 22:16:18 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find host user").SetInternal(err)
|
2022-05-15 10:57:54 +08:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:16:18 +08:00
|
|
|
if hostUser != nil {
|
2022-06-27 19:11:56 +08:00
|
|
|
// data desensitize
|
2022-07-08 22:16:18 +08:00
|
|
|
hostUser.OpenID = ""
|
2022-06-27 19:11:56 +08:00
|
|
|
}
|
2022-06-22 19:16:31 +08:00
|
|
|
|
2022-05-15 10:57:54 +08:00
|
|
|
systemStatus := api.SystemStatus{
|
2022-08-07 09:23:46 +08:00
|
|
|
Host: hostUser,
|
2022-05-17 21:21:13 +08:00
|
|
|
Profile: s.Profile,
|
2022-05-15 10:57:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemStatus)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system status response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-03-29 20:53:43 +08:00
|
|
|
}
|