This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Application

    Build and Deploy

    Learn to build and deploy a web application in Piglet Run.

    Create a FastAPI App

    1. Set Up Project

    cd ~/workspace
    mkdir myapp && cd myapp
    python -m venv venv
    source venv/bin/activate
    pip install fastapi uvicorn psycopg[binary]
    

    2. Create Application

    Create main.py:

    from fastapi import FastAPI
    import psycopg
    
    app = FastAPI()
    
    @app.get("/")
    def root():
        return {"message": "Hello from Piglet Run!"}
    
    @app.get("/users")
    def get_users():
        conn = psycopg.connect("postgres://postgres@localhost/postgres")
        cur = conn.execute("SELECT * FROM users")
        return cur.fetchall()
    
    if __name__ == "__main__":
        import uvicorn
        uvicorn.run(app, host="0.0.0.0", port=8000)
    

    3. Run Locally

    python main.py
    

    4. Deploy with Nginx

    See Deploy Task for production deployment.

    Next Steps