Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: update server activity (#898)

boojack79180928d4af
diff --git a/api/activity.go b/api/activity.go
index 62a4ef267..80f76bcf4 100644
--- a/api/activity.go
+++ b/api/activity.go
@@ -103,7 +103,8 @@ type ActivityTagCreatePayload struct {
}
type ActivityServerStartPayload struct {
- Profile *profile.Profile `json:"profile"`
+ ServerID string `json:"serverId"`
+ Profile *profile.Profile `json:"profile"`
}
type Activity struct {
diff --git a/api/system_setting.go b/api/system_setting.go
index 645f29897..7e4f69f65 100644
--- a/api/system_setting.go
+++ b/api/system_setting.go
@@ -2,6 +2,7 @@ package api
import (
"encoding/json"
+ "errors"
"fmt"
"golang.org/x/exp/slices"
@@ -10,6 +11,8 @@ import (
type SystemSettingName string
const (
+ // SystemSettingServerID is the key type of server id.
+ SystemSettingServerID SystemSettingName = "serverId"
// SystemSettingAllowSignUpName is the key type of allow signup setting.
SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
// SystemSettingAdditionalStyleName is the key type of additional style.
@@ -38,6 +41,8 @@ type CustomizedProfile struct {
func (key SystemSettingName) String() string {
switch key {
+ case SystemSettingServerID:
+ return "serverId"
case SystemSettingAllowSignUpName:
return "allowSignUp"
case SystemSettingAdditionalStyleName:
@@ -56,7 +61,7 @@ var (
type SystemSetting struct {
Name SystemSettingName
- // Value is a JSON string with basic value
+ // Value is a JSON string with basic value.
Value string
Description string
}
@@ -68,7 +73,9 @@ type SystemSettingUpsert struct {
}
func (upsert SystemSettingUpsert) Validate() error {
- if upsert.Name == SystemSettingAllowSignUpName {
+ if upsert.Name == SystemSettingServerID {
+ return errors.New("update server id is not allowed")
+ } else if upsert.Name == SystemSettingAllowSignUpName {
value := false
err := json.Unmarshal([]byte(upsert.Value), &value)
if err != nil {
diff --git a/bin/server/main.go b/bin/server/main.go
index edd86b4dd..ccae898c4 100644
--- a/bin/server/main.go
+++ b/bin/server/main.go
@@ -26,8 +26,19 @@ const (
`
)
-func run(profile *profile.Profile) error {
+func run() error {
ctx := context.Background()
+ profile, err := profile.GetProfile()
+ if err != nil {
+ return err
+ }
+ println("---")
+ println("profile")
+ println("mode:", profile.Mode)
+ println("port:", profile.Port)
+ println("dsn:", profile.DSN)
+ println("version:", profile.Version)
+ println("---")
db := DB.NewDB(profile)
if err := db.Open(ctx); err != nil {
@@ -48,30 +59,9 @@ func run(profile *profile.Profile) error {
return serverInstance.Run(ctx)
}
-func execute() error {
- profile, err := profile.GetProfile()
- if err != nil {
- return err
- }
-
- println("---")
- println("profile")
- println("mode:", profile.Mode)
- println("port:", profile.Port)
- println("dsn:", profile.DSN)
- println("version:", profile.Version)
- println("---")
-
- if err := run(profile); err != nil {
- fmt.Printf("error: %+v\n", err)
- return err
- }
-
- return nil
-}
-
func main() {
- if err := execute(); err != nil {
+ if err := run(); err != nil {
+ fmt.Printf("error: %+v\n", err)
os.Exit(1)
}
}
diff --git a/server/server.go b/server/server.go
index 5c50f5e1c..2b83a93cb 100644
--- a/server/server.go
+++ b/server/server.go
@@ -6,8 +6,10 @@ import (
"fmt"
"time"
+ "github.com/google/uuid"
"github.com/pkg/errors"
"github.com/usememos/memos/api"
+ "github.com/usememos/memos/common"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
@@ -21,6 +23,8 @@ import (
type Server struct {
e *echo.Echo
+ ID string
+
Collector *MetricCollector
Profile *profile.Profile
@@ -99,15 +103,35 @@ func NewServer(profile *profile.Profile) *Server {
}
func (s *Server) Run(ctx context.Context) 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
+ }
+ }
+ s.ID = serverIDValue.Value
+
if err := s.createServerStartActivity(ctx); err != nil {
return errors.Wrap(err, "failed to create activity")
}
+
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
}
func (s *Server) createServerStartActivity(ctx context.Context) error {
payload := api.ActivityServerStartPayload{
- Profile: s.Profile,
+ ServerID: s.ID,
+ Profile: s.Profile,
}
payloadStr, err := json.Marshal(payload)
if err != nil {