v1.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package v1
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  6. "github.com/improbable-eng/grpc-web/go/grpcweb"
  7. "github.com/labstack/echo/v4"
  8. "github.com/labstack/echo/v4/middleware"
  9. "google.golang.org/grpc"
  10. "google.golang.org/grpc/credentials/insecure"
  11. "google.golang.org/grpc/reflection"
  12. v1pb "github.com/usememos/memos/proto/gen/api/v1"
  13. "github.com/usememos/memos/server/profile"
  14. "github.com/usememos/memos/store"
  15. )
  16. type APIV1Service struct {
  17. v1pb.UnimplementedWorkspaceServiceServer
  18. v1pb.UnimplementedWorkspaceSettingServiceServer
  19. v1pb.UnimplementedAuthServiceServer
  20. v1pb.UnimplementedUserServiceServer
  21. v1pb.UnimplementedMemoServiceServer
  22. v1pb.UnimplementedResourceServiceServer
  23. v1pb.UnimplementedInboxServiceServer
  24. v1pb.UnimplementedActivityServiceServer
  25. v1pb.UnimplementedWebhookServiceServer
  26. v1pb.UnimplementedMarkdownServiceServer
  27. v1pb.UnimplementedIdentityProviderServiceServer
  28. Secret string
  29. Profile *profile.Profile
  30. Store *store.Store
  31. grpcServer *grpc.Server
  32. }
  33. func NewAPIV1Service(secret string, profile *profile.Profile, store *store.Store, grpcServer *grpc.Server) *APIV1Service {
  34. grpc.EnableTracing = true
  35. apiv1Service := &APIV1Service{
  36. Secret: secret,
  37. Profile: profile,
  38. Store: store,
  39. grpcServer: grpcServer,
  40. }
  41. v1pb.RegisterWorkspaceServiceServer(grpcServer, apiv1Service)
  42. v1pb.RegisterWorkspaceSettingServiceServer(grpcServer, apiv1Service)
  43. v1pb.RegisterAuthServiceServer(grpcServer, apiv1Service)
  44. v1pb.RegisterUserServiceServer(grpcServer, apiv1Service)
  45. v1pb.RegisterMemoServiceServer(grpcServer, apiv1Service)
  46. v1pb.RegisterResourceServiceServer(grpcServer, apiv1Service)
  47. v1pb.RegisterInboxServiceServer(grpcServer, apiv1Service)
  48. v1pb.RegisterActivityServiceServer(grpcServer, apiv1Service)
  49. v1pb.RegisterWebhookServiceServer(grpcServer, apiv1Service)
  50. v1pb.RegisterMarkdownServiceServer(grpcServer, apiv1Service)
  51. v1pb.RegisterIdentityProviderServiceServer(grpcServer, apiv1Service)
  52. reflection.Register(grpcServer)
  53. return apiv1Service
  54. }
  55. // RegisterGateway registers the gRPC-Gateway with the given Echo instance.
  56. func (s *APIV1Service) RegisterGateway(ctx context.Context, echoServer *echo.Echo) error {
  57. conn, err := grpc.NewClient(
  58. fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port),
  59. grpc.WithTransportCredentials(insecure.NewCredentials()),
  60. grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)),
  61. )
  62. if err != nil {
  63. return err
  64. }
  65. gwMux := runtime.NewServeMux()
  66. if err := v1pb.RegisterWorkspaceServiceHandler(ctx, gwMux, conn); err != nil {
  67. return err
  68. }
  69. if err := v1pb.RegisterWorkspaceSettingServiceHandler(ctx, gwMux, conn); err != nil {
  70. return err
  71. }
  72. if err := v1pb.RegisterAuthServiceHandler(ctx, gwMux, conn); err != nil {
  73. return err
  74. }
  75. if err := v1pb.RegisterUserServiceHandler(ctx, gwMux, conn); err != nil {
  76. return err
  77. }
  78. if err := v1pb.RegisterMemoServiceHandler(ctx, gwMux, conn); err != nil {
  79. return err
  80. }
  81. if err := v1pb.RegisterResourceServiceHandler(ctx, gwMux, conn); err != nil {
  82. return err
  83. }
  84. if err := v1pb.RegisterInboxServiceHandler(ctx, gwMux, conn); err != nil {
  85. return err
  86. }
  87. if err := v1pb.RegisterActivityServiceHandler(ctx, gwMux, conn); err != nil {
  88. return err
  89. }
  90. if err := v1pb.RegisterWebhookServiceHandler(ctx, gwMux, conn); err != nil {
  91. return err
  92. }
  93. if err := v1pb.RegisterMarkdownServiceHandler(ctx, gwMux, conn); err != nil {
  94. return err
  95. }
  96. if err := v1pb.RegisterIdentityProviderServiceHandler(ctx, gwMux, conn); err != nil {
  97. return err
  98. }
  99. gwGroup := echoServer.Group("")
  100. gwGroup.Use(middleware.CORS())
  101. handler := echo.WrapHandler(gwMux)
  102. gwGroup.Any("/api/v1/*", handler)
  103. gwGroup.Any("/file/*", handler)
  104. // GRPC web proxy.
  105. options := []grpcweb.Option{
  106. grpcweb.WithCorsForRegisteredEndpointsOnly(false),
  107. grpcweb.WithOriginFunc(func(_ string) bool {
  108. return true
  109. }),
  110. }
  111. wrappedGrpc := grpcweb.WrapServer(s.grpcServer, options...)
  112. echoServer.Any("/memos.api.v1.*", echo.WrapHandler(wrappedGrpc))
  113. return nil
  114. }