chore: update server activity (#898)

This commit is contained in:
boojack 2023-01-03 20:05:37 +08:00 committed by GitHub
parent e5550828a0
commit 79180928d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 28 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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 {