2022-03-29 20:53:43 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-03 23:05:42 +08:00
|
|
|
"context"
|
2022-03-29 20:53:43 +08:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-11-18 21:17:52 +08:00
|
|
|
"os"
|
2022-03-29 20:53:43 +08:00
|
|
|
|
2023-01-03 23:05:42 +08:00
|
|
|
"github.com/google/uuid"
|
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-12-25 10:28:51 +08:00
|
|
|
hostUser.Email = ""
|
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-11-14 22:21:19 +08:00
|
|
|
Host: hostUser,
|
2022-11-22 23:45:11 +08:00
|
|
|
Profile: *s.Profile,
|
2022-11-19 17:07:40 +08:00
|
|
|
DBSize: 0,
|
2022-11-14 22:21:19 +08:00
|
|
|
AllowSignUp: false,
|
|
|
|
AdditionalStyle: "",
|
|
|
|
AdditionalScript: "",
|
2022-12-18 21:18:30 +08:00
|
|
|
CustomizedProfile: api.CustomizedProfile{
|
2022-12-22 19:48:44 +08:00
|
|
|
Name: "memos",
|
2022-12-23 00:21:53 +08:00
|
|
|
LogoURL: "",
|
2022-12-22 19:48:44 +08:00
|
|
|
Description: "",
|
|
|
|
Locale: "en",
|
|
|
|
Appearance: "system",
|
|
|
|
ExternalURL: "",
|
2022-12-18 21:18:30 +08:00
|
|
|
},
|
2022-11-03 21:47:36 +08: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 {
|
2023-01-03 23:05:42 +08:00
|
|
|
if systemSetting.Name == api.SystemSettingServerID || systemSetting.Name == api.SystemSettingSecretSessionName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-03 21:47:36 +08:00
|
|
|
var value interface{}
|
2022-12-18 21:18:30 +08:00
|
|
|
err := json.Unmarshal([]byte(systemSetting.Value), &value)
|
2022-11-03 21:47:36 +08:00
|
|
|
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 23:42:44 +08:00
|
|
|
} else if systemSetting.Name == api.SystemSettingAdditionalStyleName {
|
|
|
|
systemStatus.AdditionalStyle = value.(string)
|
2022-11-14 22:21:19 +08:00
|
|
|
} else if systemSetting.Name == api.SystemSettingAdditionalScriptName {
|
|
|
|
systemStatus.AdditionalScript = value.(string)
|
2022-12-18 21:18:30 +08:00
|
|
|
} else if systemSetting.Name == api.SystemSettingCustomizedProfileName {
|
|
|
|
valueMap := value.(map[string]interface{})
|
2022-12-28 20:22:52 +08:00
|
|
|
systemStatus.CustomizedProfile = api.CustomizedProfile{}
|
|
|
|
if v := valueMap["name"]; v != nil {
|
|
|
|
systemStatus.CustomizedProfile.Name = v.(string)
|
|
|
|
}
|
|
|
|
if v := valueMap["logoUrl"]; v != nil {
|
|
|
|
systemStatus.CustomizedProfile.LogoURL = v.(string)
|
|
|
|
}
|
|
|
|
if v := valueMap["description"]; v != nil {
|
|
|
|
systemStatus.CustomizedProfile.Description = v.(string)
|
|
|
|
}
|
|
|
|
if v := valueMap["locale"]; v != nil {
|
|
|
|
systemStatus.CustomizedProfile.Locale = v.(string)
|
|
|
|
}
|
|
|
|
if v := valueMap["appearance"]; v != nil {
|
|
|
|
systemStatus.CustomizedProfile.Appearance = v.(string)
|
|
|
|
}
|
|
|
|
if v := valueMap["externalUrl"]; v != nil {
|
|
|
|
systemStatus.CustomizedProfile.ExternalURL = v.(string)
|
2022-12-18 21:18:30 +08:00
|
|
|
}
|
2022-11-03 21:47:36 +08:00
|
|
|
}
|
2022-05-15 10:57:54 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:07:40 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
2022-12-10 13:23:38 +08:00
|
|
|
// Get database size for host user.
|
2022-11-19 17:07:40 +08:00
|
|
|
if ok {
|
|
|
|
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 && user.Role == api.Host {
|
|
|
|
fi, err := os.Stat(s.Profile.DSN)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read database fileinfo").SetInternal(err)
|
|
|
|
}
|
|
|
|
systemStatus.DBSize = fi.Size()
|
|
|
|
}
|
2022-11-18 21:17:52 +08:00
|
|
|
}
|
|
|
|
|
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-11-03 21:47:36 +08: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)
|
|
|
|
}
|
2022-12-28 20:22:52 +08:00
|
|
|
if user == nil || user.Role != api.Host {
|
2022-11-03 21:47:36 +08:00
|
|
|
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-12-07 22:45:47 +08:00
|
|
|
|
|
|
|
g.POST("/system/vacuum", 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 || user.Role != api.Host {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
}
|
|
|
|
if err := s.Store.Vacuum(ctx); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to vacuum database").SetInternal(err)
|
|
|
|
}
|
|
|
|
c.Response().WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
})
|
2022-03-29 20:53:43 +08:00
|
|
|
}
|
2023-01-03 23:05:42 +08:00
|
|
|
|
|
|
|
func (s *Server) getSystemServerID(ctx context.Context) (string, error) {
|
|
|
|
serverIDKey := api.SystemSettingServerID
|
|
|
|
serverIDValue, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
|
|
|
Name: &serverIDKey,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if serverIDValue == nil || serverIDValue.Value == "" {
|
|
|
|
serverIDValue, err = s.Store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
|
|
|
|
Name: serverIDKey,
|
|
|
|
Value: uuid.NewString(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serverIDValue.Value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getSystemSecretSessionName(ctx context.Context) (string, error) {
|
|
|
|
secretSessionNameKey := api.SystemSettingSecretSessionName
|
|
|
|
secretSessionNameValue, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
|
|
|
Name: &secretSessionNameKey,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if secretSessionNameValue == nil || secretSessionNameValue.Value == "" {
|
|
|
|
secretSessionNameValue, err = s.Store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
|
|
|
|
Name: secretSessionNameKey,
|
|
|
|
Value: uuid.NewString(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secretSessionNameValue.Value, nil
|
|
|
|
}
|