mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: implement part of tag service (#2051)
* feat: add grpc gateway tempalte * chore: update * chore: move directory * chore: update
This commit is contained in:
33
api/v2/gw_mux.go
Normal file
33
api/v2/gw_mux.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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))
|
||||
}
|
46
api/v2/tag_service.go
Normal file
46
api/v2/tag_service.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
||||
"github.com/usememos/memos/store"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type TagService struct {
|
||||
apiv2pb.UnimplementedTagServiceServer
|
||||
|
||||
Store *store.Store
|
||||
}
|
||||
|
||||
// NewTagService creates a new TagService.
|
||||
func NewTagService(store *store.Store) *TagService {
|
||||
return &TagService{
|
||||
Store: store,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TagService) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequest) (*apiv2pb.ListTagsResponse, error) {
|
||||
tags, err := s.Store.ListTags(ctx, &store.FindTag{
|
||||
CreatorID: int(request.CreatorId),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to list tags: %v", err)
|
||||
}
|
||||
|
||||
response := &apiv2pb.ListTagsResponse{}
|
||||
for _, tag := range tags {
|
||||
response.Tags = append(response.Tags, convertTagFromStore(tag))
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func convertTagFromStore(tag *store.Tag) *apiv2pb.Tag {
|
||||
return &apiv2pb.Tag{
|
||||
Name: tag.Name,
|
||||
CreatorId: int32(tag.CreatorID),
|
||||
}
|
||||
}
|
1
api/v2/v2.go
Normal file
1
api/v2/v2.go
Normal file
@@ -0,0 +1 @@
|
||||
package v2
|
Reference in New Issue
Block a user