Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created June 6, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/f277f9c898319c0948befb09cdf0508b to your computer and use it in GitHub Desktop.
Save podhmo/f277f9c898319c0948befb09cdf0508b to your computer and use it in GitHub Desktop.
from egoist.app import create_app, SettingsDict, App
settings: SettingsDict = {"rootdir": "output", "here": __file__}
app = create_app(settings)
def setup_server(app: App) -> None:
from egoist.ext.serverprocess.lazyparams import find_free_port, create_sentinel_file
app.include("egoist.ext.serverprocess") # for add_server_process
app.add_server_process(
"uvicorn server:app --port {port}",
params=dict(port=find_free_port),
name="api-server",
env={"SENTINEL": create_sentinel_file},
)
def run() -> None:
from egoist.ext.serverprocess.runtime import get_discovery, get_http_client
url = get_discovery().lookup("api-server")
print("->", url)
res = get_http_client().get(url)
res.raise_for_status()
print("<-", res.json())
if __name__ == "__main__":
app.include(setup_server)
app.commit(dry_run=False)
run()
default:
python main.py
serve:
uvicorn server:app --reload
from fastapi import FastAPI
app = FastAPI()
@app.on_event("startup")
async def startup_event():
import os
from egoist.ext.serverprocess.spawn import FileConnectionChecker
sentinel = os.environ.get("SENTINEL")
if sentinel is not None:
checker = FileConnectionChecker(sentinel=sentinel)
assert checker.pong() is True
@app.get("/")
async def root():
return {"message": "Hello World"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment