Allow setting port and log level in config
Change-Id: Ib9e4ac06292aeaad9154a9f11997ed3bd1ab6779
diff --git a/README.md b/README.md
index c44e080..8a9d0df 100644
--- a/README.md
+++ b/README.md
@@ -22,8 +22,8 @@
- `--config` or `-c`: YAML configuration file containing mapping directives and global settings (optional)
- `--mappings` or `-m`: Individual YAML mapping files to load (can be used multiple times, optional)
-- `--port` or `-p`: Port to listen on (default: 8080)
-- `--log-level` or `-l`: Log level (debug, info, warn, error) (default: info)
+- `--port` or `-p`: Port to listen on (overrides config file, defaults to 3000 if not specified)
+- `--log-level` or `-l`: Log level (debug, info, warn, error) (overrides config file, defaults to warn if not specified)
- `--help` or `-h`: Show help message
**Note**: At least one mapping source must be provided
@@ -32,11 +32,13 @@
KoralPipe-TermMapper supports loading configuration from multiple sources:
-1. **Main Configuration File** (`-c`): Contains global settings (SDK, server endpoints) and optional mapping lists
+1. **Main Configuration File** (`-c`): Contains global settings (SDK, server endpoints, port, log level) and optional mapping lists
2. **Individual Mapping Files** (`-m`): Contains single mapping lists, can be specified multiple times
The main configuration provides global settings, and all mapping lists from both sources are combined. Duplicate mapping IDs across all sources will result in an error.
+### Configuration File Format
+
Configurations can contain global settings and mapping lists (used with the `-c` flag):
```yaml
@@ -46,6 +48,12 @@
# Optional: Custom server endpoint for Kalamar plugin integration
server: "https://custom.example.com/"
+# Optional: Port to listen on (default: 3000)
+port: 8080
+
+# Optional: Log level - debug, info, warn, error (default: warn)
+loglevel: info
+
# Optional: Mapping lists (same format as individual mapping files)
lists:
- id: mapping-list-id
@@ -71,12 +79,16 @@
- "[pattern2] <> [replacement2]"
```
-The `sdk` and `server` fields in the main configuration file are optional and override the default endpoints used for Kalamar plugin integration:
+Command line arguments take precedence over configuration file values:
+
+The `sdk`, `server`, `port`, and `loglevel` fields in the main configuration file are optional and override the following default values:
- **`sdk`**: Custom SDK JavaScript file URL (default: `https://korap.ids-mannheim.de/js/korap-plugin-latest.js`)
- **`server`**: Custom server endpoint URL (default: `https://korap.ids-mannheim.de/`)
+- **`port`**: Server port (default: `3000`)
+- **`loglevel`**: Log level (default: `warn`)
-These values are applied during configuration parsing and affect the HTML plugin page served at the root endpoint (`/`). When using only individual mapping files (`-m` flags), default values are used.
+These values are applied during configuration parsing. When using only individual mapping files (`-m` flags), default values are used unless overridden by command line arguments.
### Mapping Rules
diff --git a/cmd/termmapper/main.go b/cmd/termmapper/main.go
index 2a2f7e3..aa940ac 100644
--- a/cmd/termmapper/main.go
+++ b/cmd/termmapper/main.go
@@ -21,10 +21,10 @@
)
type appConfig struct {
- Port int `kong:"short='p',default='8080',help='Port to listen on'"`
+ Port *int `kong:"short='p',help='Port to listen on'"`
Config string `kong:"short='c',help='YAML configuration file containing mapping directives and global settings'"`
Mappings []string `kong:"short='m',help='Individual YAML mapping files to load'"`
- LogLevel string `kong:"short='l',default='info',help='Log level (debug, info, warn, error)'"`
+ LogLevel *string `kong:"short='l',help='Log level (debug, info, warn, error)'"`
}
// TemplateData holds data for the Kalamar plugin template
@@ -79,15 +79,26 @@
log.Fatal().Msg("At least one configuration source must be provided: use -c for main config file or -m for mapping files")
}
- // Set up logging
- setupLogger(cfg.LogLevel)
-
// Load configuration from multiple sources
yamlConfig, err := config.LoadFromSources(cfg.Config, cfg.Mappings)
if err != nil {
log.Fatal().Err(err).Msg("Failed to load configuration")
}
+ finalPort := yamlConfig.Port
+ finalLogLevel := yamlConfig.LogLevel
+
+ // Use command line values if provided (they override config file)
+ if cfg.Port != nil {
+ finalPort = *cfg.Port
+ }
+ if cfg.LogLevel != nil {
+ finalLogLevel = *cfg.LogLevel
+ }
+
+ // Set up logging with the final log level
+ setupLogger(finalLogLevel)
+
// Create a new mapper instance
m, err := mapper.NewMapper(yamlConfig.Lists)
if err != nil {
@@ -105,8 +116,8 @@
// Start server
go func() {
- log.Info().Int("port", cfg.Port).Msg("Starting server")
- if err := app.Listen(fmt.Sprintf(":%d", cfg.Port)); err != nil {
+ log.Info().Int("port", finalPort).Msg("Starting server")
+ if err := app.Listen(fmt.Sprintf(":%d", finalPort)); err != nil {
log.Fatal().Err(err).Msg("Server error")
}
}()
diff --git a/config/config.go b/config/config.go
index 46d6639..ff0db31 100644
--- a/config/config.go
+++ b/config/config.go
@@ -10,8 +10,10 @@
)
const (
- defaultServer = "https://korap.ids-mannheim.de/"
- defaultSDK = "https://korap.ids-mannheim.de/js/korap-plugin-latest.js"
+ defaultServer = "https://korap.ids-mannheim.de/"
+ defaultSDK = "https://korap.ids-mannheim.de/js/korap-plugin-latest.js"
+ defaultPort = 3000
+ defaultLogLevel = "warn"
)
// MappingRule represents a single mapping rule in the configuration
@@ -29,9 +31,11 @@
// MappingConfig represents the root configuration containing multiple mapping lists
type MappingConfig struct {
- SDK string `yaml:"sdk,omitempty"`
- Server string `yaml:"server,omitempty"`
- Lists []MappingList `yaml:"lists,omitempty"`
+ SDK string `yaml:"sdk,omitempty"`
+ Server string `yaml:"server,omitempty"`
+ Port int `yaml:"port,omitempty"`
+ LogLevel string `yaml:"loglevel,omitempty"`
+ Lists []MappingList `yaml:"lists,omitempty"`
}
// LoadFromSources loads configuration from multiple sources and merges them:
@@ -145,6 +149,12 @@
if config.Server == "" {
config.Server = defaultServer
}
+ if config.Port == 0 {
+ config.Port = defaultPort
+ }
+ if config.LogLevel == "" {
+ config.LogLevel = defaultLogLevel
+ }
}
// validateMappingLists validates a slice of mapping lists