Added grpc web service

This commit is contained in:
2024-11-17 11:07:26 -05:00
parent 1f1cd68192
commit 1ad11d7e08
10 changed files with 590 additions and 30 deletions

306
grpc/imgurl.pb.go Normal file
View File

@@ -0,0 +1,306 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.34.1
// protoc v5.28.3
// source: grpc/imgurl.proto
package grpc
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
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 Format int32
const (
Format_JPG Format = 1
Format_PNG Format = 2
Format_BMP Format = 3
Format_WEBP Format = 4
Format_GIF Format = 5
Format_ICO Format = 6
)
// Enum value maps for Format.
var (
Format_name = map[int32]string{
1: "JPG",
2: "PNG",
3: "BMP",
4: "WEBP",
5: "GIF",
6: "ICO",
}
Format_value = map[string]int32{
"JPG": 1,
"PNG": 2,
"BMP": 3,
"WEBP": 4,
"GIF": 5,
"ICO": 6,
}
)
func (x Format) Enum() *Format {
p := new(Format)
*p = x
return p
}
func (x Format) String() string {
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
func (Format) Descriptor() protoreflect.EnumDescriptor {
return file_grpc_imgurl_proto_enumTypes[0].Descriptor()
}
func (Format) Type() protoreflect.EnumType {
return &file_grpc_imgurl_proto_enumTypes[0]
}
func (x Format) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x)
}
// Deprecated: Do not use.
func (x *Format) UnmarshalJSON(b []byte) error {
num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b)
if err != nil {
return err
}
*x = Format(num)
return nil
}
// Deprecated: Use Format.Descriptor instead.
func (Format) EnumDescriptor() ([]byte, []int) {
return file_grpc_imgurl_proto_rawDescGZIP(), []int{0}
}
type UrlRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Image *string `protobuf:"bytes,1,req,name=image" json:"image,omitempty"`
Params []string `protobuf:"bytes,2,rep,name=params" json:"params,omitempty"`
Format *Format `protobuf:"varint,3,opt,name=format,enum=Format" json:"format,omitempty"`
}
func (x *UrlRequest) Reset() {
*x = UrlRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_grpc_imgurl_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UrlRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UrlRequest) ProtoMessage() {}
func (x *UrlRequest) ProtoReflect() protoreflect.Message {
mi := &file_grpc_imgurl_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UrlRequest.ProtoReflect.Descriptor instead.
func (*UrlRequest) Descriptor() ([]byte, []int) {
return file_grpc_imgurl_proto_rawDescGZIP(), []int{0}
}
func (x *UrlRequest) GetImage() string {
if x != nil && x.Image != nil {
return *x.Image
}
return ""
}
func (x *UrlRequest) GetParams() []string {
if x != nil {
return x.Params
}
return nil
}
func (x *UrlRequest) GetFormat() Format {
if x != nil && x.Format != nil {
return *x.Format
}
return Format_JPG
}
type UrlResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Url *string `protobuf:"bytes,1,req,name=url" json:"url,omitempty"`
}
func (x *UrlResponse) Reset() {
*x = UrlResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_grpc_imgurl_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UrlResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UrlResponse) ProtoMessage() {}
func (x *UrlResponse) ProtoReflect() protoreflect.Message {
mi := &file_grpc_imgurl_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UrlResponse.ProtoReflect.Descriptor instead.
func (*UrlResponse) Descriptor() ([]byte, []int) {
return file_grpc_imgurl_proto_rawDescGZIP(), []int{1}
}
func (x *UrlResponse) GetUrl() string {
if x != nil && x.Url != nil {
return *x.Url
}
return ""
}
var File_grpc_imgurl_proto protoreflect.FileDescriptor
var file_grpc_imgurl_proto_rawDesc = []byte{
0x0a, 0x11, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x69, 0x6d, 0x67, 0x75, 0x72, 0x6c, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0x5b, 0x0a, 0x0a, 0x55, 0x72, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x02, 0x28, 0x09,
0x52, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12,
0x1f, 0x0a, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32,
0x07, 0x2e, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
0x22, 0x1f, 0x0a, 0x0b, 0x55, 0x72, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12,
0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x02, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72,
0x6c, 0x2a, 0x3f, 0x0a, 0x06, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x07, 0x0a, 0x03, 0x4a,
0x50, 0x47, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x50, 0x4e, 0x47, 0x10, 0x02, 0x12, 0x07, 0x0a,
0x03, 0x42, 0x4d, 0x50, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x57, 0x45, 0x42, 0x50, 0x10, 0x04,
0x12, 0x07, 0x0a, 0x03, 0x47, 0x49, 0x46, 0x10, 0x05, 0x12, 0x07, 0x0a, 0x03, 0x49, 0x43, 0x4f,
0x10, 0x06, 0x32, 0x34, 0x0a, 0x09, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x12,
0x27, 0x0a, 0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x12, 0x0b, 0x2e, 0x55, 0x72,
0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0c, 0x2e, 0x55, 0x72, 0x6c, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x35, 0x5a, 0x33, 0x67, 0x69, 0x74, 0x68,
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x69, 0x74, 0x65, 0x77, 0x6f, 0x72, 0x78, 0x70,
0x72, 0x6f, 0x2f, 0x69, 0x6d, 0x67, 0x2d, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2d, 0x75, 0x72, 0x6c,
0x2d, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x67, 0x72, 0x70, 0x63,
}
var (
file_grpc_imgurl_proto_rawDescOnce sync.Once
file_grpc_imgurl_proto_rawDescData = file_grpc_imgurl_proto_rawDesc
)
func file_grpc_imgurl_proto_rawDescGZIP() []byte {
file_grpc_imgurl_proto_rawDescOnce.Do(func() {
file_grpc_imgurl_proto_rawDescData = protoimpl.X.CompressGZIP(file_grpc_imgurl_proto_rawDescData)
})
return file_grpc_imgurl_proto_rawDescData
}
var file_grpc_imgurl_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_grpc_imgurl_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_grpc_imgurl_proto_goTypes = []interface{}{
(Format)(0), // 0: Format
(*UrlRequest)(nil), // 1: UrlRequest
(*UrlResponse)(nil), // 2: UrlResponse
}
var file_grpc_imgurl_proto_depIdxs = []int32{
0, // 0: UrlRequest.format:type_name -> Format
1, // 1: Generator.Generate:input_type -> UrlRequest
2, // 2: Generator.Generate:output_type -> UrlResponse
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_grpc_imgurl_proto_init() }
func file_grpc_imgurl_proto_init() {
if File_grpc_imgurl_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_grpc_imgurl_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UrlRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_grpc_imgurl_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UrlResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_grpc_imgurl_proto_rawDesc,
NumEnums: 1,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_grpc_imgurl_proto_goTypes,
DependencyIndexes: file_grpc_imgurl_proto_depIdxs,
EnumInfos: file_grpc_imgurl_proto_enumTypes,
MessageInfos: file_grpc_imgurl_proto_msgTypes,
}.Build()
File_grpc_imgurl_proto = out.File
file_grpc_imgurl_proto_rawDesc = nil
file_grpc_imgurl_proto_goTypes = nil
file_grpc_imgurl_proto_depIdxs = nil
}

