mirror of
https://github.com/usememos/memos.git
synced 2025-02-19 04:40:40 +01:00
chore: update server activity (#898)
This commit is contained in:
parent
e5550828a0
commit
79180928d4
@ -103,7 +103,8 @@ type ActivityTagCreatePayload struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ActivityServerStartPayload struct {
|
type ActivityServerStartPayload struct {
|
||||||
Profile *profile.Profile `json:"profile"`
|
ServerID string `json:"serverId"`
|
||||||
|
Profile *profile.Profile `json:"profile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Activity struct {
|
type Activity struct {
|
||||||
|
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
@ -10,6 +11,8 @@ import (
|
|||||||
type SystemSettingName string
|
type SystemSettingName string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// SystemSettingServerID is the key type of server id.
|
||||||
|
SystemSettingServerID SystemSettingName = "serverId"
|
||||||
// SystemSettingAllowSignUpName is the key type of allow signup setting.
|
// SystemSettingAllowSignUpName is the key type of allow signup setting.
|
||||||
SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
|
SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
|
||||||
// SystemSettingAdditionalStyleName is the key type of additional style.
|
// SystemSettingAdditionalStyleName is the key type of additional style.
|
||||||
@ -38,6 +41,8 @@ type CustomizedProfile struct {
|
|||||||
|
|
||||||
func (key SystemSettingName) String() string {
|
func (key SystemSettingName) String() string {
|
||||||
switch key {
|
switch key {
|
||||||
|
case SystemSettingServerID:
|
||||||
|
return "serverId"
|
||||||
case SystemSettingAllowSignUpName:
|
case SystemSettingAllowSignUpName:
|
||||||
return "allowSignUp"
|
return "allowSignUp"
|
||||||
case SystemSettingAdditionalStyleName:
|
case SystemSettingAdditionalStyleName:
|
||||||
@ -56,7 +61,7 @@ var (
|
|||||||
|
|
||||||
type SystemSetting struct {
|
type SystemSetting struct {
|
||||||
Name SystemSettingName
|
Name SystemSettingName
|
||||||
// Value is a JSON string with basic value
|
// Value is a JSON string with basic value.
|
||||||
Value string
|
Value string
|
||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
@ -68,7 +73,9 @@ type SystemSettingUpsert struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (upsert SystemSettingUpsert) Validate() error {
|
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
|
value := false
|
||||||
err := json.Unmarshal([]byte(upsert.Value), &value)
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -26,8 +26,19 @@ const (
|
|||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
func run(profile *profile.Profile) error {
|
func run() error {
|
||||||
ctx := context.Background()
|
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)
|
db := DB.NewDB(profile)
|
||||||
if err := db.Open(ctx); err != nil {
|
if err := db.Open(ctx); err != nil {
|
||||||
@ -48,30 +59,9 @@ func run(profile *profile.Profile) error {
|
|||||||
return serverInstance.Run(ctx)
|
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() {
|
func main() {
|
||||||
if err := execute(); err != nil {
|
if err := run(); err != nil {
|
||||||
|
fmt.Printf("error: %+v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/usememos/memos/api"
|
"github.com/usememos/memos/api"
|
||||||
|
"github.com/usememos/memos/common"
|
||||||
"github.com/usememos/memos/server/profile"
|
"github.com/usememos/memos/server/profile"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
|
|
||||||
@ -21,6 +23,8 @@ import (
|
|||||||
type Server struct {
|
type Server struct {
|
||||||
e *echo.Echo
|
e *echo.Echo
|
||||||
|
|
||||||
|
ID string
|
||||||
|
|
||||||
Collector *MetricCollector
|
Collector *MetricCollector
|
||||||
|
|
||||||
Profile *profile.Profile
|
Profile *profile.Profile
|
||||||
@ -99,15 +103,35 @@ func NewServer(profile *profile.Profile) *Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Run(ctx context.Context) error {
|
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 {
|
if err := s.createServerStartActivity(ctx); err != nil {
|
||||||
return errors.Wrap(err, "failed to create activity")
|
return errors.Wrap(err, "failed to create activity")
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
|
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) createServerStartActivity(ctx context.Context) error {
|
func (s *Server) createServerStartActivity(ctx context.Context) error {
|
||||||
payload := api.ActivityServerStartPayload{
|
payload := api.ActivityServerStartPayload{
|
||||||
Profile: s.Profile,
|
ServerID: s.ID,
|
||||||
|
Profile: s.Profile,
|
||||||
}
|
}
|
||||||
payloadStr, err := json.Marshal(payload)
|
payloadStr, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user