Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

Building an Internal API Platform with Python, Flask, Docker, and Apache Reverse Proxy

Posted on 2025-12-092025-12-09 by Rico

A lightweight, secure, and maintainable framework for executing dynamic Python scripts as internal APIs.

In many enterprise IT environments, teams frequently need lightweight APIs for internal systems, automation, scheduled tasks, diagnostic tools, or ad-hoc scripts. These APIs don’t always justify a full microservice architecture, but they must be:

  • Easy to extend
  • Secure
  • Maintainable
  • Fast to deploy

To meet these needs, I built a lightweight Internal API Platform using:

  • Python + Flask
  • Docker
  • Apache Reverse Proxy
  • Dynamic execution of .py scripts
  • Namespace separation (root, api, admin)
  • IP whitelisting + API Key + Admin Key security
  • Centralized logging with trace IDs
  • Health check, route listing, and metrics endpoints

This article documents the full architecture and implementation.


✨ 1. Goal: A Fast, Secure, Maintainable Internal API Platform

The platform’s objectives:

✔ Execute Python scripts directly as API endpoints

Examples:

/api/hello.py
/admin/stats.py
/report_json.py

Each script only needs to define:

def main(request):
    ...

and it becomes a fully functional API.


✔ Namespace-based routing

Automatically maps URL prefixes to folder structures:

URL PrefixFolderPurpose
/xxx.py/app/xxx.pyGeneral scripts
/api/xxx.py/app/api/xxx.pyInternal API
/admin/xxx.py/app/admin/xxx.pyAdmin / privileged API

✔ Security

Built-in protection:

  • IP Whitelist
  • API Key (for /api)
  • Admin Key (for /admin)

Example Docker environment variables:

API_TRUSTED_IPS="172.18.0.1,127.0.0.1"
API_KEY="secret123"
ADMIN_API_KEY="admin456"

✔ Unified Enterprise Logging

Every request is logged with:

  • Endpoint & namespace
  • Client IP
  • Query parameters
  • Execution time
  • Status code
  • Trace ID
  • Stacktrace for script exceptions

Logs stored at:

/opt/docker/python/app/logs/framework.log

✔ Platform-Level APIs

To support observability and operations:

  • /_platform/health: health & uptime
  • /_platform/routes: list available endpoints
  • /_platform/metrics: real-time metrics

🧱 2. Architecture Overview

The architecture is simple, containerized, and easy to maintain:

Client → Apache Reverse Proxy → Flask Internal API Platform → Dynamically execute Python scripts

For every *.py call, the platform:

  1. Resolves the correct script based on URL
  2. Dynamically imports and executes it
  3. Returns output (text/JSON/file)
  4. Logs execution details
  5. Attaches a trace ID

📂 3. Directory Structure

The platform’s directory layout:

/opt/docker/python/app
├── app.py                 # Platform dispatcher
├── logs/
│   └── framework.log
├── api/
│   ├── hello.py
│   └── report_json.py
└── admin/
    └── stats.py

Scripts placed in these folders automatically become API endpoints.


🧠 4. Core Technology: The Dynamic Dispatcher

At the heart of the platform is Python’s dynamic import mechanism:

spec = importlib.util.spec_from_file_location(module_name, script_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
handler = getattr(module, "handle", None) or getattr(module, "main", None)
result = handler(request)

This enables:

  • Zero-config API creation
  • Scripts acting as independent endpoints
  • Rapid internal tooling and automation

Adding a new script = dropping a .py file into a folder.


🔐 5. Security Layer

The platform implements three security controls:


✔ 1. IP Whitelist

if ALLOWED_IP_SET and client_ip not in ALLOWED_IP_SET:
    return make_response("Forbidden (IP not allowed)", 403)

✔ 2. API Key for /api

if area == "api" and API_KEY:
    if request.headers.get("X-API-Key") != API_KEY:
        return make_response("Forbidden (invalid API key)", 403)

✔ 3. Admin Key for /admin

if area == "admin" and ADMIN_API_KEY:
    if request.headers.get("X-Admin-Key") != ADMIN_API_KEY:
        return make_response("Forbidden (invalid Admin key)", 403)

This structure prevents accidental exposure of internal tools and ensures security without over-engineering.


📈 6. Platform-Level Endpoints

✔ 1. Health Check

/_platform/health

{
  "status": "ok",
  "uptime_sec": 1623,
  "requests_total": 83,
  "errors_total": 1
}

✔ 2. List All Script Endpoints

/_platform/routes

{
  "root": ["report.py"],
  "api": ["hello.py", "report_json.py"],
  "admin": ["stats.py"]
}

✔ 3. Metrics

/_platform/metrics

{
  "uptime_sec": 1644,
  "requests_total": 95,
  "requests_by_area": {
    "root": 30,
    "api": 50,
    "admin": 15
  },
  "errors_total": 1
}

👨‍💻 7. Writing API Scripts (Extremely Simple)

Each endpoint is just a .py file.


✔ /api/hello.py

def main(request):
    name = request.args.get("name", "Guest")
    return {"message": f"Hello, {name}!"}

✔ /admin/stats.py

def main(request):
    return {"status": "admin ok"}

✔ /api/download.py (File download)

from flask import send_file

def main(request):
    return send_file("/app/data/sample.txt", as_attachment=True)

The platform handles:

  • JSON conversion
  • Response wrapping
  • Error logging
  • Trace ID generation

🐳 8. Docker Deployment

Dockerfile

FROM python:3.11-slim
WORKDIR /app
COPY app/ /app/
RUN pip install flask gunicorn
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]

Run Command

docker run -d \
  --name pyapp \
  --network intranet-net \
  -v /opt/docker/python/app:/app \
  -e API_TRUSTED_IPS="192.168.1.10,127.0.0.1" \
  -e API_KEY="secret123" \
  -e ADMIN_API_KEY="admin456" \
  mypython:1.0

🔄 9. Apache Reverse Proxy

ProxyPreserveHost On
ProxyPass        /  http://pyapp:5000/
ProxyPassReverse /  http://pyapp:5000/

🎯 Conclusion: A Practical and Enterprise-Ready Internal API Framework

This Internal API Platform provides:

CapabilityDescription
🧠 Dynamic Python executionEvery .py file becomes an API automatically
📂 Namespaced routingroot / api / admin
🔐 SecurityIP whitelist, API Key, Admin Key
📝 Centralized loggingtrace_id, stacktrace, execution time
📈 Observabilityhealth, routes, metrics
⚡ Rapid developmentadd script → instant endpoint
🧰 IT-friendlyideal for automation, tools, internal integrations

This framework is perfect for:

  • Internal reporting APIs
  • System automation
  • Backend tools
  • Scheduled tasks
  • Lightweight internal microservices

Recent Posts

  • Planning and Key Considerations for IT Data Room Construction
  • IT 機房建置的規劃與考量
  • Token/s and Concurrency:
  • Token/s 與並發:企業導入大型語言模型時,最容易被誤解的兩個指標
  • Running OpenCode AI using Docker

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • January 2026
  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • CUDA
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • MIS
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • Python
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2026 Nuface Blog | Powered by Superbs Personal Blog theme