v2.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package v2
  2. import (
  3. "context"
  4. "fmt"
  5. grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  6. "github.com/labstack/echo/v4"
  7. apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
  8. "github.com/usememos/memos/server/profile"
  9. "github.com/usememos/memos/store"
  10. "google.golang.org/grpc"
  11. "google.golang.org/grpc/credentials/insecure"
  12. "google.golang.org/grpc/reflection"
  13. )
  14. type APIV2Service struct {
  15. Secret string
  16. Profile *profile.Profile
  17. Store *store.Store
  18. grpcServer *grpc.Server
  19. grpcServerPort int
  20. }
  21. func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store, grpcServerPort int) *APIV2Service {
  22. authProvider := NewGRPCAuthInterceptor(store, secret)
  23. grpcServer := grpc.NewServer(
  24. grpc.ChainUnaryInterceptor(
  25. authProvider.AuthenticationInterceptor,
  26. ),
  27. )
  28. apiv2pb.RegisterSystemServiceServer(grpcServer, NewSystemService(profile, store))
  29. apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store, secret))
  30. apiv2pb.RegisterMemoServiceServer(grpcServer, NewMemoService(store))
  31. apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store))
  32. apiv2pb.RegisterResourceServiceServer(grpcServer, NewResourceService(store))
  33. reflection.Register(grpcServer)
  34. return &APIV2Service{
  35. Secret: secret,
  36. Profile: profile,
  37. Store: store,
  38. grpcServer: grpcServer,
  39. grpcServerPort: grpcServerPort,
  40. }
  41. }
  42. func (s *APIV2Service) GetGRPCServer() *grpc.Server {
  43. return s.grpcServer
  44. }
  45. // RegisterGateway registers the gRPC-Gateway with the given Echo instance.
  46. func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error {
  47. // Create a client connection to the gRPC Server we just started.
  48. // This is where the gRPC-Gateway proxies the requests.
  49. conn, err := grpc.DialContext(
  50. ctx,
  51. fmt.Sprintf(":%d", s.grpcServerPort),
  52. grpc.WithTransportCredentials(insecure.NewCredentials()),
  53. )
  54. if err != nil {
  55. return err
  56. }
  57. gwMux := grpcRuntime.NewServeMux()
  58. if err := apiv2pb.RegisterSystemServiceHandler(context.Background(), gwMux, conn); err != nil {
  59. return err
  60. }
  61. if err := apiv2pb.RegisterUserServiceHandler(context.Background(), gwMux, conn); err != nil {
  62. return err
  63. }
  64. if err := apiv2pb.RegisterMemoServiceHandler(context.Background(), gwMux, conn); err != nil {
  65. return err
  66. }
  67. if err := apiv2pb.RegisterTagServiceHandler(context.Background(), gwMux, conn); err != nil {
  68. return err
  69. }
  70. if err := apiv2pb.RegisterResourceServiceHandler(context.Background(), gwMux, conn); err != nil {
  71. return err
  72. }
  73. e.Any("/api/v2/*", echo.WrapHandler(gwMux))
  74. return nil
  75. }