memos/server/server.go

178 lines
5.1 KiB
Go
Raw Normal View History

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"
"net/http"
2022-02-04 18:54:24 +08:00
"time"
2022-02-03 15:32:03 +08:00
"github.com/google/uuid"
2024-08-20 08:25:34 +08:00
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/labstack/echo/v4"
"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
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-09-04 23:31:28 +08:00
"github.com/usememos/memos/server/runner/memoproperty"
"github.com/usememos/memos/server/runner/s3presign"
2024-08-20 08:07:48 +08:00
"github.com/usememos/memos/server/runner/version"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/store"
2022-02-03 15:32:03 +08:00
)
type Server struct {
Secret string
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
}
func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store) (*Server, error) {
2023-02-03 10:30:18 +08:00
s := &Server{
Store: store,
2023-02-03 10:30:18 +08:00
Profile: profile,
}
2024-04-18 21:41:00 +08:00
echoServer := echo.New()
echoServer.Debug = true
echoServer.HideBanner = true
echoServer.HidePort = true
echoServer.Use(middleware.Recover())
2024-04-18 21:41:00 +08:00
s.echoServer = echoServer
workspaceBasicSetting, err := s.getOrUpsertWorkspaceBasicSetting(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get workspace basic setting")
}
2022-02-03 15:32:03 +08:00
secret := "usememos"
2022-05-02 09:40:25 +08:00
if profile.Mode == "prod" {
secret = workspaceBasicSetting.SecretKey
2022-03-29 07:30:29 +08:00
}
s.Secret = secret
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-08-20 08:25:34 +08:00
grpcrecovery.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")
}
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 {
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 {
2024-08-28 22:53:57 +08:00
slog.Error("failed to shutdown server", slog.String("error", err.Error()))
2023-02-03 10:30:18 +08:00
}
2024-04-29 08:00:37 +08:00
// Close database connection.
if err := s.Store.Close(); err != nil {
2024-08-28 22:53:57 +08:00
slog.Error("failed to close database", slog.String("error", err.Error()))
2023-02-03 10:30:18 +08:00
}
2024-08-28 22:53:57 +08:00
slog.Info("memos stopped properly")
2023-02-03 10:30:18 +08:00
}
2024-04-18 23:34:35 +08:00
func (s *Server) StartBackgroundRunners(ctx context.Context) {
2024-08-20 08:07:48 +08:00
s3presignRunner := s3presign.NewRunner(s.Store)
s3presignRunner.RunOnce(ctx)
versionRunner := version.NewRunner(s.Store, s.Profile)
versionRunner.RunOnce(ctx)
memopropertyRunner := memoproperty.NewRunner(s.Store)
memopropertyRunner.RunOnce(ctx)
go s3presignRunner.Run(ctx)
go versionRunner.Run(ctx)
go memopropertyRunner.Run(ctx)
}
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 {
return nil, errors.Wrap(err, "failed to get workspace basic setting")
}
modified := false
if workspaceBasicSetting.SecretKey == "" {
workspaceBasicSetting.SecretKey = uuid.NewString()
modified = true
}
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,
Value: &storepb.WorkspaceSetting_BasicSetting{BasicSetting: workspaceBasicSetting},
})
if err != nil {
return nil, errors.Wrap(err, "failed to upsert workspace setting")
}
workspaceBasicSetting = workspaceSetting.GetBasicSetting()
}
return workspaceBasicSetting, nil
}