feat: use caarlos0/env for config
This commit is contained in:
parent
ae24924f39
commit
b2a4b33e7d
5 changed files with 26 additions and 55 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
build/*
|
||||
data
|
||||
|
|
|
|||
17
README.md
17
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.
|
||||
|
||||
|
|
|
|||
1
go.mod
1
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
|
||||
|
|
|
|||
2
go.sum
2
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=
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue