Skip to content

Commit

Permalink
feat: support setting log level (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc authored Nov 15, 2024
1 parent 9b06cad commit eacb901
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- [Missing `X-Forwarded-For` header](#missing-x-forwarded-for-header)
- [Missing `X-Forwarded-Method` header](#missing-x-forwarded-method-header)
- [Blocked country](#blocked-country)
- [Request authorized](#request-authorized)
- [Authorized by country](#authorized-by-country)
- [Roadmap](#roadmap)

## Introduction
Expand Down Expand Up @@ -162,17 +162,18 @@ Check if the service is healthy.

The following environment variables can be used to configure Geoblock:

| Variable | Description | Default |
| :---------------- | :----------------------------- | :-------------- |
| `GEOBLOCK_CONFIG` | Path to the configuration file | `./config.yaml` |
| `GEOBLOCK_PORT` | Port to listen on | `8080` |
| Variable | Description | Default |
| :------------------- | :----------------------------- | :-------------- |
| `GEOBLOCK_CONFIG` | Path to the configuration file | `./config.yaml` |
| `GEOBLOCK_PORT` | Port to listen on | `8080` |
| `GEOBLOCK_LOG_LEVEL` | Log level | `info` |

## Manual testing

Start geoblock with the provided example configuration:

```bash
GEOBLOCK_CONFIG=examples/config.yaml GEOBLOCK_PORT=8080 make run
GEOBLOCK_CONFIG=examples/config.yaml GEOBLOCK_PORT=8080 GEOBLOCK_LOG_LEVEL=debug make run
```

### Missing `X-Forwarded-For` and `X-Forwarded-Host` and `X-Forwarded-Method` headers
Expand Down Expand Up @@ -210,15 +211,15 @@ X-Forwarded-Host: example.com
```http
GET http://localhost:8080/v1/forward-auth
X-Forwarded-For: 8.8.8.8
X-Forwarded-Host: example.com
X-Forwarded-Host: example.org
X-Forwarded-Method: GET
```

### Request authorized
### Authorized by country

```http
GET http://localhost:8080/v1/forward-auth
X-Forwarded-For: 127.0.0.1
X-Forwarded-For: 8.8.8.8
X-Forwarded-Host: example.com
X-Forwarded-Method: GET
```
Expand All @@ -228,10 +229,11 @@ X-Forwarded-Method: GET
- [x] Support environment variables
- [x] Docker image
- [x] Publish Docker image
- [x] Auto-update databases
- [x] Auto-reload configuration
- [ ] Add metrics
- [ ] Write documentation
- [ ] Auto-update databases
- [ ] Cache responses
- [ ] Add metrics
- [ ] Add more tests
- [ ] Add e2e tests
- [ ] ~~Cache databases~~
- [ ] Support command line arguments
18 changes: 17 additions & 1 deletion cmd/geoblock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ func getEnv(key, fallback string) string {
type appOptions struct {
configPath string
serverPort string
logLevel string
}

// getOptions returns the application options from the environment variables.
func getOptions() *appOptions {
return &appOptions{
configPath: getEnv("GEOBLOCK_CONFIG", "config.yaml"),
serverPort: getEnv("GEOBLOCK_PORT", "8080"),
logLevel: getEnv("GEOBLOCK_LOG_LEVEL", "info"),
}
}

Expand Down Expand Up @@ -86,12 +89,25 @@ func autoReload(engine *rules.Engine, config string) {
}
}

func main() {
// configureLogger configures the logger with the given log level and sets the
// formatter.
func configureLogger(level string) {
// This should be done first, before any log message is emitted to avoid
// inconsistent log messages.
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})

if lvl, err := log.ParseLevel(level); err != nil {
log.Warnf("Invalid log level: %s", level)
} else {
log.SetLevel(lvl)
}
}

func main() {
options := getOptions()
configureLogger(options.logLevel)

log.Info("Loading configuration file")
config, err := schema.ReadFile(options.configPath)
Expand Down
22 changes: 6 additions & 16 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@
access_control:
default_policy: deny
rules:
- domains:
- bc.gas.ovh
policy: allow

- networks:
- 10.0.0.0/8
- 127.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
policy: allow

- autonomous_systems:
- 1234
- 5678
- domains:
- example.org
countries:
- US
policy: deny

- countries:
- FR
policy: allow

- domains:
- jellyfin.rocha.io
- jellyseerr.rocha.io
- wizarr.rocha.io
- example.com
countries:
- BR
- FR
- US
policy: allow

0 comments on commit eacb901

Please sign in to comment.