2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-02 23:18:12 +08:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
2023-07-30 00:00:49 +08:00
|
|
|
"net"
|
2023-07-06 21:56:42 +08:00
|
|
|
"net/http"
|
2022-02-04 18:54:24 +08:00
|
|
|
"time"
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
"github.com/google/uuid"
|
2023-07-30 00:00:49 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-01-02 23:18:12 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-07-02 18:56:25 +08:00
|
|
|
apiv1 "github.com/usememos/memos/api/v1"
|
2023-07-30 00:00:49 +08:00
|
|
|
apiv2 "github.com/usememos/memos/api/v2"
|
|
|
|
"github.com/usememos/memos/common/log"
|
2023-07-06 22:53:38 +08:00
|
|
|
"github.com/usememos/memos/common/util"
|
2023-05-26 09:43:51 +08:00
|
|
|
"github.com/usememos/memos/plugin/telegram"
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
|
|
|
"github.com/usememos/memos/store"
|
2023-07-30 00:00:49 +08:00
|
|
|
"go.uber.org/zap"
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-07-30 23:49:10 +08:00
|
|
|
e *echo.Echo
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-03-05 21:42:32 +08:00
|
|
|
ID string
|
2023-06-17 21:25:46 +08:00
|
|
|
Secret string
|
2023-03-05 21:42:32 +08:00
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
2023-05-26 09:43:51 +08:00
|
|
|
|
2023-07-30 23:49:10 +08:00
|
|
|
// API services.
|
|
|
|
apiV2Service *apiv2.APIV2Service
|
|
|
|
|
2023-07-30 09:53:24 +08:00
|
|
|
// Asynchronous runners.
|
|
|
|
backupRunner *BackupRunner
|
|
|
|
telegramBot *telegram.Bot
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store) (*Server, error) {
|
2022-02-03 15:32:03 +08:00
|
|
|
e := echo.New()
|
|
|
|
e.Debug = true
|
|
|
|
e.HideBanner = true
|
2022-05-20 22:48:36 +08:00
|
|
|
e.HidePort = true
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-02-03 10:30:18 +08:00
|
|
|
s := &Server{
|
|
|
|
e: e,
|
2023-06-17 21:25:46 +08:00
|
|
|
Store: store,
|
2023-02-03 10:30:18 +08:00
|
|
|
Profile: profile,
|
2023-01-03 23:05:42 +08:00
|
|
|
|
2023-07-30 09:53:24 +08:00
|
|
|
// Asynchronous runners.
|
|
|
|
backupRunner: NewBackupRunner(store),
|
|
|
|
telegramBot: telegram.NewBotWithHandler(newTelegramHandler(store)),
|
|
|
|
}
|
2023-05-26 09:43:51 +08:00
|
|
|
|
2022-02-04 18:54:24 +08:00
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
2022-08-19 00:45:02 +08:00
|
|
|
Format: `{"time":"${time_rfc3339}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}",` +
|
|
|
|
`"status":${status},"error":"${error}"}` + "\n",
|
2022-02-04 18:54:24 +08:00
|
|
|
}))
|
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
e.Use(middleware.Gzip())
|
|
|
|
|
2022-07-30 14:52:37 +08:00
|
|
|
e.Use(middleware.CORS())
|
|
|
|
|
2023-01-07 10:51:34 +08:00
|
|
|
e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
|
2023-02-12 17:29:23 +08:00
|
|
|
Skipper: defaultGetRequestSkipper,
|
2023-01-07 10:51:34 +08:00
|
|
|
XSSProtection: "1; mode=block",
|
|
|
|
ContentTypeNosniff: "nosniff",
|
|
|
|
XFrameOptions: "SAMEORIGIN",
|
|
|
|
HSTSPreloadEnabled: false,
|
|
|
|
}))
|
2022-12-31 15:40:53 +08:00
|
|
|
|
2022-02-04 18:54:24 +08:00
|
|
|
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
|
2023-07-13 11:25:59 +08:00
|
|
|
Skipper: func(c echo.Context) bool {
|
|
|
|
// this is a hack to skip timeout for openai chat streaming
|
|
|
|
// because streaming require to flush response. But the timeout middleware will break it.
|
2023-07-14 13:09:21 +08:00
|
|
|
return c.Request().URL.Path == "/api/v1/openai/chat-streaming"
|
2023-07-13 11:25:59 +08:00
|
|
|
},
|
2022-02-04 18:54:24 +08:00
|
|
|
ErrorMessage: "Request timeout",
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}))
|
|
|
|
|
2023-01-03 23:05:42 +08:00
|
|
|
serverID, err := s.getSystemServerID(ctx)
|
|
|
|
if err != nil {
|
2023-07-15 10:26:19 +08:00
|
|
|
return nil, fmt.Errorf("failed to retrieve system server ID: %w", err)
|
2023-01-03 23:05:42 +08:00
|
|
|
}
|
|
|
|
s.ID = serverID
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-04-03 13:41:27 +08:00
|
|
|
embedFrontend(e)
|
|
|
|
|
|
|
|
secret := "usememos"
|
2022-05-02 09:40:25 +08:00
|
|
|
if profile.Mode == "prod" {
|
2023-04-03 13:41:27 +08:00
|
|
|
secret, err = s.getSystemSecretSessionName(ctx)
|
2023-01-03 23:05:42 +08:00
|
|
|
if err != nil {
|
2023-07-15 10:26:19 +08:00
|
|
|
return nil, fmt.Errorf("failed to retrieve system secret session name: %w", err)
|
2023-01-03 23:05:42 +08:00
|
|
|
}
|
2022-03-29 07:30:29 +08:00
|
|
|
}
|
2023-06-17 21:25:46 +08:00
|
|
|
s.Secret = secret
|
2023-01-03 23:05:42 +08:00
|
|
|
|
2022-10-26 20:13:02 +08:00
|
|
|
rootGroup := e.Group("")
|
2023-07-02 18:56:25 +08:00
|
|
|
apiV1Service := apiv1.NewAPIV1Service(s.Secret, profile, store)
|
2023-07-01 00:03:28 +08:00
|
|
|
apiV1Service.Register(rootGroup)
|
2023-06-17 21:25:46 +08:00
|
|
|
|
2023-07-30 23:49:10 +08:00
|
|
|
s.apiV2Service = apiv2.NewAPIV2Service(s.Secret, profile, store, s.Profile.Port+1)
|
2023-07-30 00:00:49 +08:00
|
|
|
// Register gRPC gateway as api v2.
|
2023-07-30 23:49:10 +08:00
|
|
|
if err := s.apiV2Service.RegisterGateway(ctx, e); err != nil {
|
2023-07-30 01:35:00 +08:00
|
|
|
return nil, fmt.Errorf("failed to register gRPC gateway: %w", err)
|
|
|
|
}
|
2023-07-30 00:00:49 +08:00
|
|
|
|
2023-01-03 23:05:42 +08:00
|
|
|
return s, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2023-01-30 00:03:21 +08:00
|
|
|
func (s *Server) Start(ctx context.Context) error {
|
2023-01-02 23:18:12 +08:00
|
|
|
if err := s.createServerStartActivity(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-05-26 09:43:51 +08:00
|
|
|
|
2023-05-29 19:49:05 +08:00
|
|
|
go s.telegramBot.Start(ctx)
|
2023-07-30 09:53:24 +08:00
|
|
|
go s.backupRunner.Run(ctx)
|
2023-05-26 09:43:51 +08:00
|
|
|
|
2023-07-30 00:00:49 +08:00
|
|
|
// Start gRPC server.
|
|
|
|
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Profile.Port+1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
2023-07-30 23:49:10 +08:00
|
|
|
if err := s.apiV2Service.GetGRPCServer().Serve(listen); err != nil {
|
2023-07-30 00:00:49 +08:00
|
|
|
log.Error("grpc server listen error", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
|
2023-01-01 21:32:17 +08:00
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
|
2023-02-03 10:30:18 +08:00
|
|
|
func (s *Server) Shutdown(ctx context.Context) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Shutdown echo server
|
|
|
|
if err := s.e.Shutdown(ctx); err != nil {
|
|
|
|
fmt.Printf("failed to shutdown server, error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close database connection
|
2023-06-17 21:25:46 +08:00
|
|
|
if err := s.Store.GetDB().Close(); err != nil {
|
2023-02-03 10:30:18 +08:00
|
|
|
fmt.Printf("failed to close database, error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("memos stopped properly\n")
|
|
|
|
}
|
|
|
|
|
2023-04-17 21:34:59 +08:00
|
|
|
func (s *Server) GetEcho() *echo.Echo {
|
|
|
|
return s.e
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (s *Server) getSystemServerID(ctx context.Context) (string, error) {
|
|
|
|
serverIDSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
|
|
|
|
Name: apiv1.SystemSettingServerIDName.String(),
|
|
|
|
})
|
2023-07-06 22:53:38 +08:00
|
|
|
if err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if serverIDSetting == nil || serverIDSetting.Value == "" {
|
|
|
|
serverIDSetting, err = s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
|
|
|
|
Name: apiv1.SystemSettingServerIDName.String(),
|
|
|
|
Value: uuid.NewString(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serverIDSetting.Value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getSystemSecretSessionName(ctx context.Context) (string, error) {
|
|
|
|
secretSessionNameValue, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
|
|
|
|
Name: apiv1.SystemSettingSecretSessionName.String(),
|
|
|
|
})
|
2023-07-06 22:53:38 +08:00
|
|
|
if err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if secretSessionNameValue == nil || secretSessionNameValue.Value == "" {
|
|
|
|
secretSessionNameValue, err = s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
|
|
|
|
Name: apiv1.SystemSettingSecretSessionName.String(),
|
|
|
|
Value: uuid.NewString(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secretSessionNameValue.Value, nil
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:18:12 +08:00
|
|
|
func (s *Server) createServerStartActivity(ctx context.Context) error {
|
2023-07-02 18:56:25 +08:00
|
|
|
payload := apiv1.ActivityServerStartPayload{
|
2023-01-03 20:05:37 +08:00
|
|
|
ServerID: s.ID,
|
|
|
|
Profile: s.Profile,
|
2023-01-02 23:18:12 +08:00
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 23:18:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &store.Activity{
|
2023-07-02 18:56:25 +08:00
|
|
|
CreatorID: apiv1.UnknownID,
|
|
|
|
Type: apiv1.ActivityServerStart.String(),
|
|
|
|
Level: apiv1.ActivityInfo.String(),
|
2023-02-17 23:55:56 +08:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 23:18:12 +08:00
|
|
|
})
|
2023-01-07 11:49:58 +08:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
|
|
|
|
func defaultGetRequestSkipper(c echo.Context) bool {
|
|
|
|
return c.Request().Method == http.MethodGet
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultAPIRequestSkipper(c echo.Context) bool {
|
2023-07-30 23:49:10 +08:00
|
|
|
path := c.Request().URL.Path
|
|
|
|
return util.HasPrefixes(path, "/api", "/api/v1", "api/v2")
|
2023-07-06 21:56:42 +08:00
|
|
|
}
|