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/*
|
build/*
|
||||||
|
data
|
||||||
|
|
|
||||||
17
README.md
17
README.md
|
|
@ -57,7 +57,7 @@ This will create the `registry` binary in the `build/` directory.
|
||||||
|
|
||||||
Alternatively, run
|
Alternatively, run
|
||||||
```bash
|
```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`.
|
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":
|
1. Set the environment to "test":
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export APP_ENV=test
|
export MCP_REGISTRY_ENVIRONMENT=test
|
||||||
./build/registry
|
./build/registry
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -183,19 +183,17 @@ Simple ping endpoint that returns environment configuration information:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
The service can be configured using environment variables:
|
The service can be configured using environment variables:
|
||||||
|
|
||||||
| Variable | Description | Default |
|
| Variable | Description | Default |
|
||||||
|----------|-------------|---------|
|
|----------|-------------|---------|
|
||||||
| `APP_ENV` | Application environment (production, test) | `production` |
|
| `MCP_REGISTRY_ENVIRONMENT` | Application environment (production, test) | `production` |
|
||||||
| `APP_VERSION`| Application version | `dev` |
|
| `MCP_REGISTRY_APP_VERSION` | Application version | `dev` |
|
||||||
| `DATABASE_URL` | MongoDB connection string | `mongodb://localhost:27017` |
|
| `MCP_REGISTRY_DATABASE_URL` | MongoDB connection string | `mongodb://localhost:27017` |
|
||||||
| `DATABASE_NAME` | MongoDB database name | `mcp-registry` |
|
| `MCP_REGISTRY_DATABASE_NAME` | MongoDB database name | `mcp-registry` |
|
||||||
| `COLLECTION_NAME` | MongoDB collection name for server registry | `servers_v2` |
|
| `MCP_REGISTRY_COLLECTION_NAME` | MongoDB collection name for server registry | `servers_v2` |
|
||||||
|
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
|
|
@ -215,4 +213,3 @@ You can specify specific endpoints to test:
|
||||||
## License
|
## License
|
||||||
|
|
||||||
See the [LICENSE](LICENSE) file for details.
|
See the [LICENSE](LICENSE) file for details.
|
||||||
|
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -11,6 +11,7 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/KyleBanks/depth v1.2.1 // indirect
|
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/jsonpointer v0.21.1 // indirect
|
||||||
github.com/go-openapi/jsonreference v0.21.0 // indirect
|
github.com/go-openapi/jsonreference v0.21.0 // indirect
|
||||||
github.com/go-openapi/spec 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 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
|
||||||
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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=
|
github.com/go-openapi/jsonpointer v0.21.1 h1:whnzv/pNXtK2FbX/W9yJfRmE2gsmkfahjMKB0fZvcic=
|
||||||
|
|
|
||||||
|
|
@ -1,56 +1,26 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"github.com/caarlos0/env/v11"
|
||||||
|
)
|
||||||
|
|
||||||
// Config holds the application configuration
|
// Config holds the application configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ServerAddress string
|
ServerAddress string `env:"MCP_REGISTRY_SERVER_ADDRESS" envDefault:":8080"`
|
||||||
DatabaseURL string
|
DatabaseURL string `env:"MCP_REGISTRY_DATABASE_URL" envDefault:"mongodb://localhost:27017"`
|
||||||
DatabaseName string
|
DatabaseName string `env:"MCP_REGISTRY_DATABASE_NAME" envDefault:"mcp-registry"`
|
||||||
CollectionName string
|
CollectionName string `env:"MCP_REGISTRY_COLLECTION_NAME" envDefault:"servers_v2"`
|
||||||
LogLevel string
|
LogLevel string `env:"MCP_REGISTRY_LOG_LEVEL" envDefault:"info"`
|
||||||
Environment string // Added environment field
|
Environment string `env:"MCP_REGISTRY_ENVIRONMENT" envDefault:"production"`
|
||||||
Version string // Application version
|
Version string `env:"MCP_REGISTRY_VERSION" envDefault:"dev"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig creates a new configuration with default values
|
// NewConfig creates a new configuration with default values
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
// Get environment from OS environment variable or default to "production"
|
var cfg Config
|
||||||
env := os.Getenv("APP_ENV")
|
err := env.Parse(&cfg)
|
||||||
if env == "" {
|
if err != nil {
|
||||||
env = "production"
|
panic(err)
|
||||||
}
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
}
|
}
|
||||||
|
return &cfg
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue