Commit 3872736

mo khan <mo@mokhan.ca>
2025-03-28 18:13:45
feat: generate golang rpc client/server
1 parent 87cd5db
pkg/rpc/ability.pb.go
@@ -0,0 +1,194 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// versions:
+// 	protoc-gen-go v1.36.6
+// 	protoc        v3.19.6
+// source: ability.proto
+
+package rpc
+
+import (
+	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
+	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
+	reflect "reflect"
+	sync "sync"
+	unsafe "unsafe"
+)
+
+const (
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
+)
+
+type AllowRequest struct {
+	state         protoimpl.MessageState `protogen:"open.v1"`
+	Subject       string                 `protobuf:"bytes,1,opt,name=subject,proto3" json:"subject,omitempty"`
+	Permission    string                 `protobuf:"bytes,2,opt,name=permission,proto3" json:"permission,omitempty"`
+	Resource      string                 `protobuf:"bytes,3,opt,name=resource,proto3" json:"resource,omitempty"`
+	unknownFields protoimpl.UnknownFields
+	sizeCache     protoimpl.SizeCache
+}
+
+func (x *AllowRequest) Reset() {
+	*x = AllowRequest{}
+	mi := &file_ability_proto_msgTypes[0]
+	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+	ms.StoreMessageInfo(mi)
+}
+
+func (x *AllowRequest) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*AllowRequest) ProtoMessage() {}
+
+func (x *AllowRequest) ProtoReflect() protoreflect.Message {
+	mi := &file_ability_proto_msgTypes[0]
+	if x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use AllowRequest.ProtoReflect.Descriptor instead.
+func (*AllowRequest) Descriptor() ([]byte, []int) {
+	return file_ability_proto_rawDescGZIP(), []int{0}
+}
+
+func (x *AllowRequest) GetSubject() string {
+	if x != nil {
+		return x.Subject
+	}
+	return ""
+}
+
+func (x *AllowRequest) GetPermission() string {
+	if x != nil {
+		return x.Permission
+	}
+	return ""
+}
+
+func (x *AllowRequest) GetResource() string {
+	if x != nil {
+		return x.Resource
+	}
+	return ""
+}
+
+type AllowReply struct {
+	state         protoimpl.MessageState `protogen:"open.v1"`
+	Result        bool                   `protobuf:"varint,1,opt,name=result,proto3" json:"result,omitempty"`
+	unknownFields protoimpl.UnknownFields
+	sizeCache     protoimpl.SizeCache
+}
+
+func (x *AllowReply) Reset() {
+	*x = AllowReply{}
+	mi := &file_ability_proto_msgTypes[1]
+	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+	ms.StoreMessageInfo(mi)
+}
+
+func (x *AllowReply) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*AllowReply) ProtoMessage() {}
+
+func (x *AllowReply) ProtoReflect() protoreflect.Message {
+	mi := &file_ability_proto_msgTypes[1]
+	if x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use AllowReply.ProtoReflect.Descriptor instead.
+func (*AllowReply) Descriptor() ([]byte, []int) {
+	return file_ability_proto_rawDescGZIP(), []int{1}
+}
+
+func (x *AllowReply) GetResult() bool {
+	if x != nil {
+		return x.Result
+	}
+	return false
+}
+
+var File_ability_proto protoreflect.FileDescriptor
+
+const file_ability_proto_rawDesc = "" +
+	"\n" +
+	"\rability.proto\x12\tauthx.rpc\"d\n" +
+	"\fAllowRequest\x12\x18\n" +
+	"\asubject\x18\x01 \x01(\tR\asubject\x12\x1e\n" +
+	"\n" +
+	"permission\x18\x02 \x01(\tR\n" +
+	"permission\x12\x1a\n" +
+	"\bresource\x18\x03 \x01(\tR\bresource\"$\n" +
+	"\n" +
+	"AllowReply\x12\x16\n" +
+	"\x06result\x18\x01 \x01(\bR\x06result2F\n" +
+	"\aAbility\x12;\n" +
+	"\aAllowed\x12\x17.authx.rpc.AllowRequest\x1a\x15.authx.rpc.AllowReply\"\x00B!Z\x1fgitlab.com/mokhax/spike/pkg/rpcb\x06proto3"
+
+var (
+	file_ability_proto_rawDescOnce sync.Once
+	file_ability_proto_rawDescData []byte
+)
+
+func file_ability_proto_rawDescGZIP() []byte {
+	file_ability_proto_rawDescOnce.Do(func() {
+		file_ability_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_ability_proto_rawDesc), len(file_ability_proto_rawDesc)))
+	})
+	return file_ability_proto_rawDescData
+}
+
+var file_ability_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
+var file_ability_proto_goTypes = []any{
+	(*AllowRequest)(nil), // 0: authx.rpc.AllowRequest
+	(*AllowReply)(nil),   // 1: authx.rpc.AllowReply
+}
+var file_ability_proto_depIdxs = []int32{
+	0, // 0: authx.rpc.Ability.Allowed:input_type -> authx.rpc.AllowRequest
+	1, // 1: authx.rpc.Ability.Allowed:output_type -> authx.rpc.AllowReply
+	1, // [1:2] is the sub-list for method output_type
+	0, // [0:1] is the sub-list for method input_type
+	0, // [0:0] is the sub-list for extension type_name
+	0, // [0:0] is the sub-list for extension extendee
+	0, // [0:0] is the sub-list for field type_name
+}
+
+func init() { file_ability_proto_init() }
+func file_ability_proto_init() {
+	if File_ability_proto != nil {
+		return
+	}
+	type x struct{}
+	out := protoimpl.TypeBuilder{
+		File: protoimpl.DescBuilder{
+			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
+			RawDescriptor: unsafe.Slice(unsafe.StringData(file_ability_proto_rawDesc), len(file_ability_proto_rawDesc)),
+			NumEnums:      0,
+			NumMessages:   2,
+			NumExtensions: 0,
+			NumServices:   1,
+		},
+		GoTypes:           file_ability_proto_goTypes,
+		DependencyIndexes: file_ability_proto_depIdxs,
+		MessageInfos:      file_ability_proto_msgTypes,
+	}.Build()
+	File_ability_proto = out.File
+	file_ability_proto_goTypes = nil
+	file_ability_proto_depIdxs = nil
+}
pkg/rpc/ability_grpc.pb.go
@@ -0,0 +1,121 @@
+// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
+// versions:
+// - protoc-gen-go-grpc v1.5.1
+// - protoc             v3.19.6
+// source: ability.proto
+
+package rpc
+
+import (
+	context "context"
+	grpc "google.golang.org/grpc"
+	codes "google.golang.org/grpc/codes"
+	status "google.golang.org/grpc/status"
+)
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the grpc package it is being compiled against.
+// Requires gRPC-Go v1.64.0 or later.
+const _ = grpc.SupportPackageIsVersion9
+
+const (
+	Ability_Allowed_FullMethodName = "/authx.rpc.Ability/Allowed"
+)
+
+// AbilityClient is the client API for Ability service.
+//
+// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
+type AbilityClient interface {
+	Allowed(ctx context.Context, in *AllowRequest, opts ...grpc.CallOption) (*AllowReply, error)
+}
+
+type abilityClient struct {
+	cc grpc.ClientConnInterface
+}
+
+func NewAbilityClient(cc grpc.ClientConnInterface) AbilityClient {
+	return &abilityClient{cc}
+}
+
+func (c *abilityClient) Allowed(ctx context.Context, in *AllowRequest, opts ...grpc.CallOption) (*AllowReply, error) {
+	cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
+	out := new(AllowReply)
+	err := c.cc.Invoke(ctx, Ability_Allowed_FullMethodName, in, out, cOpts...)
+	if err != nil {
+		return nil, err
+	}
+	return out, nil
+}
+
+// AbilityServer is the server API for Ability service.
+// All implementations must embed UnimplementedAbilityServer
+// for forward compatibility.
+type AbilityServer interface {
+	Allowed(context.Context, *AllowRequest) (*AllowReply, error)
+	mustEmbedUnimplementedAbilityServer()
+}
+
+// UnimplementedAbilityServer must be embedded to have
+// forward compatible implementations.
+//
+// NOTE: this should be embedded by value instead of pointer to avoid a nil
+// pointer dereference when methods are called.
+type UnimplementedAbilityServer struct{}
+
+func (UnimplementedAbilityServer) Allowed(context.Context, *AllowRequest) (*AllowReply, error) {
+	return nil, status.Errorf(codes.Unimplemented, "method Allowed not implemented")
+}
+func (UnimplementedAbilityServer) mustEmbedUnimplementedAbilityServer() {}
+func (UnimplementedAbilityServer) testEmbeddedByValue()                 {}
+
+// UnsafeAbilityServer may be embedded to opt out of forward compatibility for this service.
+// Use of this interface is not recommended, as added methods to AbilityServer will
+// result in compilation errors.
+type UnsafeAbilityServer interface {
+	mustEmbedUnimplementedAbilityServer()
+}
+
+func RegisterAbilityServer(s grpc.ServiceRegistrar, srv AbilityServer) {
+	// If the following call pancis, it indicates UnimplementedAbilityServer was
+	// embedded by pointer and is nil.  This will cause panics if an
+	// unimplemented method is ever invoked, so we test this at initialization
+	// time to prevent it from happening at runtime later due to I/O.
+	if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
+		t.testEmbeddedByValue()
+	}
+	s.RegisterService(&Ability_ServiceDesc, srv)
+}
+
+func _Ability_Allowed_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+	in := new(AllowRequest)
+	if err := dec(in); err != nil {
+		return nil, err
+	}
+	if interceptor == nil {
+		return srv.(AbilityServer).Allowed(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: Ability_Allowed_FullMethodName,
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(AbilityServer).Allowed(ctx, req.(*AllowRequest))
+	}
+	return interceptor(ctx, in, info, handler)
+}
+
+// Ability_ServiceDesc is the grpc.ServiceDesc for Ability service.
+// It's only intended for direct use with grpc.RegisterService,
+// and not to be introspected or modified (even as a copy)
+var Ability_ServiceDesc = grpc.ServiceDesc{
+	ServiceName: "authx.rpc.Ability",
+	HandlerType: (*AbilityServer)(nil),
+	Methods: []grpc.MethodDesc{
+		{
+			MethodName: "Allowed",
+			Handler:    _Ability_Allowed_Handler,
+		},
+	},
+	Streams:  []grpc.StreamDesc{},
+	Metadata: "ability.proto",
+}
protos/ability.proto
@@ -1,6 +1,8 @@
 syntax = "proto3";
 
 package authx.rpc;
