Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Config Service (FastAPI + MongoDB)

A tiny HTTP service to read and write app-scoped configs stored in MongoDB.

  • Read one config: GET /{app_id}/get?config_id=motd
  • Read all configs for an app: GET /{app_id}/get
  • Write a config (GET): GET /{app_id}/set?config_id=motd&value=hello&auth=some_secret_key
  • Write a config (POST): POST /{app_id}/set with JSON body

The service enforces an API key for write operations via API_AUTH_TOKEN.

Quick start with Docker

  1. Build the image:
docker build -t config-service:latest .
  1. Run MongoDB (example using Docker):
docker run -d --name mongo -p 27017:27017 mongo:7
  1. Run the service:
docker run --rm -p 8000:8000 \
  -e MONGO_URI="mongodb://host.docker.internal:27017" \
  -e API_AUTH_TOKEN="some_secret_key" \
  --name config-service \
  config-service:latest
  • If running Mongo in another container on the same Docker network, you can set MONGO_URI=mongodb://mongo:27017 and attach both containers to the same network.

API

Health

  • GET /health -> {"status":"ok"}

Read

  • GET /{app_id}/get?config_id=motd
    • 200: {"app_id":"test_app","config_id":"motd","value":"hello","updated_at":"...","created_at":"..." }
    • 404 if not found.
  • GET /{app_id}/get
    • 200: {"app_id":"test_app","count":2,"configs":{"motd":"hello","feature_flag":true}}

Write (requires auth)

Provide the token in one of:

  • Query param: ?auth=API_AUTH_TOKEN

  • Header: X-API-KEY: API_AUTH_TOKEN

  • POST body field: "auth": "API_AUTH_TOKEN"

  • GET style (matches your example):

    • GET /{app_id}/set?config_id=motd&value=hello&auth=some_secret_key
    • Returns:
      {
        "success": true,
        "upserted": true,
        "config": {
          "app_id": "test_app",
          "config_id": "motd",
          "value": "hello",
          "updated_at": "...",
          "created_at": "..."
        }
      }
  • POST style (recommended):

    • POST /{app_id}/set
    • Body:
      {
        "config_id": "motd",
        "value": "hello",
        "auth": "some_secret_key"
      }

Notes on values

  • The service will attempt to parse JSON-like strings for value. Examples that will parse as JSON:
    • {"a":1}, [1,2,3], "hello", 123, true, false, null
  • If parsing fails, the value is stored as a string.

Local development (without Docker)

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

export MONGO_URI="mongodb://localhost:27017"
export API_AUTH_TOKEN="some_secret_key"

uvicorn app:app --reload

Open docs at: http://localhost:8000/docs

Data model

  • Collection: configs
  • Document:
    {
      "app_id": "test_app",
      "config_id": "motd",
      "value": "hello",
      "created_at": "2025-01-01T00:00:00Z",
      "updated_at": "2025-01-01T00:00:00Z"
    }
  • Unique index on (app_id, config_id).

Security

  • Set API_AUTH_TOKEN to a strong secret.
  • Prefer POST with header X-API-KEY to avoid logging secrets in URLs.
  • Restrict network access as appropriate for your environment.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages