Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

3.0 KB
Raw Blame
package api

import (
	"net/http"

	"connectrpc.com/connect"
	"github.com/pkg/errors"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"github.com/usememos/memos/proto/gen/api/apiconnect"
)

// ConnectServiceHandler wraps APIService to implement Connect handler interfaces.
// It adapts the existing gRPC service implementations to work with Connect's
// request/response wrapper types.
//
// This wrapper pattern allows us to:
// - Reuse existing gRPC service implementations
// - Support both native gRPC and Connect protocols
// - Maintain a single source of truth for business logic.
type ConnectServiceHandler struct {
	*APIService
}

// NewConnectServiceHandler creates a new Connect service handler.
func NewConnectServiceHandler(svc *APIService) *ConnectServiceHandler {
	return &ConnectServiceHandler{APIService: svc}
}

// RegisterConnectHandlers registers all Connect service handlers on the given mux.
func (s *ConnectServiceHandler) RegisterConnectHandlers(mux *http.ServeMux, opts ...connect.HandlerOption) {
	// Register all service handlers
	handlers := []struct {
		path    string
		handler http.Handler
	}{
		wrap(apiconnect.NewInstanceServiceHandler(s, opts...)),
		wrap(apiconnect.NewAuthServiceHandler(s, opts...)),
		wrap(apiconnect.NewUserServiceHandler(s, opts...)),
		wrap(apiconnect.NewMemoServiceHandler(s, opts...)),
		wrap(apiconnect.NewSpaceServiceHandler(s, opts...)),
		wrap(apiconnect.NewAttachmentServiceHandler(s, opts...)),
		wrap(apiconnect.NewAIServiceHandler(s, opts...)),
		wrap(apiconnect.NewIdentityProviderServiceHandler(s, opts...)),
	}

	for _, h := range handlers {
		mux.Handle(h.path, h.handler)
	}
}

// wrap converts (path, handler) return value to a struct for cleaner iteration.
func wrap(path string, handler http.Handler) struct {
	path    string
	handler http.Handler
} {
	return struct {
		path    string
		handler http.Handler
	}{path, handler}
}

// convertGRPCError converts gRPC status errors to Connect errors, carrying
// the status details across and, for rate-limit refusals, the HTTP header
// fields a generic client reads.
func convertGRPCError(err error) error {
	if err == nil {
		return nil
	}
	st, ok := status.FromError(err)
	if !ok {
		return connect.NewError(connect.CodeInternal, err)
	}
	connectErr := connect.NewError(grpcCodeToConnectCode(st.Code()), errors.New(st.Message()))
	for _, detail := range st.Proto().GetDetails() {
		message, unmarshalErr := detail.UnmarshalNew()
		if unmarshalErr != nil {
			continue
		}
		if connectDetail, detailErr := connect.NewErrorDetail(message); detailErr == nil {
			connectErr.AddDetail(connectDetail)
		}
	}
	for key, values := range rateLimitHTTPHeaders(st) {
		for _, value := range values {
			connectErr.Meta().Add(key, value)
		}
	}
	return connectErr
}

// grpcCodeToConnectCode converts gRPC status codes to Connect error codes.
// gRPC and Connect use the same error code semantics, so this is a direct cast.
// See: https://connectrpc.com/docs/protocol/#error-codes
func grpcCodeToConnectCode(code codes.Code) connect.Code {
	return connect.Code(code)
}