Ani
Ani

Reputation: 351

Azure App Service FastApi server not running on specified port

I am running FastApi server on Azure App Service.
This is by linking the code to github (and not using container).
I have specified startup command as: python -m uvicorn routes:app --host 0.0.0.0 --port 8000 enter image description here when I run type in browser: https://fastapi-sappv01.azurewebsites.net:8000/ I get error

It gives errenter image description hereor

Also I have a streamlit frontend which sends request to the server.

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='stock-app2.azurewebsites.net', port=8080): Max retries exceeded with url: /symbols (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x7fcc2e9e9970>: Failed to resolve 'stock-app2.azurewebsites.net' ([Errno 8] nodename nor servname provided, or not known)"))

Questions
1: How to debug the issue?
2: How can I view the server side logs? (print statements in code?)
3: If I do not specify the --port parameter, which port should I expect the server to run?

Upvotes: 0

Views: 729

Answers (1)

Aslesha Kantamsetti
Aslesha Kantamsetti

Reputation: 1501

I created a simple web app with FastAPI backend and Streamlit as frontend. It was successfully deployed to Azure.

For deploying FastAPI and Streamlit, I created two web apps in the Azure portal.

Below is my complete code for the backend and frontend.

backend/main.py

from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class Item(BaseModel):
    name: str
    description: str = None
    price: float
    tax: float = None
@app.get("/")
def read_root():
    return {"message": "Welcome from the API"}
@app.post("/items")
async def create_item(item: Item):
    return item
if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8080)

backend/requirements.txt:

fastapi
uvicorn

Ensure the backend URL is the correct one in the frontend.

frontend/streamlit_app.py:

import streamlit as st
import requests
st.title('FastAPI - Streamlit Integration')
def create_item(item_data):
    url = "https://kapythonapi.azurewebsites.net/items"
    response = requests.post(url, json=item_data)
    return response.json()
st.sidebar.title('Create Item')
name = st.sidebar.text_input('Name')
description = st.sidebar.text_area('Description')
price = st.sidebar.number_input('Price')
tax = st.sidebar.number_input('Tax')
if st.sidebar.button('Create'):
    item_data = {"name": name, "description": description, "price": price, "tax": tax}
    response = create_item(item_data)
    st.write('Response from backend:', response)

frontend/requirements.txt:

streamlit

After deploying both the frontend and backend to the Azure app service, configure the startup commands in the Configuration section of the Azure web app as shown below.

For Fastapi:

 gunicorn --worker-class uvicorn.workers.UvicornWorker --timeout 600 --access-logfile '-' --error-logfile '-' main:app

For Streamlit app:

python -m streamlit run <YourFileName>.py --server.port 8000 --server.address 0.0.0.0

enter image description here

enter image description here

If you encounter any CORS errors, enable CORS in the FastAPI backend app as shown below.

enter image description here

Azure App Service Output:

enter image description here

enter image description here

Upvotes: 0

Related Questions