Context: Ruikang Guo resume 2023.08.03
pip install fastapi uvicorn[standard] llama_index openai
python -m uvicorn Main:app --reload
/
returns hello mundo
/api/v1/{your question here}
returns a response
CORS is a security feature implemented by web browsers to prevent web pages from making requests to a different domain than the one that served the web page. This security measure is in place to protect users from potential security vulnerabilities.
If we don’t do any configuration, the application cannot return anything to the frontend due to CORS error.
# Add these lines
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Configure CORS
origins = ["https://rkguo.xyz"] # Replace with your allowed origin(s)
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True, # Optional
allow_methods=["*"], # Optional, You can adjust the HTTP methods as needed
allow_headers=["*"], # Optional, You can adjust the allowed headers as needed
)
Deployed to Heroku
-
Create requirements.txt
pip freeze > requirements.txt
-
Create a “Procfile”
web: uvicorn main:app --host=0.0.0.0 --port=${PORT:-5000}
-
Create a gitignore
**/__pycache__/ .env dep lambda_artifact.zip *.pyc *.pyo venv/
-
Install Heroku CLI
-
Heroku CLI Login
heroku login
-
Create a new Heroku App
Skip this step if you already created an app using the heroku web GUI
heroku create your-app-name
-
Add Heroku remote
git remote add heroku https://git.heroku.com/your-app-name.git
-
Push to heroku
git push heroku your-main-or-master-branch-name
-
Deal with .env files
Using Heroku Dashboard:
You can set, modify, or view environment variables from the Heroku dashboard:
- Navigate to Heroku Dashboard
- Select your application.
- Go to the "Settings" tab.
- Find the "Config Vars" section and use the "Reveal Config Vars" button.
- Here you can add key-value pairs equivalent to what you have in your
.env
file.