From a78897913427e3dfe8ade3c0f787bbb07fab6ee1 Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Sat, 27 Apr 2024 22:43:02 -0500 Subject: [PATCH 1/5] refactor: field name formatting --- types/json.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/json.go b/types/json.go index 7d33cd2..8f94c76 100644 --- a/types/json.go +++ b/types/json.go @@ -5,9 +5,9 @@ type SendGridTemplates struct { } type SendGridTokens struct { - ApiKey string `json:"api_key"` - RecoveryCode string `json:"recovery_code,omitempty"` - ApiKeyId string `json:"api_key_id,omitempty"` + ApiKey string `json:"apiKey"` + RecoveryCode string `json:"recoveryCode,omitempty"` + ApiKeyId string `json:"apiKeyId,omitempty"` Templates SendGridTemplates `json:"templates,omitempty"` } From 001e6f11a1263d5852e6da052b1af3a2160991f2 Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Sat, 27 Apr 2024 22:43:51 -0500 Subject: [PATCH 2/5] feat: setup cloudinary integration --- go.mod | 3 +++ go.sum | 10 ++++++++++ services/service.go | 2 ++ setup/server_setup.go | 9 +++++++++ tokens.json | 5 +++++ types/json.go | 7 +++++++ 6 files changed, 36 insertions(+) diff --git a/go.mod b/go.mod index 690aa2e..37c48b7 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,9 @@ require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect + github.com/cloudinary/cloudinary-go/v2 v2.7.0 // indirect github.com/containerd/continuity v0.4.3 // indirect + github.com/creasty/defaults v1.5.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/docker/cli v25.0.4+incompatible // indirect github.com/docker/docker v25.0.4+incompatible // indirect @@ -38,6 +40,7 @@ require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.3 // indirect + github.com/gorilla/schema v1.2.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect diff --git a/go.sum b/go.sum index c4cb620..5b51413 100644 --- a/go.sum +++ b/go.sum @@ -79,6 +79,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI= github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloudinary/cloudinary-go/v2 v2.7.0 h1:8Fuh/SOen6IQgqH8CLso2E+kuKi2xjbdiyXOspwXFTM= +github.com/cloudinary/cloudinary-go/v2 v2.7.0/go.mod h1:jtSxa6xbzvu4IwChRJVDcXwVXrTRczhbvq3Z1VSoFdk= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= @@ -95,6 +97,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= +github.com/creasty/defaults v1.5.1 h1:j8WexcS3d/t4ZmllX4GEkl4wIB/trOr035ajcLHCISM= +github.com/creasty/defaults v1.5.1/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -151,6 +155,7 @@ github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaC github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= @@ -224,6 +229,7 @@ github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= @@ -233,12 +239,15 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/googleapis/gax-go/v2 v2.12.3 h1:5/zPPDvw8Q1SuXjrqrZslrqT7dL/uJT2CQii/cLCKqA= github.com/googleapis/gax-go/v2 v2.12.3/go.mod h1:AKloxT6GtNbaLm8QTNSidHUVsHYcBHwWRvkNFJUQcS4= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= +github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/heimdalr/dag v1.0.1/go.mod h1:t+ZkR+sjKL4xhlE1B9rwpvwfo+x+2R0363efS+Oghns= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= @@ -378,6 +387,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= diff --git a/services/service.go b/services/service.go index 11e90c3..d3669fe 100644 --- a/services/service.go +++ b/services/service.go @@ -3,6 +3,7 @@ package services import ( "database/sql" + "github.com/cloudinary/cloudinary-go/v2" "github.com/go-jet/jet/v2/qrm" "github.com/go-playground/validator/v10" "github.com/stadio-app/stadio-backend/types" @@ -13,6 +14,7 @@ type Service struct { TX *sql.Tx StructValidator *validator.Validate Tokens *types.Tokens + Cloudinary *cloudinary.Cloudinary } // Returns a transaction if present. diff --git a/setup/server_setup.go b/setup/server_setup.go index 9322f3a..3d3cbbb 100644 --- a/setup/server_setup.go +++ b/setup/server_setup.go @@ -6,6 +6,7 @@ import ( "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/playground" "github.com/ayaanqui/go-migration-tool/migration_tool" + "github.com/cloudinary/cloudinary-go/v2" "github.com/go-chi/chi/v5" "github.com/go-playground/validator/v10" "github.com/stadio-app/stadio-backend/graph" @@ -38,10 +39,18 @@ func NewServer(db_conn *sql.DB, router *chi.Mux) *types.ServerBase { } server.Tokens = &tokens + + // Cloudinary CDN + cloudinary, err := cloudinary.NewFromParams(tokens.Cloudinary.CloudName, tokens.Cloudinary.ApiKey, tokens.Cloudinary.ApiSecret) + if err != nil { + panic(err) + } + service := services.Service{ DB: server.DB, StructValidator: server.StructValidator, Tokens: &tokens, + Cloudinary: cloudinary, } // TODO: only show in dev environment diff --git a/tokens.json b/tokens.json index 695d1cc..91a8d54 100644 --- a/tokens.json +++ b/tokens.json @@ -7,5 +7,10 @@ "templates": { "emailVerification": "d-cf46254fdbe349728acf71dc3ca9cec1" } + }, + "cloudinary": { + "cloudName": "development", + "apiKey": "134931444131283", + "apiSecret": "dWLrjkq7RFZBo1RHjAjL50GYnx0" } } \ No newline at end of file diff --git a/types/json.go b/types/json.go index 8f94c76..ad5b041 100644 --- a/types/json.go +++ b/types/json.go @@ -11,9 +11,16 @@ type SendGridTokens struct { Templates SendGridTemplates `json:"templates,omitempty"` } +type CloudinaryTokens struct { + ApiKey string `json:"apiKey"` + ApiSecret string `json:"apiSecret"` + CloudName string `json:"cloudName"` +} + type Tokens struct { JwtKey string `json:"jwtKey"` SendGrid SendGridTokens `json:"sendGrid,omitempty"` + Cloudinary CloudinaryTokens `json:"cloudinary"` // To add other tokens create a struct and add them here, // make sure to also update ./tokens.json } From e98d37b2e28d3501a872587fc377301a1e5e9dcb Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Sat, 27 Apr 2024 22:45:04 -0500 Subject: [PATCH 3/5] fix: cloudinary.cloudName value --- tokens.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokens.json b/tokens.json index 91a8d54..b137077 100644 --- a/tokens.json +++ b/tokens.json @@ -9,7 +9,7 @@ } }, "cloudinary": { - "cloudName": "development", + "cloudName": "stadio", "apiKey": "134931444131283", "apiSecret": "dWLrjkq7RFZBo1RHjAjL50GYnx0" } From 683ea0fc4719fc0b9d94b63e23666322fd39dd5c Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Tue, 23 Apr 2024 12:35:14 -0500 Subject: [PATCH 4/5] feat: cloudflare image upload helper methods --- services/image_upload_service.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 services/image_upload_service.go diff --git a/services/image_upload_service.go b/services/image_upload_service.go new file mode 100644 index 0000000..2d8a867 --- /dev/null +++ b/services/image_upload_service.go @@ -0,0 +1,23 @@ +package services + +import ( + "context" + + "github.com/99designs/gqlgen/graphql" + "github.com/cloudflare/cloudflare-go" +) + +func (service Service) CloudflareImageUpload(ctx context.Context, data cloudflare.UploadImageParams) (cloudflare.Image, error) { + cf_resource_container := cloudflare.ResourceContainer{ + Level: cloudflare.UserRouteLevel, + Type: cloudflare.UserType, + Identifier: "", + } + return service.CloudflareApi.UploadImage(ctx, &cf_resource_container, data) +} + +func (service Service) GraphImageUpload(ctx context.Context, file graphql.Upload) (cloudflare.Image, error) { + return service.CloudflareImageUpload(ctx, cloudflare.UploadImageParams{ + File: nil, + }) +} From 73a942e541b20e02bea6efe589bf3fc89a8589c2 Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Sat, 27 Apr 2024 22:58:44 -0500 Subject: [PATCH 5/5] feat: cloudinary upload helper method --- services/image_upload_service.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/services/image_upload_service.go b/services/image_upload_service.go index 2d8a867..cb2fbdb 100644 --- a/services/image_upload_service.go +++ b/services/image_upload_service.go @@ -4,20 +4,13 @@ import ( "context" "github.com/99designs/gqlgen/graphql" - "github.com/cloudflare/cloudflare-go" + "github.com/cloudinary/cloudinary-go/v2/api/uploader" ) -func (service Service) CloudflareImageUpload(ctx context.Context, data cloudflare.UploadImageParams) (cloudflare.Image, error) { - cf_resource_container := cloudflare.ResourceContainer{ - Level: cloudflare.UserRouteLevel, - Type: cloudflare.UserType, - Identifier: "", - } - return service.CloudflareApi.UploadImage(ctx, &cf_resource_container, data) -} - -func (service Service) GraphImageUpload(ctx context.Context, file graphql.Upload) (cloudflare.Image, error) { - return service.CloudflareImageUpload(ctx, cloudflare.UploadImageParams{ - File: nil, - }) +func (service Service) GraphImageUpload( + ctx context.Context, + file graphql.Upload, + params uploader.UploadParams, +) (*uploader.UploadResult, error) { + return service.Cloudinary.Upload.Upload(ctx, file.File, params) }