Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate envs lib #397

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/caarlos0/env/v11 v11.2.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/davidebianchi/go-jsonclient v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMU
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2/go.mod h1:RnUjnIXxEJcL6BgCvNyzCCRzZcxCgsZCi+RNlvYor5Q=
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
148 changes: 24 additions & 124 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"net/http"
"strings"

envlib "github.com/caarlos0/env/v11"
"github.com/gorilla/mux"
"github.com/mia-platform/configlib"
)

const (
Expand All @@ -37,127 +37,27 @@ const (
// EnvironmentVariables struct with the mapping of desired
// environment variables.
type EnvironmentVariables struct {
LogLevel string
HTTPPort string
ServiceVersion string
TargetServiceHost string
TargetServiceOASPath string
OPAModulesDirectory string
APIPermissionsFilePath string
UserPropertiesHeader string
UserGroupsHeader string
UserIdHeader string
ClientTypeHeader string
BindingsCrudServiceURL string
MongoDBUrl string
MongoDBConnectionMaxIdleTimeMs int
RolesCollectionName string
BindingsCollectionName string
PathPrefixStandalone string
DelayShutdownSeconds int
Standalone bool
AdditionalHeadersToProxy string
ExposeMetrics bool
}

var EnvVariablesConfig = []configlib.EnvConfig{
{
Key: "LOG_LEVEL",
Variable: "LogLevel",
DefaultValue: "info",
},
{
Key: "HTTP_PORT",
Variable: "HTTPPort",
DefaultValue: "8080",
},
{
Key: "SERVICE_VERSION",
Variable: "ServiceVersion",
DefaultValue: "latest",
},
{
Key: targetServiceHostEnvKey,
Variable: "TargetServiceHost",
},
{
Key: targetServiceOASPathEnvKey,
Variable: "TargetServiceOASPath",
},
{
Key: "OPA_MODULES_DIRECTORY",
Variable: "OPAModulesDirectory",
Required: true,
},
{
Key: apiPermissionsFilePathEnvKey,
Variable: "APIPermissionsFilePath",
},
{
Key: "USER_PROPERTIES_HEADER_KEY",
Variable: "UserPropertiesHeader",
DefaultValue: "miauserproperties",
},
{
Key: "USER_GROUPS_HEADER_KEY",
Variable: "UserGroupsHeader",
DefaultValue: "miausergroups",
},
{
Key: "USER_ID_HEADER_KEY",
Variable: "UserIdHeader",
DefaultValue: "miauserid",
},
{
Key: "CLIENT_TYPE_HEADER_KEY",
Variable: "ClientTypeHeader",
DefaultValue: "Client-Type",
},
{
Key: "DELAY_SHUTDOWN_SECONDS",
Variable: "DelayShutdownSeconds",
DefaultValue: "10",
},
{
Key: "MONGODB_URL",
Variable: "MongoDBUrl",
},
{
Key: "MONGODB_CONNECTION_MAX_IDLE_TIME_MS",
Variable: "MongoDBConnectionMaxIdleTimeMs",
DefaultValue: "1000",
},
{
Key: "BINDINGS_COLLECTION_NAME",
Variable: "BindingsCollectionName",
},
{
Key: "ROLES_COLLECTION_NAME",
Variable: "RolesCollectionName",
},
{
Key: standaloneEnvKey,
Variable: "Standalone",
},
{
Key: "PATH_PREFIX_STANDALONE",
Variable: "PathPrefixStandalone",
DefaultValue: "/eval",
},
{
Key: bindingsCrudServiceURL,
Variable: "BindingsCrudServiceURL",
},
{
Key: "ADDITIONAL_HEADERS_TO_PROXY",
Variable: "AdditionalHeadersToProxy",
DefaultValue: "miauserid",
},
{
Key: "EXPOSE_METRICS",
Variable: "ExposeMetrics",
DefaultValue: "true",
},
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
HTTPPort string `env:"HTTP_PORT" envDefault:"8080"`
ServiceVersion string `env:"SERVICE_VERSION" envDefault:"latest"`
TargetServiceHost string `env:"TARGET_SERVICE_HOST"`
TargetServiceOASPath string `env:"TARGET_SERVICE_OAS_PATH"`
OPAModulesDirectory string `env:"OPA_MODULES_DIRECTORY,required"`
APIPermissionsFilePath string `env:"API_PERMISSIONS_FILE_PATH"`
UserPropertiesHeader string `env:"USER_PROPERTIES_HEADER_KEY" envDefault:"miauserproperties"`
UserGroupsHeader string `env:"USER_GROUPS_HEADER_KEY" envDefault:"miausergroups"`
UserIdHeader string `env:"USER_ID_HEADER_KEY" envDefault:"miauserid"`
ClientTypeHeader string `env:"CLIENT_TYPE_HEADER_KEY" envDefault:"Client-Type"`
BindingsCrudServiceURL string `env:"BINDINGS_CRUD_SERVICE_URL"`
MongoDBUrl string `env:"MONGODB_URL"`
MongoDBConnectionMaxIdleTimeMs int `env:"MONGODB_CONNECTION_MAX_IDLE_TIME_MS" envDefault:"1000"`
RolesCollectionName string `env:"ROLES_COLLECTION_NAME"`
BindingsCollectionName string `env:"BINDINGS_COLLECTION_NAME"`
PathPrefixStandalone string `env:"PATH_PREFIX_STANDALONE" envDefault:"/eval"`
DelayShutdownSeconds int `env:"DELAY_SHUTDOWN_SECONDS" envDefault:"10"`
Standalone bool `env:"STANDALONE"`
AdditionalHeadersToProxy string `env:"ADDITIONAL_HEADERS_TO_PROXY" envDefault:"miauserid"`
ExposeMetrics bool `env:"EXPOSE_METRICS" envDefault:"true"`
}

type EnvKey struct{}
Expand All @@ -184,8 +84,8 @@ func GetEnv(requestContext context.Context) (EnvironmentVariables, error) {
}

func GetEnvOrDie() EnvironmentVariables {
var env EnvironmentVariables
if err := configlib.GetEnvVariables(EnvVariablesConfig, &env); err != nil {
env, err := envlib.ParseAs[EnvironmentVariables]()
if err != nil {
panic(err.Error())
}

Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ import (
)

func main() {
entrypoint(make(chan os.Signal, 1))
env := config.GetEnvOrDie()

entrypoint(make(chan os.Signal, 1), env)
os.Exit(0)
}

func entrypoint(shutdown chan os.Signal) {
env := config.GetEnvOrDie()

func entrypoint(shutdown chan os.Signal, env config.EnvironmentVariables) {
// Init logger instance.
log, err := glogrus.InitHelper(glogrus.InitOptions{Level: env.LogLevel})
if err != nil {
Expand Down
Loading