From b2a4b33e7d7d55ce6ef595dc6aa136b8cd8dc9a3 Mon Sep 17 00:00:00 2001 From: Toby Padilla Date: Thu, 24 Apr 2025 15:03:08 -0600 Subject: [PATCH] feat: use caarlos0/env for config --- .gitignore | 1 + README.md | 17 +++++------ go.mod | 1 + go.sum | 2 ++ internal/config/config.go | 60 ++++++++++----------------------------- 5 files changed, 26 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index a007fea..9b8d11e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/* +data diff --git a/README.md b/README.md index c2a1d0f..613b533 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ This will create the `registry` binary in the `build/` directory. Alternatively, run ```bash -DATABASE_URL="mongodb://localhost:27017" ./build/registry +MCP_REGISTRY_DATABASE_URL="mongodb://localhost:27017" ./build/registry ``` By default, the service will run on `http://localhost:8080`. @@ -83,7 +83,7 @@ For development and testing purposes, the application includes a fake service wi 1. Set the environment to "test": ```bash -export APP_ENV=test +export MCP_REGISTRY_ENVIRONMENT=test ./build/registry ``` @@ -183,19 +183,17 @@ Simple ping endpoint that returns environment configuration information: } ``` - ## Configuration The service can be configured using environment variables: | Variable | Description | Default | |----------|-------------|---------| -| `APP_ENV` | Application environment (production, test) | `production` | -| `APP_VERSION`| Application version | `dev` | -| `DATABASE_URL` | MongoDB connection string | `mongodb://localhost:27017` | -| `DATABASE_NAME` | MongoDB database name | `mcp-registry` | -| `COLLECTION_NAME` | MongoDB collection name for server registry | `servers_v2` | - +| `MCP_REGISTRY_ENVIRONMENT` | Application environment (production, test) | `production` | +| `MCP_REGISTRY_APP_VERSION` | Application version | `dev` | +| `MCP_REGISTRY_DATABASE_URL` | MongoDB connection string | `mongodb://localhost:27017` | +| `MCP_REGISTRY_DATABASE_NAME` | MongoDB database name | `mcp-registry` | +| `MCP_REGISTRY_COLLECTION_NAME` | MongoDB collection name for server registry | `servers_v2` | ## Testing @@ -215,4 +213,3 @@ You can specify specific endpoints to test: ## License See the [LICENSE](LICENSE) file for details. - diff --git a/go.mod b/go.mod index 19b77e4..4174177 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( require ( github.com/KyleBanks/depth v1.2.1 // indirect + github.com/caarlos0/env/v11 v11.3.1 // indirect github.com/go-openapi/jsonpointer v0.21.1 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/spec v0.21.0 // indirect diff --git a/go.sum b/go.sum index f154cfa..daa0051 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA= +github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U= 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/go-openapi/jsonpointer v0.21.1 h1:whnzv/pNXtK2FbX/W9yJfRmE2gsmkfahjMKB0fZvcic= diff --git a/internal/config/config.go b/internal/config/config.go index 37590bb..bf484f3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,56 +1,26 @@ package config -import "os" +import ( + "github.com/caarlos0/env/v11" +) // Config holds the application configuration type Config struct { - ServerAddress string - DatabaseURL string - DatabaseName string - CollectionName string - LogLevel string - Environment string // Added environment field - Version string // Application version + ServerAddress string `env:"MCP_REGISTRY_SERVER_ADDRESS" envDefault:":8080"` + DatabaseURL string `env:"MCP_REGISTRY_DATABASE_URL" envDefault:"mongodb://localhost:27017"` + DatabaseName string `env:"MCP_REGISTRY_DATABASE_NAME" envDefault:"mcp-registry"` + CollectionName string `env:"MCP_REGISTRY_COLLECTION_NAME" envDefault:"servers_v2"` + LogLevel string `env:"MCP_REGISTRY_LOG_LEVEL" envDefault:"info"` + Environment string `env:"MCP_REGISTRY_ENVIRONMENT" envDefault:"production"` + Version string `env:"MCP_REGISTRY_VERSION" envDefault:"dev"` } // NewConfig creates a new configuration with default values func NewConfig() *Config { - // Get environment from OS environment variable or default to "production" - env := os.Getenv("APP_ENV") - if env == "" { - env = "production" - } - - // Get version from environment variable or default to "dev" - version := os.Getenv("APP_VERSION") - if version == "" { - version = "dev" - } - - // Get database URL from environment variable or default to localhost - dbURL := os.Getenv("DATABASE_URL") - if dbURL == "" { - dbURL = "localhost:5432" - } - - // Get database name from environment variable or default to "mcp_registry" - dbName := os.Getenv("DATABASE_NAME") - if dbName == "" { - dbName = "mcp-registry" - } - // Get collection name from environment variable or default to "entries" - collectionName := os.Getenv("COLLECTION_NAME") - if collectionName == "" { - collectionName = "servers_v2" - } - - return &Config{ - ServerAddress: ":8080", - DatabaseURL: dbURL, - DatabaseName: dbName, - CollectionName: collectionName, - LogLevel: "info", - Environment: env, - Version: version, + var cfg Config + err := env.Parse(&cfg) + if err != nil { + panic(err) } + return &cfg }