2023-07-30 00:00:49 +08:00
|
|
|
package v2
|
2023-07-30 01:35:00 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
2023-07-30 23:49:10 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2023-07-30 01:35:00 +08:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
2023-07-30 23:49:10 +08:00
|
|
|
type APIV2Service struct {
|
|
|
|
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-07-31 20:55:40 +08:00
|
|
|
authProvider := NewGRPCAuthInterceptor(store, secret)
|
2023-07-30 23:49:10 +08:00
|
|
|
grpcServer := grpc.NewServer(
|
|
|
|
grpc.ChainUnaryInterceptor(
|
|
|
|
authProvider.AuthenticationInterceptor,
|
|
|
|
),
|
|
|
|
)
|
2023-08-05 21:30:23 +08:00
|
|
|
apiv2pb.RegisterSystemServiceServer(grpcServer, NewSystemService(profile, store))
|
2023-09-14 20:16:17 +08:00
|
|
|
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store, secret))
|
2023-08-05 19:51:32 +08:00
|
|
|
apiv2pb.RegisterMemoServiceServer(grpcServer, NewMemoService(store))
|
2023-07-30 01:35:00 +08:00
|
|
|
apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store))
|
2023-07-30 23:49:10 +08:00
|
|
|
|
|
|
|
return &APIV2Service{
|
|
|
|
Secret: secret,
|
|
|
|
Profile: profile,
|
|
|
|
Store: store,
|
|
|
|
grpcServer: grpcServer,
|
|
|
|
grpcServerPort: grpcServerPort,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
gwMux := grpcRuntime.NewServeMux()
|
2023-08-05 21:30:23 +08:00
|
|
|
if err := apiv2pb.RegisterSystemServiceHandler(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
|
|
|
|
}
|
|
|
|
e.Any("/api/v2/*", echo.WrapHandler(gwMux))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|