1
1
mirror of https://github.com/usememos/memos.git synced 2024-12-24 03:33:52 +03:00
memos/server/server.go

183 lines
4.7 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
2023-01-02 18:18:12 +03:00
"context"
2022-02-03 10:32:03 +03:00
"fmt"
"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
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2023-01-02 18:18:12 +03:00
"github.com/pkg/errors"
echoSwagger "github.com/swaggo/echo-swagger"
2023-09-17 17:55:13 +03:00
apiv1 "github.com/usememos/memos/api/v1"
apiv2 "github.com/usememos/memos/api/v2"
"github.com/usememos/memos/plugin/telegram"
"github.com/usememos/memos/server/integration"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/server/profile"
2023-10-17 18:44:16 +03:00
"github.com/usememos/memos/server/service/backup"
"github.com/usememos/memos/server/service/metric"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/store"
2022-02-03 10:32:03 +03:00
)
type Server struct {
e *echo.Echo
2022-02-03 10:32:03 +03:00
ID string
Secret string
Profile *profile.Profile
Store *store.Store
// API services.
apiV2Service *apiv2.APIV2Service
2023-07-30 04:53:24 +03:00
// Asynchronous runners.
2023-10-17 18:44:16 +03:00
backupRunner *backup.BackupRunner
2023-07-30 04:53:24 +03:00
telegramBot *telegram.Bot
2022-02-03 10:32:03 +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,
Store: store,
2023-02-03 05:30:18 +03:00
Profile: profile,
2023-07-30 04:53:24 +03:00
// Asynchronous runners.
2023-10-17 18:44:16 +03:00
backupRunner: backup.NewBackupRunner(store),
telegramBot: telegram.NewBotWithHandler(integration.NewTelegramHandler(store)),
2023-07-30 04:53:24 +03:00
}
2022-02-04 13:54:24 +03:00
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `{"time":"${time_rfc3339}","latency":"${latency_human}",` +
2022-08-18 19:45:02 +03:00
`"method":"${method}","uri":"${uri}",` +
`"status":${status},"error":"${error}"}` + "\n",
2022-02-04 13:54:24 +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
}))
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")
}
s.ID = serverID
2022-02-03 10:32:03 +03:00
2023-09-06 16:59:20 +03:00
// Serve frontend.
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)
}
secret := "usememos"
2022-05-02 04:40:25 +03:00
if profile.Mode == "prod" {
secret, err = s.getSystemSecretSessionName(ctx)
if err != nil {
2023-09-17 17:55:13 +03:00
return nil, errors.Wrap(err, "failed to retrieve system secret session name")
}
2022-03-29 02:30:29 +03:00
}
s.Secret = secret
rootGroup := e.Group("")
apiV1Service := apiv1.NewAPIV1Service(s.Secret, profile, store, s.telegramBot)
apiV1Service.Register(rootGroup)
s.apiV2Service = apiv2.NewAPIV2Service(s.Secret, profile, store, s.Profile.Port+1)
// Register gRPC gateway as api v2.
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")
}
return s, nil
2022-02-03 10:32:03 +03:00
}
func (s *Server) Start(ctx context.Context) error {
go s.telegramBot.Start(ctx)
2023-07-30 04:53:24 +03:00
go s.backupRunner.Run(ctx)
2023-10-17 18:44:16 +03:00
metric.Enqueue("server start")
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
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")
}
func (s *Server) GetEcho() *echo.Echo {
return s.e
}
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 {
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 {
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-09-17 14:20:03 +03:00
func grpcRequestSkipper(c echo.Context) bool {
return strings.HasPrefix(c.Request().URL.Path, "/memos.api.v2.")
}