7. Services via Port binding
Export services via port binding
- The twelve-factor app is completely self-contained and does not rely on runtime injection of a webserver into the execution environment to create a web-facing service.
- The web app exports HTTP as a service by binding to a port, and listening to requests coming in on that port.
In most languages this is done by using a webserver library.
- HTTP is not the only service that can be exported by port binding.
- Nearly any kind of server software can be run via a process binding to a port and awaiting incoming requests.
Examples include ejabberd (speaking XMPP), and Redis (speaking the Redis protocol).
FastAPI
pip install fastapi
pip install uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
To run a fastapi application Specify HOST 0.0.0.0 and Port 8000 using environment variables
uvicorn app.main:app --host ${HOST} --port ${PORT}