Added grpc web service

This commit is contained in:
2024-11-17 11:22:49 -05:00
parent 1ad11d7e08
commit 4d2a7418cb
4 changed files with 65 additions and 62 deletions

View File

@@ -1,6 +1,9 @@
version: '3' version: '3'
tasks: tasks:
proto-build-php:
cmds:
- protoc --php_out=. --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin grpc/imgurl.proto
proto-build: proto-build:
cmds: cmds:
- protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative grpc/imgurl.proto - protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative grpc/imgurl.proto

View File

@@ -23,31 +23,31 @@ const (
type Format int32 type Format int32
const ( const (
Format_JPG Format = 1 Format_JPG Format = 0
Format_PNG Format = 2 Format_PNG Format = 1
Format_BMP Format = 3 Format_BMP Format = 2
Format_WEBP Format = 4 Format_WEBP Format = 3
Format_GIF Format = 5 Format_GIF Format = 4
Format_ICO Format = 6 Format_ICO Format = 5
) )
// Enum value maps for Format. // Enum value maps for Format.
var ( var (
Format_name = map[int32]string{ Format_name = map[int32]string{
1: "JPG", 0: "JPG",
2: "PNG", 1: "PNG",
3: "BMP", 2: "BMP",
4: "WEBP", 3: "WEBP",
5: "GIF", 4: "GIF",
6: "ICO", 5: "ICO",
} }
Format_value = map[string]int32{ Format_value = map[string]int32{
"JPG": 1, "JPG": 0,
"PNG": 2, "PNG": 1,
"BMP": 3, "BMP": 2,
"WEBP": 4, "WEBP": 3,
"GIF": 5, "GIF": 4,
"ICO": 6, "ICO": 5,
} }
) )
@@ -73,16 +73,6 @@ func (x Format) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x) 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. // Deprecated: Use Format.Descriptor instead.
func (Format) EnumDescriptor() ([]byte, []int) { func (Format) EnumDescriptor() ([]byte, []int) {
return file_grpc_imgurl_proto_rawDescGZIP(), []int{0} return file_grpc_imgurl_proto_rawDescGZIP(), []int{0}
@@ -93,9 +83,9 @@ type UrlRequest struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Image *string `protobuf:"bytes,1,req,name=image" json:"image,omitempty"` Image string `protobuf:"bytes,1,opt,name=image,proto3" json:"image,omitempty"`
Params []string `protobuf:"bytes,2,rep,name=params" json:"params,omitempty"` Params []string `protobuf:"bytes,2,rep,name=params,proto3" json:"params,omitempty"`
Format *Format `protobuf:"varint,3,opt,name=format,enum=Format" json:"format,omitempty"` Format *Format `protobuf:"varint,3,opt,name=format,proto3,enum=Format,oneof" json:"format,omitempty"`
} }
func (x *UrlRequest) Reset() { func (x *UrlRequest) Reset() {
@@ -131,8 +121,8 @@ func (*UrlRequest) Descriptor() ([]byte, []int) {
} }
func (x *UrlRequest) GetImage() string { func (x *UrlRequest) GetImage() string {
if x != nil && x.Image != nil { if x != nil {
return *x.Image return x.Image
} }
return "" return ""
} }
@@ -156,7 +146,7 @@ type UrlResponse struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Url *string `protobuf:"bytes,1,req,name=url" json:"url,omitempty"` Url string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"`
} }
func (x *UrlResponse) Reset() { func (x *UrlResponse) Reset() {
@@ -192,8 +182,8 @@ func (*UrlResponse) Descriptor() ([]byte, []int) {
} }
func (x *UrlResponse) GetUrl() string { func (x *UrlResponse) GetUrl() string {
if x != nil && x.Url != nil { if x != nil {
return *x.Url return x.Url
} }
return "" return ""
} }
@@ -202,25 +192,29 @@ var File_grpc_imgurl_proto protoreflect.FileDescriptor
var file_grpc_imgurl_proto_rawDesc = []byte{ var file_grpc_imgurl_proto_rawDesc = []byte{
0x0a, 0x11, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x69, 0x6d, 0x67, 0x75, 0x72, 0x6c, 0x2e, 0x70, 0x72, 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, 0x6f, 0x74, 0x6f, 0x22, 0x6b, 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, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 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, 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, 0x24, 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, 0x07, 0x2e, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x48, 0x00, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d,
0x61, 0x74, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
0x22, 0x1f, 0x0a, 0x0b, 0x55, 0x72, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 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, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72,
0x6c, 0x2a, 0x3f, 0x0a, 0x06, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x07, 0x0a, 0x03, 0x4a, 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, 0x50, 0x47, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x50, 0x4e, 0x47, 0x10, 0x01, 0x12, 0x07, 0x0a,
0x03, 0x42, 0x4d, 0x50, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x57, 0x45, 0x42, 0x50, 0x10, 0x04, 0x03, 0x42, 0x4d, 0x50, 0x10, 0x02, 0x12, 0x08, 0x0a, 0x04, 0x57, 0x45, 0x42, 0x50, 0x10, 0x03,
0x12, 0x07, 0x0a, 0x03, 0x47, 0x49, 0x46, 0x10, 0x05, 0x12, 0x07, 0x0a, 0x03, 0x49, 0x43, 0x4f, 0x12, 0x07, 0x0a, 0x03, 0x47, 0x49, 0x46, 0x10, 0x04, 0x12, 0x07, 0x0a, 0x03, 0x49, 0x43, 0x4f,
0x10, 0x06, 0x32, 0x34, 0x0a, 0x09, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x12, 0x10, 0x05, 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, 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, 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, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x5e, 0x5a, 0x33, 0x67, 0x69, 0x74, 0x68,
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x69, 0x74, 0x65, 0x77, 0x6f, 0x72, 0x78, 0x70, 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, 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, 0x2d, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x67, 0x72, 0x70, 0x63, 0xca,
0x02, 0x26, 0x53, 0x69, 0x74, 0x65, 0x77, 0x6f, 0x72, 0x78, 0x5c, 0x4c, 0x69, 0x62, 0x72, 0x61,
0x72, 0x79, 0x5c, 0x49, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x5c,
0x49, 0x6d, 0x67, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@@ -284,6 +278,7 @@ func file_grpc_imgurl_proto_init() {
} }
} }
} }
file_grpc_imgurl_proto_msgTypes[0].OneofWrappers = []interface{}{}
type x struct{} type x struct{}
out := protoimpl.TypeBuilder{ out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{ File: protoimpl.DescBuilder{

View File

@@ -1,22 +1,25 @@
syntax = "proto3";
option go_package = "github.com/siteworxpro/img-proxy-url-generator/grpc"; option go_package = "github.com/siteworxpro/img-proxy-url-generator/grpc";
option php_namespace = "Siteworx\\Library\\Integrations\\ImgProxy";
message UrlRequest { message UrlRequest {
required string image = 1; string image = 1;
repeated string params = 2; repeated string params = 2;
optional Format format = 3; optional Format format = 3;
} }
enum Format { enum Format {
JPG = 1; JPG = 0;
PNG = 2; PNG = 1;
BMP = 3; BMP = 2;
WEBP = 4; WEBP = 3;
GIF = 5; GIF = 4;
ICO = 6; ICO = 5;
} }
message UrlResponse { message UrlResponse {
required string url = 1; string url = 1;
} }
service Generator { service Generator {

View File

@@ -3,7 +3,6 @@ package grpc
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/siteworxpro/img-proxy-url-generator/generator" "github.com/siteworxpro/img-proxy-url-generator/generator"
"log" "log"
"strings" "strings"
@@ -19,6 +18,7 @@ func NewService(imgGenerator *generator.Generator) *GeneratorService {
} }
func (s *GeneratorService) Generate(c context.Context, r *UrlRequest) (*UrlResponse, error) { func (s *GeneratorService) Generate(c context.Context, r *UrlRequest) (*UrlResponse, error) {
defer c.Done()
var err error var err error
format := generator.DEF format := generator.DEF
@@ -26,16 +26,18 @@ func (s *GeneratorService) Generate(c context.Context, r *UrlRequest) (*UrlRespo
if r.Format != nil { if r.Format != nil {
format, err = s.imgGenerator.StringToFormat(r.Format.String()) format, err = s.imgGenerator.StringToFormat(r.Format.String())
if err != nil { if err != nil {
println(err.Error())
return nil, err return nil, err
} }
} }
url, err := s.imgGenerator.GenerateUrl(*r.Image, r.Params, format) url, err := s.imgGenerator.GenerateUrl(r.Image, r.Params, format)
if err != nil { if err != nil {
println(err.Error())
return nil, err return nil, err
} }
log.Println(fmt.Sprintf("%s - [%s] - (%s)", *r.Image, strings.Join(r.Params, ","), url)) log.Println(fmt.Sprintf("%s - [%s] - (%s)", r.Image, strings.Join(r.Params, ","), url))
return &UrlResponse{Url: aws.String(url)}, nil return &UrlResponse{Url: url}, nil
} }