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"
|
2024-04-18 21:41:00 +08:00
|
|
|
"log/slog"
|
|
|
|
"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"
|
2024-05-26 21:39:34 +08:00
|
|
|
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
2023-07-30 00:00:49 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-05-26 21:38:59 +08:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-01-02 23:18:12 +08:00
|
|
|
"github.com/pkg/errors"
|
2024-04-18 21:41:00 +08:00
|
|
|
"github.com/soheilhy/cmux"
|
|
|
|
"google.golang.org/grpc"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2024-04-11 17:53:00 +08:00
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2024-05-01 10:28:32 +08:00
|
|
|
apiv1 "github.com/usememos/memos/server/router/api/v1"
|
|
|
|
"github.com/usememos/memos/server/router/frontend"
|
|
|
|
"github.com/usememos/memos/server/router/rss"
|
2024-05-02 22:08:45 +08:00
|
|
|
s3objectpresigner "github.com/usememos/memos/server/service/s3_object_presigner"
|
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-06-17 21:25:46 +08:00
|
|
|
Secret string
|
2023-03-05 21:42:32 +08:00
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
2024-04-18 21:41:00 +08:00
|
|
|
|
|
|
|
echoServer *echo.Echo
|
|
|
|
grpcServer *grpc.Server
|
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) {
|
2023-02-03 10:30:18 +08:00
|
|
|
s := &Server{
|
2023-06-17 21:25:46 +08:00
|
|
|
Store: store,
|
2023-02-03 10:30:18 +08:00
|
|
|
Profile: profile,
|
2023-11-13 22:12:25 +08:00
|
|
|
}
|
|
|
|
|
2024-04-18 21:41:00 +08:00
|
|
|
echoServer := echo.New()
|
|
|
|
echoServer.Debug = true
|
|
|
|
echoServer.HideBanner = true
|
|
|
|
echoServer.HidePort = true
|
2024-05-26 21:38:59 +08:00
|
|
|
echoServer.Use(middleware.Recover())
|
2024-04-18 21:41:00 +08:00
|
|
|
s.echoServer = echoServer
|
|
|
|
|
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
|
|
|
}
|
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.
|
2024-04-18 21:41:00 +08:00
|
|
|
echoServer.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-05-03 07:16:07 +08:00
|
|
|
// Serve frontend resources.
|
|
|
|
frontend.NewFrontendService(profile, store).Serve(ctx, echoServer)
|
2024-02-29 23:54:43 +08:00
|
|
|
|
2024-04-18 21:41:00 +08:00
|
|
|
rootGroup := echoServer.Group("")
|
2024-04-13 12:07:53 +08:00
|
|
|
|
2024-04-13 12:10:57 +08:00
|
|
|
// Create and register RSS routes.
|
2024-04-13 12:07:53 +08:00
|
|
|
rss.NewRSSService(s.Profile, s.Store).RegisterRoutes(rootGroup)
|
|
|
|
|
2024-05-07 22:10:27 +08:00
|
|
|
grpcServer := grpc.NewServer(
|
|
|
|
// Override the maximum receiving message size to 100M for uploading large resources.
|
|
|
|
grpc.MaxRecvMsgSize(100*1024*1024),
|
|
|
|
grpc.ChainUnaryInterceptor(
|
|
|
|
apiv1.NewLoggerInterceptor().LoggerInterceptor,
|
2024-05-26 21:38:59 +08:00
|
|
|
grpc_recovery.UnaryServerInterceptor(),
|
2024-05-07 22:10:27 +08:00
|
|
|
apiv1.NewGRPCAuthInterceptor(store, secret).AuthenticationInterceptor,
|
|
|
|
))
|
2024-04-18 21:41:00 +08:00
|
|
|
s.grpcServer = grpcServer
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
apiV1Service := apiv1.NewAPIV1Service(s.Secret, profile, store, grpcServer)
|
|
|
|
// Register gRPC gateway as api v1.
|
|
|
|
if err := apiV1Service.RegisterGateway(ctx, echoServer); 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
|
|
|
}
|
|
|
|
|
2024-04-18 23:34:35 +08:00
|
|
|
func (s *Server) Start(ctx context.Context) error {
|
2024-05-11 06:50:22 +08:00
|
|
|
address := fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port)
|
2024-04-18 21:41:00 +08:00
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to listen")
|
|
|
|
}
|
|
|
|
|
|
|
|
muxServer := cmux.New(listener)
|
|
|
|
go func() {
|
2024-04-30 10:18:04 +08:00
|
|
|
grpcListener := muxServer.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
|
2024-04-18 21:41:00 +08:00
|
|
|
if err := s.grpcServer.Serve(grpcListener); err != nil {
|
2024-07-27 19:24:37 +08:00
|
|
|
slog.Error("failed to serve gRPC", "error", err)
|
2024-04-18 21:41:00 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
2024-06-21 18:16:35 +08:00
|
|
|
httpListener := muxServer.Match(cmux.HTTP1Fast(http.MethodPatch))
|
2024-04-18 21:41:00 +08:00
|
|
|
s.echoServer.Listener = httpListener
|
|
|
|
if err := s.echoServer.Start(address); err != nil {
|
2024-07-27 19:24:37 +08:00
|
|
|
slog.Error("failed to start echo server", "error", err)
|
2024-04-18 21:41:00 +08:00
|
|
|
}
|
|
|
|
}()
|
2024-04-30 22:06:34 +08:00
|
|
|
go func() {
|
|
|
|
if err := muxServer.Serve(); err != nil {
|
2024-07-27 19:24:37 +08:00
|
|
|
slog.Error("mux server listen error", "error", err)
|
2024-04-30 22:06:34 +08:00
|
|
|
}
|
|
|
|
}()
|
2024-04-18 23:34:35 +08:00
|
|
|
s.StartBackgroundRunners(ctx)
|
2024-04-18 21:41:00 +08:00
|
|
|
|
2024-04-30 22:06:34 +08:00
|
|
|
return nil
|
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()
|
|
|
|
|
2024-04-29 08:00:37 +08:00
|
|
|
// Shutdown echo server.
|
2024-04-18 21:41:00 +08:00
|
|
|
if err := s.echoServer.Shutdown(ctx); err != nil {
|
2023-02-03 10:30:18 +08:00
|
|
|
fmt.Printf("failed to shutdown server, error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2024-04-29 08:00:37 +08:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2024-04-18 23:34:35 +08:00
|
|
|
func (s *Server) StartBackgroundRunners(ctx context.Context) {
|
2024-04-18 21:41:00 +08:00
|
|
|
go versionchecker.NewVersionChecker(s.Store, s.Profile).Start(ctx)
|
2024-05-02 22:08:45 +08:00
|
|
|
go s3objectpresigner.NewS3ObjectPresigner(s.Store).Start(ctx)
|
2023-04-17 21:34:59 +08:00
|
|
|
}
|
|
|
|
|
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.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 {
|
2024-04-17 08:56:52 +08:00
|
|
|
workspaceSetting, err := s.Store.UpsertWorkspaceSetting(ctx, &storepb.WorkspaceSetting{
|
2024-05-13 20:03:04 +08:00
|
|
|
Key: storepb.WorkspaceSettingKey_BASIC,
|
2024-04-11 17:53:00 +08:00
|
|
|
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
|
|
|
}
|