From b53839cdf023fe4c2ea7c907909690fcb6caebb2 Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 28 Aug 2024 21:16:48 +0500 Subject: [PATCH] env: remove unused types, add default values auth: fix orthography, remove auth structure --- auth/auth.go | 129 +++++++++++++++++------------------ env/env.go | 159 ++++++++++++++++--------------------------- env/env_test.go | 64 +++++------------ middlewares/fiber.go | 4 +- 4 files changed, 141 insertions(+), 215 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index cf0dd22..908bfd8 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -2,6 +2,7 @@ package auth import ( "errors" + "github.com/Toscale-platform/kit/env" "github.com/Toscale-platform/kit/log" "github.com/Toscale-platform/kit/output" "github.com/Toscale-platform/kit/validator" @@ -15,82 +16,74 @@ type response struct { User uint64 `json:"user"` } -type Auth struct { - isDebug bool - host string -} - type TotalAdminPermission struct { - IsAvaliableTools bool `json:"tools,omitempty"` - IsAvaliableTerminals bool `json:"terminals,omitempty"` - IsAvaliableUsers bool `json:"users,omitempty"` - IsAvaliableBackendTesting bool `json:"backendTesting,omitempty"` - IsAvaliableDocumentation bool `json:"documentation,omitempty"` - IsAvaliableInsights bool `json:"insights,omitempty"` - IsAvaliableBalancer bool `json:"balancer,omitempty"` - IsAvaliableNews bool `json:"news,omitempty"` - IsAvaliableTwitter bool `json:"twitter,omitempty"` - IsAvaliableForex bool `json:"forex,omitempty"` - IsAvaliableLanguages bool `json:"languages,omitempty"` + IsAvailableTools bool `json:"tools,omitempty"` + IsAvailableTerminals bool `json:"terminals,omitempty"` + IsAvailableUsers bool `json:"users,omitempty"` + IsAvailableBackendTesting bool `json:"backendTesting,omitempty"` + IsAvailableDocumentation bool `json:"documentation,omitempty"` + IsAvailableInsights bool `json:"insights,omitempty"` + IsAvailableBalancer bool `json:"balancer,omitempty"` + IsAvailableNews bool `json:"news,omitempty"` + IsAvailableTwitter bool `json:"twitter,omitempty"` + IsAvailableForex bool `json:"forex,omitempty"` + IsAvailableLanguages bool `json:"languages,omitempty"` } -var httpClient = http.Client{} - -func Init(host string, isDebug bool) *Auth { - return &Auth{ - isDebug: isDebug, - host: host, - } -} +var ( + Debug = env.GetBool("DEBUG", false) + Host = env.GetString("AUTH_HOST", "https://auth.toscale.io") + httpClient = http.Client{} +) -func (a *Auth) GetAdminPermissions(ctx *fasthttp.RequestCtx, serviceName string) (permissions TotalAdminPermission, err error) { - if !a.isDebug { - permissions, err = FetchAdminPermissions(ctx, a.host) +func GetAdminPermissions(ctx *fasthttp.RequestCtx) (permissions TotalAdminPermission, err error) { + if !Debug { + permissions, err = FetchAdminPermissions(ctx, Host) if err != nil { return } } else { permissions = TotalAdminPermission{ - IsAvaliableTools: true, - IsAvaliableTerminals: true, - IsAvaliableUsers: true, - IsAvaliableBackendTesting: true, - IsAvaliableDocumentation: true, - IsAvaliableInsights: true, - IsAvaliableBalancer: true, - IsAvaliableNews: true, - IsAvaliableTwitter: true, - IsAvaliableForex: true, - IsAvaliableLanguages: true, + IsAvailableTools: true, + IsAvailableTerminals: true, + IsAvailableUsers: true, + IsAvailableBackendTesting: true, + IsAvailableDocumentation: true, + IsAvailableInsights: true, + IsAvailableBalancer: true, + IsAvailableNews: true, + IsAvailableTwitter: true, + IsAvailableForex: true, + IsAvailableLanguages: true, } } return permissions, nil } -func (a *Auth) ValidateAdminPermissions(next fasthttp.RequestHandler, serviceName string) fasthttp.RequestHandler { +func ValidateAdminPermissions(next fasthttp.RequestHandler, serviceName string) fasthttp.RequestHandler { return func(ctx *fasthttp.RequestCtx) { var permissions TotalAdminPermission var err error - if !a.isDebug { - permissions, err = FetchAdminPermissions(ctx, a.host) + if !Debug { + permissions, err = FetchAdminPermissions(ctx, Host) if err != nil { output.JsonMessageResult(ctx, 403, "forbidden") return } } else { permissions = TotalAdminPermission{ - IsAvaliableTools: true, - IsAvaliableTerminals: true, - IsAvaliableUsers: true, - IsAvaliableBackendTesting: true, - IsAvaliableDocumentation: true, - IsAvaliableInsights: true, - IsAvaliableBalancer: true, - IsAvaliableNews: true, - IsAvaliableTwitter: true, - IsAvaliableForex: true, + IsAvailableTools: true, + IsAvailableTerminals: true, + IsAvailableUsers: true, + IsAvailableBackendTesting: true, + IsAvailableDocumentation: true, + IsAvailableInsights: true, + IsAvailableBalancer: true, + IsAvailableNews: true, + IsAvailableTwitter: true, + IsAvailableForex: true, } } @@ -99,67 +92,67 @@ func (a *Auth) ValidateAdminPermissions(next fasthttp.RequestHandler, serviceNam switch serviceName { case "terminals": { - if !permissions.IsAvaliableTerminals { + if !permissions.IsAvailableTerminals { isInvalid = true } } case "tools": { - if !permissions.IsAvaliableTools { + if !permissions.IsAvailableTools { isInvalid = true } } case "users": { - if !permissions.IsAvaliableUsers { + if !permissions.IsAvailableUsers { isInvalid = true } } case "backendTesting": { - if !permissions.IsAvaliableBackendTesting { + if !permissions.IsAvailableBackendTesting { isInvalid = true } } case "documentation": { - if !permissions.IsAvaliableDocumentation { + if !permissions.IsAvailableDocumentation { isInvalid = true } } case "insights": { - if !permissions.IsAvaliableInsights { + if !permissions.IsAvailableInsights { isInvalid = true } } case "balancer": { - if !permissions.IsAvaliableBalancer { + if !permissions.IsAvailableBalancer { isInvalid = true } } case "news": { - if !permissions.IsAvaliableNews { + if !permissions.IsAvailableNews { isInvalid = true } } case "twitter": { - if !permissions.IsAvaliableTwitter { + if !permissions.IsAvailableTwitter { isInvalid = true } } case "forex": { - if !permissions.IsAvaliableForex { + if !permissions.IsAvailableForex { isInvalid = true } } case "languages": { - if !permissions.IsAvaliableLanguages { + if !permissions.IsAvailableLanguages { isInvalid = true } } @@ -188,10 +181,10 @@ func FetchAdminPermissions(ctx *fasthttp.RequestCtx, host string) (perms TotalAd return internalGetPermissions(token, host) } -func (a *Auth) IsAdmin(next fasthttp.RequestHandler) fasthttp.RequestHandler { +func IsAdmin(next fasthttp.RequestHandler) fasthttp.RequestHandler { return func(ctx *fasthttp.RequestCtx) { - if !a.isDebug { - id, err := VerifyAdmin(ctx, a.host) + if !Debug { + id, err := VerifyAdmin(ctx, Host) if err != nil { output.JsonMessageResult(ctx, 403, "forbidden") return @@ -206,9 +199,9 @@ func (a *Auth) IsAdmin(next fasthttp.RequestHandler) fasthttp.RequestHandler { } } -func (a *Auth) IsAdminFiber(c *fiber.Ctx) error { - if !a.isDebug { - id, err := VerifyAdminFiber(c, a.host) +func IsAdminFiber(c *fiber.Ctx) error { + if !Debug { + id, err := VerifyAdminFiber(c, Host) if err != nil { return fiber.NewError(403, "forbidden") } diff --git a/env/env.go b/env/env.go index 4689e0c..227ca91 100644 --- a/env/env.go +++ b/env/env.go @@ -11,138 +11,99 @@ func init() { _ = godotenv.Load() } -func parseInt(key string, bitSize int) int64 { - v, err := strconv.ParseInt(os.Getenv(key), 10, bitSize) - if err != nil { - return 0 - } - - return v -} - -func parseUint(key string, bitSize int) uint64 { - v, err := strconv.ParseUint(os.Getenv(key), 10, bitSize) - if err != nil { - return 0 - } - - return v -} - -func parseFloat(key string, bitSize int) float64 { - v, err := strconv.ParseFloat(os.Getenv(key), bitSize) - if err != nil { - return 0 - } - - return v -} +// String -func parseComplex(key string, bitSize int) complex128 { - v, err := strconv.ParseComplex(os.Getenv(key), bitSize) - if err != nil { - return 0 +func GetString(key string, defaultValue ...string) string { + v := os.Getenv(key) + if len(v) == 0 && len(defaultValue) > 0 { + return defaultValue[0] } - return v } -// String - -func GetString(key string) string { - return os.Getenv(key) -} - -// Slices of string, bytes and runes +// Slice of string -func GetSlice(key string) []string { +func GetSlice(key string, defaultValue ...[]string) []string { v := os.Getenv(key) - if v == "" { + if len(v) == 0 { + if len(defaultValue) > 0 { + return defaultValue[0] + } return []string{} } - return strings.Split(v, ",") } -func GetBytes(key string) []byte { - return []byte(os.Getenv(key)) -} - -func GetRunes(key string) []rune { - return []rune(os.Getenv(key)) -} - // Bool -func GetBool(key string) bool { +func GetBool(key string, defaultValue ...bool) bool { v, err := strconv.ParseBool(os.Getenv(key)) if err != nil { + if len(defaultValue) > 0 { + return defaultValue[0] + } return false } - return v } // Int -func GetInt(key string) int { - return int(parseInt(key, 0)) -} - -func GetInt8(key string) int8 { - return int8(parseInt(key, 8)) -} - -func GetInt16(key string) int16 { - return int16(parseInt(key, 16)) -} - -func GetInt32(key string) int32 { - return int32(parseInt(key, 32)) +func GetInt(key string, defaultValue ...int) int { + v, err := strconv.ParseInt(os.Getenv(key), 10, 0) + if err != nil { + if len(defaultValue) > 0 { + return defaultValue[0] + } + return 0 + } + return int(v) } -func GetInt64(key string) int64 { - return parseInt(key, 64) +func GetInt64(key string, defaultValue ...int64) int64 { + v, err := strconv.ParseInt(os.Getenv(key), 10, 64) + if err != nil { + if len(defaultValue) > 0 { + return defaultValue[0] + } + return 0 + } + return v } // Uint -func GetUint(key string) uint { - return uint(parseUint(key, 0)) -} - -func GetUint8(key string) uint8 { - return uint8(parseUint(key, 8)) -} - -func GetUint16(key string) uint16 { - return uint16(parseUint(key, 16)) -} - -func GetUint32(key string) uint32 { - return uint32(parseUint(key, 32)) +func GetUint(key string, defaultValue ...uint) uint { + v, err := strconv.ParseUint(os.Getenv(key), 10, 0) + if err != nil { + if len(defaultValue) > 0 { + return defaultValue[0] + } + return 0 + } + return uint(v) } -func GetUint64(key string) uint64 { - return parseUint(key, 64) +func GetUint64(key string, defaultValue ...uint64) uint64 { + v, err := strconv.ParseUint(os.Getenv(key), 10, 64) + if err != nil { + if len(defaultValue) > 0 { + return defaultValue[0] + } + return 0 + } + return v } // Float -func GetFloat32(key string) float32 { - return float32(parseFloat(key, 32)) -} - -func GetFloat64(key string) float64 { - return parseFloat(key, 64) -} - -// Complex - -func GetComplex64(key string) complex64 { - return complex64(parseComplex(key, 64)) -} - -func GetComplex128(key string) complex128 { - return parseComplex(key, 128) +func GetFloat64(key string, defaultValue ...float64) float64 { + v, err := strconv.ParseFloat(os.Getenv(key), 64) + if err != nil { + if len(defaultValue) > 0 { + return defaultValue[0] + } + return 0 + } + return v } diff --git a/env/env_test.go b/env/env_test.go index 5672ceb..7398100 100644 --- a/env/env_test.go +++ b/env/env_test.go @@ -43,14 +43,6 @@ func TestGetSlice(t *testing.T) { testSlice(t, "[]string", GetSlice("SLICE"), []string{"a", "b", "c"}) } -func TestGetBytes(t *testing.T) { - testSlice(t, "[]uint8", GetBytes("SLICE"), []byte{97, 44, 98, 44, 99}) -} - -func TestGetRunes(t *testing.T) { - testSlice(t, "[]int32", GetRunes("SLICE"), []rune{'a', ',', 'b', ',', 'c'}) -} - // Bool func TestGetBool(t *testing.T) { @@ -63,18 +55,6 @@ func TestGetInt(t *testing.T) { test(t, "int", GetInt("INT"), 100) } -func TestGetInt8(t *testing.T) { - test(t, "int8", GetInt8("INT"), 100) -} - -func TestGetInt16(t *testing.T) { - test(t, "int16", GetInt16("INT"), 100) -} - -func TestGetInt32(t *testing.T) { - test(t, "int32", GetInt32("INT"), 100) -} - func TestGetInt64(t *testing.T) { test(t, "int64", GetInt64("INT"), 100) } @@ -85,42 +65,16 @@ func TestGetUint(t *testing.T) { test(t, "uint", GetUint("INT"), 100) } -func TestGetUint8(t *testing.T) { - test(t, "uint8", GetUint8("INT"), 100) -} - -func TestGetUint16(t *testing.T) { - test(t, "uint16", GetUint16("INT"), 100) -} - -func TestGetUint32(t *testing.T) { - test(t, "uint32", GetUint32("INT"), 100) -} - func TestGetUint64(t *testing.T) { test(t, "uint64", GetUint64("INT"), 100) } // Float -func TestGetFloat32(t *testing.T) { - test(t, "float32", GetFloat32("INT"), 100) -} - func TestGetFloat64(t *testing.T) { test(t, "float64", GetFloat64("INT"), 100) } -// Complex - -func TestGetComplex64(t *testing.T) { - test(t, "complex64", GetComplex64("INT"), 100) -} - -func TestGetComplex128(t *testing.T) { - test(t, "complex128", GetComplex128("INT"), 100) -} - // Empty func TestEmptyString(t *testing.T) { @@ -138,3 +92,21 @@ func TestEmptyBool(t *testing.T) { func TestEmptyInt(t *testing.T) { test(t, "int", GetInt("NOT EXIST KEY"), 0) } + +// Default + +func TestDefaultString(t *testing.T) { + test(t, "string", GetString("DEF", "default"), "default") +} + +func TestDefaultSlice(t *testing.T) { + testSlice(t, "[]string", GetSlice("DEF", []string{"a", "b"}), []string{"a", "b"}) +} + +func TestDefaultBool(t *testing.T) { + test(t, "bool", GetBool("DEF", true), true) +} + +func TestDefaultInt(t *testing.T) { + test(t, "int", GetInt("DEF", 100), 100) +} diff --git a/middlewares/fiber.go b/middlewares/fiber.go index d895438..67ae6b8 100644 --- a/middlewares/fiber.go +++ b/middlewares/fiber.go @@ -7,8 +7,8 @@ import ( ) var ( - Debug = env.GetBool("DEBUG") - Host = "https://auth.toscale.io" + Debug = env.GetBool("DEBUG", false) + Host = env.GetString("AUTH_HOST", "https://auth.toscale.io") ) func Cors(c *fiber.Ctx) error {