Skip to content

Commit

Permalink
wip (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlapao authored Dec 10, 2022
1 parent 1cdd1ae commit 1d46f8f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
98 changes: 98 additions & 0 deletions configuration/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package configuration

import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"

"github.com/cjlapao/common-go/guard"
)

type RedisConfigurationProvider struct {
connectionString string
client string
}

func NewRedisConfigurationProvider(connString string) *RedisConfigurationProvider {
result := RedisConfigurationProvider{
connectionString: connString,
}

return &result
}

func (ev RedisConfigurationProvider) UpsertKey(key string, value interface{}) error {
emptyKey := guard.EmptyOrNil(key, "key")
emptyValue := guard.EmptyOrNil(value, "value")

if emptyKey != nil {
return emptyKey
}

if emptyValue != nil {
return emptyValue
}

switch v := value.(type) {
case int:
os.Setenv(key, strconv.Itoa(v))
case float64:
os.Setenv(key, fmt.Sprintf("%v", v))
case bool:
os.Setenv(key, fmt.Sprintf("%v", v))
os.Setenv(key, fmt.Sprintf("%v", v))
case string:
os.Setenv(key, v)
default:
jsonBytes, err := json.Marshal(v)
if err == nil {
os.Setenv(key, string(jsonBytes))
}
}

return nil
}

func (ev RedisConfigurationProvider) UpsertKeys(values map[string]interface{}) []error {
errorArray := make([]error, 0)

if values == nil {
errorArray = append(errorArray, errors.New("array is nil"))
return errorArray
}

if len(values) > 0 {
for key, value := range values {
emptykey := guard.EmptyOrNil(key)
emptyValue := guard.EmptyOrNil(value)

if emptykey != nil {
errorArray = append(errorArray, emptykey)
}

if emptyValue != nil {
errorArray = append(errorArray, emptyValue)
}

ev.UpsertKey(key, value)
}

return errorArray
}

return nil
}

func (ev RedisConfigurationProvider) Get(key string) interface{} {
return os.Getenv(key)
}

func (ev RedisConfigurationProvider) Clear(key string) {
emptyKey := guard.EmptyOrNil(key, "key")

if emptyKey == nil {
os.Setenv(key, "")
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ require (
github.com/cjlapao/common-go-cryptorand v0.0.4
github.com/cjlapao/common-go-identity v0.0.3
github.com/fatih/color v1.13.0
github.com/go-redis/redis/v8 v8.11.5
github.com/pascaldekloe/jwt v1.12.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cjlapao/common-go-cryptorand v0.0.4 h1:M4hBZlxXJJ4yY3itQzDCGYJH0rmXYd1nOa8T97EPPpc=
github.com/cjlapao/common-go-cryptorand v0.0.4/go.mod h1:gUG7Bso/ZDD8tOoVmMvaYWMsglfAO9eg+p74OQH7Z2w=
github.com/cjlapao/common-go-identity v0.0.3 h1:0/lZ6Ke9KErhM4ZeJIfmETq+zUdf2jl0CH+Kn6v+HgQ=
github.com/cjlapao/common-go-identity v0.0.3/go.mod h1:xuNepNCHVI/51Q6DQgNPYvx3HS0VaeEhGnp8YcDO/+I=
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=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand All @@ -21,6 +28,9 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/pascaldekloe/jwt v1.12.0 h1:imQSkPOtAIBAXoKKjL9ZVJuF/rVqJ+ntiLGpLyeqMUQ=
github.com/pascaldekloe/jwt v1.12.0/go.mod h1:LiIl7EwaglmH1hWThd/AmydNCnHf/mmfluBlNqHbk8U=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -34,15 +44,19 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 33 additions & 0 deletions helper/http_helper/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/cjlapao/common-go/language"
)

var MaxUploadSize = int64(1 * 1024 * 1024) // 1mb in memory

// DownloadFile Downloads a file from a url
func DownloadFile(url string, filepath string) error {
resp, err := http.Get(url)
Expand Down Expand Up @@ -190,3 +192,34 @@ func JoinUrl(element ...string) string {

return strings.ReplaceAll(base, "//", "/")
}

func GetFormFile(request *http.Request, fileName string) (io.Reader, error) {
contentType := ""
if request.Header.Get("Content-Type") != "" {
contentType = request.Header.Get("Content-Type")
}
if request.Header.Get("content-type") != "" {
contentType = request.Header.Get("content-type")
}

if contentType == "" {
return nil, errors.New("unknown content type")
}

if strings.HasPrefix(contentType, "multipart/form-data") {
if request.MultipartForm == nil {
err := request.ParseMultipartForm(MaxUploadSize)
if err != nil {
return nil, err
}
}

if file, _, err := request.FormFile(fileName); err != nil {
return nil, err
} else {
return file, nil
}
} else {
return request.Body, nil
}
}

0 comments on commit 1d46f8f

Please sign in to comment.