+option go_package = "gitlab.com/mokhax/spike/pkg/rpc";
+
 
 service Ability {
   rpc Allowed (AllowRequest) returns (AllowReply) {}
go.mod
@@ -12,6 +12,8 @@ require (
 	github.com/stretchr/testify v1.10.0
 	github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8
 	golang.org/x/oauth2 v0.28.0
+	google.golang.org/grpc v1.65.0
+	google.golang.org/protobuf v1.36.6
 )
 
 require (
@@ -23,7 +25,7 @@ require (
 	github.com/go-jose/go-jose/v3 v3.0.4 // indirect
 	github.com/go-stack/stack v1.8.1 // indirect
 	github.com/goccy/go-json v0.10.3 // indirect
-	github.com/golang/protobuf v1.5.2 // indirect
+	github.com/golang/protobuf v1.5.4 // indirect
 	github.com/google/jsonapi v1.0.0 // indirect
 	github.com/google/uuid v1.6.0 // indirect
 	github.com/kr/text v0.2.0 // indirect
@@ -38,8 +40,11 @@ require (
 	go.opentelemetry.io/otel/trace v1.29.0 // indirect
 	golang.org/x/crypto v0.36.0 // indirect
 	golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
+	golang.org/x/net v0.26.0 // indirect
 	golang.org/x/sys v0.31.0 // indirect
-	google.golang.org/protobuf v1.28.1 // indirect
+	golang.org/x/text v0.23.0 // indirect
+	google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
+	google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )
@@ -48,4 +53,6 @@ tool (
 	github.com/arthurnn/twirp-ruby/protoc-gen-twirp_ruby
 	github.com/magefile/mage
 	github.com/playwright-community/playwright-go/cmd/playwright
+	google.golang.org/grpc/cmd/protoc-gen-go-grpc
+	google.golang.org/protobuf/cmd/protoc-gen-go
 )
go.sum
@@ -23,6 +23,8 @@ github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU
 github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
 github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
 github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
+github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -88,6 +90,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
+golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
+golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
 golang.org/x/oauth2 v0.28.0 h1:CrgCKl8PPAVtLnU3c+EDw6x11699EWlsDeWNWKdIOkc=
 golang.org/x/oauth2 v0.28.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -114,16 +118,24 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
 golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
+golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
+golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
 golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 h1:1GBuWVLM/KMVUv1t1En5Gs+gFZCNd360GGb4sSxtrhU=
+google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
+google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
+google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
+google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A=
+google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA=
 google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
-google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
-google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
+google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
+google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
magefile.go
@@ -65,13 +65,16 @@ func Authzd() error {
 
 // Generate gRPC from protocal buffers
 func Protos() error {
-	outDir := "lib/authx/rpc"
 	for _, file := range x.Must(filepath.Glob("./protos/*.proto")) {
 		if err := sh.RunV(
 			"protoc",
 			"--proto_path=./protos",
-			"--ruby_out="+outDir,
-			"--twirp_ruby_out="+outDir,
+			"--go_out=pkg/rpc",
+			"--go_opt=paths=source_relative",
+			"--go-grpc_out=pkg/rpc",
+			"--go-grpc_opt=paths=source_relative",
+			"--ruby_out=lib/authx/rpc",
+			"--twirp_ruby_out=lib/authx/rpc",
 			file,
 		); err != nil {
 			return err
Makefile
@@ -1,7 +1,9 @@
 default:
 	@go install tool
-	@command -v protoc-gen-twirp_ruby
 	@command -v mage
 	@command -v playwright
+	@command -v protoc-gen-go
+	@command -v protoc-gen-go-grpc
+	@command -v protoc-gen-twirp_ruby
 	@mage -l
 	@ruby ./script/cibuild