2022-03-29 15:53:43 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-03 18:05:42 +03:00
|
|
|
"context"
|
2022-03-29 15:53:43 +03:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-11-18 16:17:52 +03:00
|
|
|
"os"
|
2022-03-29 15:53:43 +03:00
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
"github.com/google/uuid"
|
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 {
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(s.Profile))
|
2022-03-29 15:53:43 +03:00
|
|
|
})
|
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-12-25 05:28:51 +03:00
|
|
|
hostUser.Email = ""
|
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{
|
2023-02-13 19:07:31 +03:00
|
|
|
Host: hostUser,
|
|
|
|
Profile: *s.Profile,
|
|
|
|
DBSize: 0,
|
|
|
|
AllowSignUp: false,
|
|
|
|
DisablePublicMemos: false,
|
|
|
|
AdditionalStyle: "",
|
|
|
|
AdditionalScript: "",
|
2022-12-18 16:18:30 +03:00
|
|
|
CustomizedProfile: api.CustomizedProfile{
|
2022-12-22 14:48:44 +03:00
|
|
|
Name: "memos",
|
2022-12-22 19:21:53 +03:00
|
|
|
LogoURL: "",
|
2022-12-22 14:48:44 +03:00
|
|
|
Description: "",
|
|
|
|
Locale: "en",
|
|
|
|
Appearance: "system",
|
|
|
|
ExternalURL: "",
|
2022-12-18 16:18:30 +03:00
|
|
|
},
|
2023-04-03 09:13:22 +03:00
|
|
|
StorageServiceID: api.DatabaseStorage,
|
2023-03-19 14:37:57 +03:00
|
|
|
LocalStoragePath: "",
|
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 {
|
2023-04-03 04:36:34 +03:00
|
|
|
if systemSetting.Name == api.SystemSettingServerIDName || systemSetting.Name == api.SystemSettingSecretSessionName || systemSetting.Name == api.SystemSettingOpenAIConfigName {
|
2023-01-03 18:05:42 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-18 17:34:22 +03:00
|
|
|
var baseValue any
|
2023-03-11 07:26:40 +03:00
|
|
|
err := json.Unmarshal([]byte(systemSetting.Value), &baseValue)
|
2022-11-03 16:47:36 +03:00
|
|
|
if err != nil {
|
2023-02-22 14:21:08 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting value").SetInternal(err)
|
2022-11-03 16:47:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if systemSetting.Name == api.SystemSettingAllowSignUpName {
|
2023-03-11 07:26:40 +03:00
|
|
|
systemStatus.AllowSignUp = baseValue.(bool)
|
2023-02-13 19:07:31 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingDisablePublicMemosName {
|
2023-03-11 07:26:40 +03:00
|
|
|
systemStatus.DisablePublicMemos = baseValue.(bool)
|
2022-11-11 18:42:44 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingAdditionalStyleName {
|
2023-03-11 07:26:40 +03:00
|
|
|
systemStatus.AdditionalStyle = baseValue.(string)
|
2022-11-14 17:21:19 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingAdditionalScriptName {
|
2023-03-11 07:26:40 +03:00
|
|
|
systemStatus.AdditionalScript = baseValue.(string)
|
2022-12-18 16:18:30 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingCustomizedProfileName {
|
2023-03-11 07:26:40 +03:00
|
|
|
customizedProfile := api.CustomizedProfile{}
|
|
|
|
err := json.Unmarshal([]byte(systemSetting.Value), &customizedProfile)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting customized profile value").SetInternal(err)
|
2022-12-18 16:18:30 +03:00
|
|
|
}
|
2023-03-11 07:26:40 +03:00
|
|
|
systemStatus.CustomizedProfile = customizedProfile
|
2023-02-15 17:54:46 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingStorageServiceIDName {
|
2023-03-11 07:26:40 +03:00
|
|
|
systemStatus.StorageServiceID = int(baseValue.(float64))
|
2023-03-19 14:37:57 +03:00
|
|
|
} else if systemSetting.Name == api.SystemSettingLocalStoragePathName {
|
|
|
|
systemStatus.LocalStoragePath = baseValue.(string)
|
2022-11-03 16:47:36 +03:00
|
|
|
}
|
2022-05-15 05:57:54 +03:00
|
|
|
}
|
|
|
|
|
2022-11-19 12:07:40 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
2022-12-10 08:23:38 +03:00
|
|
|
// Get database size for host user.
|
2022-11-19 12:07:40 +03: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 16:17:52 +03:00
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(systemStatus))
|
2022-05-15 05:57:54 +03:00
|
|
|
})
|
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)
|
|
|
|
}
|
2022-12-28 15:22:52 +03:00
|
|
|
if user == nil || user.Role != api.Host {
|
2022-11-03 16:47:36 +03: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)
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(systemSetting))
|
2022-11-03 16:47:36 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/system/setting", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
2023-02-24 03:31:54 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(systemSettingList))
|
2022-11-03 16:47:36 +03:00
|
|
|
})
|
2022-12-07 17:45:47 +03: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")
|
|
|
|
}
|
2023-02-24 03:31:54 +03:00
|
|
|
|
2022-12-07 17:45:47 +03:00
|
|
|
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")
|
|
|
|
}
|
2023-02-24 03:31:54 +03:00
|
|
|
|
2022-12-07 17:45:47 +03:00
|
|
|
if err := s.Store.Vacuum(ctx); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to vacuum database").SetInternal(err)
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, true)
|
2022-12-07 17:45:47 +03:00
|
|
|
})
|
2022-03-29 15:53:43 +03:00
|
|
|
}
|
2023-01-03 18:05:42 +03:00
|
|
|
|
|
|
|
func (s *Server) getSystemServerID(ctx context.Context) (string, error) {
|
|
|
|
serverIDValue, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
2023-04-03 04:36:34 +03:00
|
|
|
Name: api.SystemSettingServerIDName,
|
2023-01-03 18:05:42 +03:00
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if serverIDValue == nil || serverIDValue.Value == "" {
|
|
|
|
serverIDValue, err = s.Store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
|
2023-04-03 04:36:34 +03:00
|
|
|
Name: api.SystemSettingServerIDName,
|
2023-01-03 18:05:42 +03:00
|
|
|
Value: uuid.NewString(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serverIDValue.Value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getSystemSecretSessionName(ctx context.Context) (string, error) {
|
|
|
|
secretSessionNameValue, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
2023-02-15 17:54:46 +03:00
|
|
|
Name: api.SystemSettingSecretSessionName,
|
2023-01-03 18:05:42 +03:00
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if secretSessionNameValue == nil || secretSessionNameValue.Value == "" {
|
|
|
|
secretSessionNameValue, err = s.Store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
|
2023-02-15 17:54:46 +03:00
|
|
|
Name: api.SystemSettingSecretSessionName,
|
2023-01-03 18:05:42 +03:00
|
|
|
Value: uuid.NewString(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secretSessionNameValue.Value, nil
|
|
|
|
}
|