24
grpc/imgurl.proto Normal file
View File

@@ -0,0 +1,24 @@
option go_package = "github.com/siteworxpro/img-proxy-url-generator/grpc";
message UrlRequest {
required string image = 1;
repeated string params = 2;
optional Format format = 3;
}
enum Format {
JPG = 1;
PNG = 2;
BMP = 3;
WEBP = 4;
GIF = 5;
ICO = 6;
}
message UrlResponse {
required string url = 1;
}
service Generator {
rpc Generate (UrlRequest) returns (UrlResponse) {}
}

121
grpc/imgurl_grpc.pb.go Normal file
View File

@@ -0,0 +1,121 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.5.1
// - protoc v5.28.3
// source: grpc/imgurl.proto
package grpc
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 (
Generator_Generate_FullMethodName = "/Generator/Generate"
)
// GeneratorClient is the client API for Generator 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 GeneratorClient interface {
Generate(ctx context.Context, in *UrlRequest, opts ...grpc.CallOption) (*UrlResponse, error)
}
type generatorClient struct {
cc grpc.ClientConnInterface
}
func NewGeneratorClient(cc grpc.ClientConnInterface) GeneratorClient {
return &generatorClient{cc}
}
func (c *generatorClient) Generate(ctx context.Context, in *UrlRequest, opts ...grpc.CallOption) (*UrlResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(UrlResponse)
err := c.cc.Invoke(ctx, Generator_Generate_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// GeneratorServer is the server API for Generator service.
// All implementations must embed UnimplementedGeneratorServer
// for forward compatibility.
type GeneratorServer interface {
Generate(context.Context, *UrlRequest) (*UrlResponse, error)
mustEmbedUnimplementedGeneratorServer()
}
// UnimplementedGeneratorServer 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 UnimplementedGeneratorServer struct{}
func (UnimplementedGeneratorServer) Generate(context.Context, *UrlRequest) (*UrlResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Generate not implemented")
}
func (UnimplementedGeneratorServer) mustEmbedUnimplementedGeneratorServer() {}
func (UnimplementedGeneratorServer) testEmbeddedByValue() {}
// UnsafeGeneratorServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to GeneratorServer will
// result in compilation errors.
type UnsafeGeneratorServer interface {
mustEmbedUnimplementedGeneratorServer()
}
func RegisterGeneratorServer(s grpc.ServiceRegistrar, srv GeneratorServer) {
// If the following call pancis, it indicates UnimplementedGeneratorServer 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(&Generator_ServiceDesc, srv)
}
func _Generator_Generate_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UrlRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GeneratorServer).Generate(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Generator_Generate_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GeneratorServer).Generate(ctx, req.(*UrlRequest))
}
return interceptor(ctx, in, info, handler)
}
// Generator_ServiceDesc is the grpc.ServiceDesc for Generator service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var Generator_ServiceDesc = grpc.ServiceDesc{
ServiceName: "Generator",
HandlerType: (*GeneratorServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Generate",
Handler: _Generator_Generate_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "grpc/imgurl.proto",
}

41
grpc/server.go Normal file
View File

@@ -0,0 +1,41 @@
package grpc
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/siteworxpro/img-proxy-url-generator/generator"
"log"
"strings"
)
type GeneratorService struct {
UnimplementedGeneratorServer
imgGenerator *generator.Generator
}
func NewService(imgGenerator *generator.Generator) *GeneratorService {
return &GeneratorService{imgGenerator: imgGenerator}
}
func (s *GeneratorService) Generate(c context.Context, r *UrlRequest) (*UrlResponse, error) {
var err error
format := generator.DEF
if r.Format != nil {
format, err = s.imgGenerator.StringToFormat(r.Format.String())
if err != nil {
return nil, err
}
}
url, err := s.imgGenerator.GenerateUrl(*r.Image, r.Params, format)
if err != nil {
return nil, err
}
log.Println(fmt.Sprintf("%s - [%s] - (%s)", *r.Image, strings.Join(r.Params, ","), url))
return &UrlResponse{Url: aws.String(url)}, nil
}