2022-03-29 15:53:43 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/api"
|
2022-09-09 15:00:04 +03:00
|
|
|
"github.com/usememos/memos/common"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
2022-03-29 15:53:43 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerSystemRoutes(g *echo.Group) {
|
|
|
|
g.GET("/ping", func(c echo.Context) error {
|
2022-06-22 14:16:31 +03:00
|
|
|
data := s.Profile
|
|
|
|
|
2022-03-29 15:53:43 +03:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-06-22 14:16:31 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(data)); err != nil {
|
2022-03-29 15:53:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose system profile").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-15 05:57:54 +03:00
|
|
|
|
|
|
|
g.GET("/status", func(c echo.Context) error {
|
2022-08-07 04:23:46 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-07-08 17:16:18 +03:00
|
|
|
hostUserType := api.Host
|
|
|
|
hostUserFind := api.UserFind{
|
|
|
|
Role: &hostUserType,
|
2022-05-15 05:57:54 +03:00
|
|
|
}
|
2022-08-07 04:23:46 +03:00
|
|
|
hostUser, err := s.Store.FindUser(ctx, &hostUserFind)
|
2022-09-09 15:00:04 +03:00
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2022-07-08 17:16:18 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find host user").SetInternal(err)
|
2022-05-15 05:57:54 +03:00
|
|
|
}
|
|
|
|
|
2022-07-08 17:16:18 +03:00
|
|
|
if hostUser != nil {
|
2022-06-27 14:11:56 +03:00
|
|
|
// data desensitize
|
2022-07-08 17:16:18 +03:00
|
|
|
hostUser.OpenID = ""
|
2022-06-27 14:11:56 +03:00
|
|
|
}
|
2022-06-22 14:16:31 +03:00
|
|
|
|
2022-05-15 05:57:54 +03:00
|
|
|
systemStatus := api.SystemStatus{
|
2022-11-14 17:21:19 +03:00
|
|
|
Host: hostUser,
|
|
|
|
Profile: s.Profile,
|
|
|
|
AllowSignUp: false,
|
|
|
|
AdditionalStyle: "",
|
|
|
|
AdditionalScript: "",
|
2022-11-03 16:47:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err)
|
|
|
|
}
|
|
|
|
for _, systemSetting := range systemSettingList {
|
|
|
|
var value interface{}
|
|
|
|
err = json.Unmarshal([]byte(systemSetting.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if systemSetting.Name == api.SystemSettingAllowSignUpName {
|
|
|
|
systemStatus.AllowSignUp = value.(bool)
|
2022-11-11 18:42:44 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingAdditionalStyleName {
|
|
|
|
systemStatus.AdditionalStyle = value.(string)
|
2022-11-14 17:21:19 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingAdditionalScriptName {
|
|
|
|
systemStatus.AdditionalScript = value.(string)
|
2022-11-03 16:47:36 +03:00
|
|
|
}
|
2022-05-15 05:57:54 +03: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-11-03 16:47:36 +03:00
|
|
|
|
|
|
|
g.POST("/system/setting", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.Store.FindUser(ctx, &api.UserFind{
|
|
|
|
ID: &userID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, "Current signin user not found")
|
|
|
|
} else if user.Role != api.Host {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
}
|
|
|
|
|
|
|
|
systemSettingUpsert := &api.SystemSettingUpsert{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(systemSettingUpsert); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post system setting request").SetInternal(err)
|
|
|
|
}
|
|
|
|
if err := systemSettingUpsert.Validate(); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "system setting invalidate").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
systemSetting, err := s.Store.UpsertSystemSetting(ctx, systemSettingUpsert)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert system setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemSetting)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system setting response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/system/setting", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemSettingList)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system setting list response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-03-29 15:53:43 +03:00
|
|
|
}
|