mirror of
https://github.com/usememos/memos.git
synced 2025-01-10 22:33:48 +08:00
89ba2a6540
* feat: add grpc gateway tempalte * chore: update * chore: move directory * chore: update
33 lines
910 B
Go
33 lines
910 B
Go
package v2
|
|
|
|
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"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
// RegisterGateway registers the gRPC-Gateway with the given Echo instance.
|
|
func RegisterGateway(ctx context.Context, e *echo.Echo, grpcServerPort int) {
|
|
// 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,
|
|
fmt.Sprintf(":%d", grpcServerPort),
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
gwMux := grpcRuntime.NewServeMux()
|
|
err = apiv2pb.RegisterTagServiceHandler(context.Background(), gwMux, conn)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
e.Any("/api/v2/*", echo.WrapHandler(gwMux))
|
|
}
|