2023-07-30 00:00:49 +08:00
|
|
|
package v2
|
2023-07-30 01:35:00 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-02-29 23:54:43 +08:00
|
|
|
"log/slog"
|
2023-12-27 08:50:02 +08:00
|
|
|
"net"
|
2023-07-30 01:35:00 +08:00
|
|
|
|
2023-10-06 19:02:40 +08:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
2023-09-17 19:20:03 +08:00
|
|
|
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
2023-07-30 01:35:00 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-12-27 08:50:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-07-30 01:35:00 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-09-15 17:57:45 +08:00
|
|
|
"google.golang.org/grpc/reflection"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
|
|
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
|
|
|
"github.com/usememos/memos/server/profile"
|
|
|
|
"github.com/usememos/memos/store"
|
2023-07-30 01:35:00 +08:00
|
|
|
)
|
|
|
|
|
2023-07-30 23:49:10 +08:00
|
|
|
type APIV2Service struct {
|
2024-01-28 07:35:42 +08:00
|
|
|
apiv2pb.UnimplementedWorkspaceServiceServer
|
2024-02-20 23:02:01 +08:00
|
|
|
apiv2pb.UnimplementedWorkspaceSettingServiceServer
|
2023-12-01 09:13:32 +08:00
|
|
|
apiv2pb.UnimplementedAuthServiceServer
|
2023-10-27 09:07:35 +08:00
|
|
|
apiv2pb.UnimplementedUserServiceServer
|
|
|
|
apiv2pb.UnimplementedMemoServiceServer
|
|
|
|
apiv2pb.UnimplementedResourceServiceServer
|
|
|
|
apiv2pb.UnimplementedTagServiceServer
|
2023-10-27 09:01:17 +08:00
|
|
|
apiv2pb.UnimplementedInboxServiceServer
|
2023-10-28 00:21:53 +08:00
|
|
|
apiv2pb.UnimplementedActivityServiceServer
|
2023-11-24 23:04:36 +08:00
|
|
|
apiv2pb.UnimplementedWebhookServiceServer
|
2024-03-13 17:31:53 +08:00
|
|
|
apiv2pb.UnimplementedLinkServiceServer
|
2024-04-13 02:55:40 +08:00
|
|
|
apiv2pb.UnimplementedStorageServiceServer
|
2024-04-13 10:50:25 +08:00
|
|
|
apiv2pb.UnimplementedIdentityProviderServiceServer
|
2023-10-27 09:01:17 +08:00
|
|
|
|
2023-07-30 23:49:10 +08:00
|
|
|
Secret string
|
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
|
|
|
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcServerPort int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store, grpcServerPort int) *APIV2Service {
|
2023-09-17 19:20:03 +08:00
|
|
|
grpc.EnableTracing = true
|
2023-07-31 20:55:40 +08:00
|
|
|
authProvider := NewGRPCAuthInterceptor(store, secret)
|
2023-07-30 23:49:10 +08:00
|
|
|
grpcServer := grpc.NewServer(
|
|
|
|
grpc.ChainUnaryInterceptor(
|
2024-02-29 23:54:43 +08:00
|
|
|
NewLoggerInterceptor().LoggerInterceptor,
|
2023-07-30 23:49:10 +08:00
|
|
|
authProvider.AuthenticationInterceptor,
|
|
|
|
),
|
|
|
|
)
|
2023-10-27 09:01:17 +08:00
|
|
|
apiv2Service := &APIV2Service{
|
|
|
|
Secret: secret,
|
|
|
|
Profile: profile,
|
|
|
|
Store: store,
|
|
|
|
grpcServer: grpcServer,
|
|
|
|
grpcServerPort: grpcServerPort,
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:35:42 +08:00
|
|
|
apiv2pb.RegisterWorkspaceServiceServer(grpcServer, apiv2Service)
|
2024-02-20 23:02:01 +08:00
|
|
|
apiv2pb.RegisterWorkspaceSettingServiceServer(grpcServer, apiv2Service)
|
2023-12-01 09:13:32 +08:00
|
|
|
apiv2pb.RegisterAuthServiceServer(grpcServer, apiv2Service)
|
2023-10-27 09:07:35 +08:00
|
|
|
apiv2pb.RegisterUserServiceServer(grpcServer, apiv2Service)
|
|
|
|
apiv2pb.RegisterMemoServiceServer(grpcServer, apiv2Service)
|
|
|
|
apiv2pb.RegisterTagServiceServer(grpcServer, apiv2Service)
|
|
|
|
apiv2pb.RegisterResourceServiceServer(grpcServer, apiv2Service)
|
2023-10-27 09:01:17 +08:00
|
|
|
apiv2pb.RegisterInboxServiceServer(grpcServer, apiv2Service)
|
2023-10-28 00:21:53 +08:00
|
|
|
apiv2pb.RegisterActivityServiceServer(grpcServer, apiv2Service)
|
2023-11-24 23:04:36 +08:00
|
|
|
apiv2pb.RegisterWebhookServiceServer(grpcServer, apiv2Service)
|
2024-03-13 17:31:53 +08:00
|
|
|
apiv2pb.RegisterLinkServiceServer(grpcServer, apiv2Service)
|
2024-04-13 02:55:40 +08:00
|
|
|
apiv2pb.RegisterStorageServiceServer(grpcServer, apiv2Service)
|
2024-04-13 10:50:25 +08:00
|
|
|
apiv2pb.RegisterIdentityProviderServiceServer(grpcServer, apiv2Service)
|
2023-09-15 17:57:45 +08:00
|
|
|
reflection.Register(grpcServer)
|
2023-07-30 23:49:10 +08:00
|
|
|
|
2023-10-27 09:01:17 +08:00
|
|
|
return apiv2Service
|
2023-07-30 23:49:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APIV2Service) GetGRPCServer() *grpc.Server {
|
|
|
|
return s.grpcServer
|
2023-07-30 01:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterGateway registers the gRPC-Gateway with the given Echo instance.
|
2023-07-30 23:49:10 +08:00
|
|
|
func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error {
|
2023-07-30 01:35:00 +08:00
|
|
|
// Create a client connection to the gRPC Server we just started.
|
|
|
|
// This is where the gRPC-Gateway proxies the requests.
|
|
|
|
conn, err := grpc.DialContext(
|
|
|
|
ctx,
|
2023-07-30 23:49:10 +08:00
|
|
|
fmt.Sprintf(":%d", s.grpcServerPort),
|
2023-07-30 01:35:00 +08:00
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-06 19:02:40 +08:00
|
|
|
gwMux := runtime.NewServeMux()
|
2024-01-28 07:35:42 +08:00
|
|
|
if err := apiv2pb.RegisterWorkspaceServiceHandler(context.Background(), gwMux, conn); err != nil {
|
2023-08-05 21:30:23 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-02-20 23:02:01 +08:00
|
|
|
if err := apiv2pb.RegisterWorkspaceSettingServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-01 09:13:32 +08:00
|
|
|
if err := apiv2pb.RegisterAuthServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-30 01:35:00 +08:00
|
|
|
if err := apiv2pb.RegisterUserServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-05 19:51:32 +08:00
|
|
|
if err := apiv2pb.RegisterMemoServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-30 01:35:00 +08:00
|
|
|
if err := apiv2pb.RegisterTagServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-16 00:11:07 +08:00
|
|
|
if err := apiv2pb.RegisterResourceServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-27 09:01:17 +08:00
|
|
|
if err := apiv2pb.RegisterInboxServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-28 00:21:53 +08:00
|
|
|
if err := apiv2pb.RegisterActivityServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-24 23:04:36 +08:00
|
|
|
if err := apiv2pb.RegisterWebhookServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-13 17:31:53 +08:00
|
|
|
if err := apiv2pb.RegisterLinkServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-13 02:55:40 +08:00
|
|
|
if err := apiv2pb.RegisterStorageServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-13 10:50:25 +08:00
|
|
|
if err := apiv2pb.RegisterIdentityProviderServiceHandler(context.Background(), gwMux, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-30 01:35:00 +08:00
|
|
|
e.Any("/api/v2/*", echo.WrapHandler(gwMux))
|
|
|
|
|
2023-09-17 19:20:03 +08:00
|
|
|
// GRPC web proxy.
|
|
|
|
options := []grpcweb.Option{
|
|
|
|
grpcweb.WithCorsForRegisteredEndpointsOnly(false),
|
|
|
|
grpcweb.WithOriginFunc(func(origin string) bool {
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
wrappedGrpc := grpcweb.WrapServer(s.grpcServer, options...)
|
|
|
|
e.Any("/memos.api.v2.*", echo.WrapHandler(wrappedGrpc))
|
|
|
|
|
2023-12-27 08:50:02 +08:00
|
|
|
// Start gRPC server.
|
|
|
|
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Profile.Addr, s.grpcServerPort))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to start gRPC server")
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
if err := s.grpcServer.Serve(listen); err != nil {
|
2024-02-29 23:54:43 +08:00
|
|
|
slog.Error("failed to start gRPC server", err)
|
2023-12-27 08:50:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-07-30 01:35:00 +08:00
|
|
|
return nil
|
|
|
|
}
|