2022-02-03 10:32:03 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-02 18:18:12 +03:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
2023-07-29 19:00:49 +03:00
|
|
|
"net"
|
2023-07-06 16:56:42 +03:00
|
|
|
"net/http"
|
2023-09-17 14:20:03 +03:00
|
|
|
"strings"
|
2022-02-04 13:54:24 +03:00
|
|
|
"time"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
"github.com/google/uuid"
|
2023-07-29 19:00:49 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-01-02 18:18:12 +03:00
|
|
|
"github.com/pkg/errors"
|
2023-08-09 16:53:06 +03:00
|
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
2023-09-17 17:55:13 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
apiv1 "github.com/usememos/memos/api/v1"
|
2023-07-29 19:00:49 +03:00
|
|
|
apiv2 "github.com/usememos/memos/api/v2"
|
|
|
|
"github.com/usememos/memos/common/log"
|
2023-05-26 04:43:51 +03:00
|
|
|
"github.com/usememos/memos/plugin/telegram"
|
2023-09-19 17:35:20 +03:00
|
|
|
"github.com/usememos/memos/server/integration"
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2023-08-26 03:07:43 +03:00
|
|
|
"github.com/usememos/memos/server/service"
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/store"
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-07-30 18:49:10 +03:00
|
|
|
e *echo.Echo
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-03-05 16:42:32 +03:00
|
|
|
ID string
|
2023-06-17 16:25:46 +03:00
|
|
|
Secret string
|
2023-03-05 16:42:32 +03:00
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-07-30 18:49:10 +03:00
|
|
|
// API services.
|
|
|
|
apiV2Service *apiv2.APIV2Service
|
|
|
|
|
2023-07-30 04:53:24 +03:00
|
|
|
// Asynchronous runners.
|
2023-08-26 03:07:43 +03:00
|
|
|
backupRunner *service.BackupRunner
|
2023-07-30 04:53:24 +03:00
|
|
|
telegramBot *telegram.Bot
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store) (*Server, error) {
|
2022-02-03 10:32:03 +03:00
|
|
|
e := echo.New()
|
|
|
|
e.Debug = true
|
|
|
|
e.HideBanner = true
|
2022-05-20 17:48:36 +03:00
|
|
|
e.HidePort = true
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-02-03 05:30:18 +03:00
|
|
|
s := &Server{
|
|
|
|
e: e,
|
2023-06-17 16:25:46 +03:00
|
|
|
Store: store,
|
2023-02-03 05:30:18 +03:00
|
|
|
Profile: profile,
|
2023-01-03 18:05:42 +03:00
|
|
|
|
2023-07-30 04:53:24 +03:00
|
|
|
// Asynchronous runners.
|
2023-08-26 03:07:43 +03:00
|
|
|
backupRunner: service.NewBackupRunner(store),
|
2023-09-19 17:35:20 +03:00
|
|
|
telegramBot: telegram.NewBotWithHandler(integration.NewTelegramHandler(store)),
|
2023-07-30 04:53:24 +03:00
|
|
|
}
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2022-02-04 13:54:24 +03:00
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
2022-08-18 19:45:02 +03:00
|
|
|
Format: `{"time":"${time_rfc3339}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}",` +
|
|
|
|
`"status":${status},"error":"${error}"}` + "\n",
|
2022-02-04 13:54:24 +03:00
|
|
|
}))
|
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
e.Use(middleware.Gzip())
|
|
|
|
|
2023-09-19 15:34:02 +03:00
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
|
|
Skipper: grpcRequestSkipper,
|
|
|
|
AllowOrigins: []string{"*"},
|
|
|
|
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
|
|
|
|
}))
|
|
|
|
|
2023-09-17 13:11:13 +03:00
|
|
|
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
|
2023-09-17 14:20:03 +03:00
|
|
|
Skipper: grpcRequestSkipper,
|
2023-09-17 13:11:13 +03:00
|
|
|
Timeout: 30 * time.Second,
|
2023-01-07 05:51:34 +03:00
|
|
|
}))
|
2022-12-31 10:40:53 +03:00
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
serverID, err := s.getSystemServerID(ctx)
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to retrieve system server ID")
|
2023-01-03 18:05:42 +03:00
|
|
|
}
|
|
|
|
s.ID = serverID
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-09-06 16:59:20 +03:00
|
|
|
// Serve frontend.
|
2023-04-03 08:41:27 +03:00
|
|
|
embedFrontend(e)
|
|
|
|
|
2023-09-06 16:59:20 +03:00
|
|
|
// Serve swagger in dev/demo mode.
|
|
|
|
if profile.Mode == "dev" || profile.Mode == "demo" {
|
|
|
|
e.GET("/api/*", echoSwagger.WrapHandler)
|
|
|
|
}
|
2023-08-09 16:53:06 +03:00
|
|
|
|
2023-04-03 08:41:27 +03:00
|
|
|
secret := "usememos"
|
2022-05-02 04:40:25 +03:00
|
|
|
if profile.Mode == "prod" {
|
2023-04-03 08:41:27 +03:00
|
|
|
secret, err = s.getSystemSecretSessionName(ctx)
|
2023-01-03 18:05:42 +03:00
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to retrieve system secret session name")
|
2023-01-03 18:05:42 +03:00
|
|
|
}
|
2022-03-29 02:30:29 +03:00
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
s.Secret = secret
|
2023-01-03 18:05:42 +03:00
|
|
|
|
2022-10-26 15:13:02 +03:00
|
|
|
rootGroup := e.Group("")
|
2023-09-19 17:35:20 +03:00
|
|
|
apiV1Service := apiv1.NewAPIV1Service(s.Secret, profile, store, s.telegramBot)
|
2023-06-30 19:03:28 +03:00
|
|
|
apiV1Service.Register(rootGroup)
|
2023-06-17 16:25:46 +03:00
|
|
|
|
2023-07-30 18:49:10 +03:00
|
|
|
s.apiV2Service = apiv2.NewAPIV2Service(s.Secret, profile, store, s.Profile.Port+1)
|
2023-07-29 19:00:49 +03:00
|
|
|
// Register gRPC gateway as api v2.
|
2023-07-30 18:49:10 +03:00
|
|
|
if err := s.apiV2Service.RegisterGateway(ctx, e); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to register gRPC gateway")
|
2023-07-29 20:35:00 +03:00
|
|
|
}
|
2023-07-29 19:00:49 +03:00
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
return s, nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-01-29 19:03:21 +03:00
|
|
|
func (s *Server) Start(ctx context.Context) error {
|
2023-01-02 18:18:12 +03:00
|
|
|
if err := s.createServerStartActivity(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-05-29 14:49:05 +03:00
|
|
|
go s.telegramBot.Start(ctx)
|
2023-07-30 04:53:24 +03:00
|
|
|
go s.backupRunner.Run(ctx)
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-07-29 19:00:49 +03:00
|
|
|
// Start gRPC server.
|
2023-08-24 04:59:23 +03:00
|
|
|
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port+1))
|
2023-07-29 19:00:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
2023-07-30 18:49:10 +03:00
|
|
|
if err := s.apiV2Service.GetGRPCServer().Serve(listen); err != nil {
|
2023-07-29 19:00:49 +03:00
|
|
|
log.Error("grpc server listen error", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-24 04:59:23 +03:00
|
|
|
return s.e.Start(fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port))
|
2023-01-01 16:32:17 +03:00
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
|
2023-02-03 05:30:18 +03: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-09-27 04:27:31 +03:00
|
|
|
if err := s.Store.Close(); err != nil {
|
2023-02-03 05:30:18 +03:00
|
|
|
fmt.Printf("failed to close database, error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("memos stopped properly\n")
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:34:59 +03:00
|
|
|
func (s *Server) GetEcho() *echo.Echo {
|
|
|
|
return s.e
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03: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 17:53:38 +03:00
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03: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 17:53:38 +03:00
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03: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 18:18:12 +03:00
|
|
|
func (s *Server) createServerStartActivity(ctx context.Context) error {
|
2023-07-02 13:56:25 +03:00
|
|
|
payload := apiv1.ActivityServerStartPayload{
|
2023-01-03 15:05:37 +03:00
|
|
|
ServerID: s.ID,
|
|
|
|
Profile: s.Profile,
|
2023-01-02 18:18:12 +03:00
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 18:18:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &store.Activity{
|
2023-10-01 09:14:33 +03:00
|
|
|
Type: apiv1.ActivityServerStart.String(),
|
|
|
|
Level: apiv1.ActivityInfo.String(),
|
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 18:18:12 +03:00
|
|
|
})
|
2023-01-07 06:49:58 +03:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-09-17 14:20:03 +03:00
|
|
|
func grpcRequestSkipper(c echo.Context) bool {
|
|
|
|
return strings.HasPrefix(c.Request().URL.Path, "/memos.api.v2.")
|
2023-07-06 16:56:42 +03:00
|
|
|
}
|