2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-02 23:18:12 +08:00
|
|
|
"context"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
2023-07-06 21:56:42 +08:00
|
|
|
"net/http"
|
2023-09-17 19:20:03 +08:00
|
|
|
"strings"
|
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"
|
2023-01-02 23:18:12 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-05-26 09:43:51 +08:00
|
|
|
"github.com/usememos/memos/plugin/telegram"
|
2024-04-11 17:53:00 +08:00
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
2023-09-19 22:35:20 +08:00
|
|
|
"github.com/usememos/memos/server/integration"
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2024-02-29 01:32:59 +08:00
|
|
|
apiv2 "github.com/usememos/memos/server/route/api/v2"
|
|
|
|
"github.com/usememos/memos/server/route/frontend"
|
2023-11-06 20:49:02 +08:00
|
|
|
versionchecker "github.com/usememos/memos/server/service/version_checker"
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/store"
|
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 09:53:24 +08:00
|
|
|
// Asynchronous runners.
|
2023-12-19 22:34:06 +08:00
|
|
|
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.
|
2023-11-13 22:12:25 +08:00
|
|
|
telegramBot: telegram.NewBotWithHandler(integration.NewTelegramHandler(store)),
|
|
|
|
}
|
|
|
|
|
2024-02-29 23:54:43 +08:00
|
|
|
// Register CORS middleware.
|
2024-04-07 22:15:15 +08:00
|
|
|
e.Use(CORSMiddleware(s.Profile.Origins))
|
2024-02-04 23:48:26 +08:00
|
|
|
|
2024-04-11 17:53:00 +08:00
|
|
|
workspaceBasicSetting, err := s.getOrUpsertWorkspaceBasicSetting(ctx)
|
2023-01-03 23:05:42 +08:00
|
|
|
if err != nil {
|
2024-04-11 17:53:00 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to get workspace basic setting")
|
2023-01-03 23:05:42 +08:00
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-04-03 13:41:27 +08:00
|
|
|
secret := "usememos"
|
2022-05-02 09:40:25 +08:00
|
|
|
if profile.Mode == "prod" {
|
2024-04-11 17:53:00 +08:00
|
|
|
secret = workspaceBasicSetting.SecretKey
|
2022-03-29 07:30:29 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
s.ID = workspaceBasicSetting.ServerId
|
2023-06-17 21:25:46 +08:00
|
|
|
s.Secret = secret
|
2023-01-03 23:05:42 +08:00
|
|
|
|
2023-11-26 23:06:50 +08:00
|
|
|
// Register healthz endpoint.
|
|
|
|
e.GET("/healthz", func(c echo.Context) error {
|
2023-11-26 23:33:34 +08:00
|
|
|
return c.String(http.StatusOK, "Service ready.")
|
2023-11-26 23:06:50 +08:00
|
|
|
})
|
|
|
|
|
2024-02-29 23:54:43 +08:00
|
|
|
// Only serve frontend when it's enabled.
|
|
|
|
if profile.Frontend {
|
|
|
|
frontendService := frontend.NewFrontendService(profile, store)
|
|
|
|
frontendService.Serve(ctx, e)
|
|
|
|
}
|
|
|
|
|
2023-12-15 07:32:49 +08:00
|
|
|
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-12-15 07:32:49 +08:00
|
|
|
if err := apiV2Service.RegisterGateway(ctx, e); err != nil {
|
2023-09-17 22:55:13 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to register gRPC gateway")
|
2023-07-30 01:35:00 +08:00
|
|
|
}
|
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-11-06 22:33:12 +08:00
|
|
|
go versionchecker.NewVersionChecker(s.Store, s.Profile).Start(ctx)
|
2023-05-29 19:49:05 +08:00
|
|
|
go s.telegramBot.Start(ctx)
|
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 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-09-27 09:27:31 +08:00
|
|
|
if err := s.Store.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
|
|
|
|
}
|
|
|
|
|
2024-04-11 17:53:00 +08:00
|
|
|
func (s *Server) getOrUpsertWorkspaceBasicSetting(ctx context.Context) (*storepb.WorkspaceBasicSetting, error) {
|
|
|
|
workspaceBasicSetting, err := s.Store.GetWorkspaceBasicSetting(ctx)
|
2023-07-06 22:53:38 +08:00
|
|
|
if err != nil {
|
2024-04-11 17:53:00 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to get workspace basic setting")
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
modified := false
|
|
|
|
if workspaceBasicSetting.ServerId == "" {
|
|
|
|
workspaceBasicSetting.ServerId = uuid.NewString()
|
|
|
|
modified = true
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
if workspaceBasicSetting.SecretKey == "" {
|
|
|
|
workspaceBasicSetting.SecretKey = uuid.NewString()
|
|
|
|
modified = true
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
if modified {
|
|
|
|
workspaceSetting, err := s.Store.UpsertWorkspaceSettingV1(ctx, &storepb.WorkspaceSetting{
|
|
|
|
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_BASIC,
|
|
|
|
Value: &storepb.WorkspaceSetting_BasicSetting{BasicSetting: workspaceBasicSetting},
|
2023-07-02 18:56:25 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-04-11 17:53:00 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to upsert workspace setting")
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
workspaceBasicSetting = workspaceSetting.GetBasicSetting()
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
return workspaceBasicSetting, nil
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
|
|
|
|
2023-09-17 19:20:03 +08:00
|
|
|
func grpcRequestSkipper(c echo.Context) bool {
|
|
|
|
return strings.HasPrefix(c.Request().URL.Path, "/memos.api.v2.")
|
2023-07-06 21:56:42 +08:00
|
|
|
}
|
2023-11-15 17:23:56 +08:00
|
|
|
|
2024-04-07 22:15:15 +08:00
|
|
|
func CORSMiddleware(origins []string) echo.MiddlewareFunc {
|
2024-02-04 23:48:26 +08:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
if grpcRequestSkipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := c.Request()
|
|
|
|
w := c.Response().Writer
|
|
|
|
|
2024-04-07 22:15:15 +08:00
|
|
|
requestOrigin := r.Header.Get("Origin")
|
|
|
|
if len(origins) == 0 {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", requestOrigin)
|
|
|
|
} else {
|
|
|
|
for _, origin := range origins {
|
|
|
|
if origin == requestOrigin {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 06:10:10 +08:00
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
|
2024-02-04 23:48:26 +08:00
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
|
|
|
|
// If it's preflight request, return immediately.
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-05 06:10:10 +08:00
|
|
|
return next(c)
|
2024-02-04 23:48:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|