Skip to content

Instantly share code, notes, and snippets.

@coryjamescrook
Created June 24, 2023 11:42
Show Gist options
  • Save coryjamescrook/f473c7ff97476769dd6a33f8710b0c4c to your computer and use it in GitHub Desktop.
Save coryjamescrook/f473c7ff97476769dd6a33f8710b0c4c to your computer and use it in GitHub Desktop.
Fastapi starter script
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Step 1: Set up the project structure
const projectDir = path.join(__dirname, 'fastapi-project');
fs.mkdirSync(projectDir);
process.chdir(projectDir);
const directories = [
'app',
'app/api',
'app/auth',
'app/database',
'app/models',
'app/utils',
'docker',
'tests',
];
directories.forEach((dir) => {
fs.mkdirSync(dir);
});
// Step 2: Set up Docker
const dockerDir = path.join(projectDir, 'docker');
fs.writeFileSync(
path.join(dockerDir, 'Dockerfile'),
`FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
COPY ./app /app
`
);
// Step 3: Install dependencies
execSync('python3 -m venv venv', { stdio: 'inherit' });
execSync('. venv/bin/activate && pip install fastapi[all] sqlalchemy passlib[bcrypt]', {
stdio: 'inherit',
});
// Step 4: Create the FastAPI application
const mainFilePath = path.join(projectDir, 'app/main.py');
fs.writeFileSync(
mainFilePath,
`from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello, FastAPI!"}
`
);
// Step 5: Configure PostgreSQL connection
const databaseFilePath = path.join(projectDir, 'app/database/database.py');
fs.writeFileSync(
databaseFilePath,
`from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "postgresql://username:password@localhost/fastapi_db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
`
);
// Step 6: Set up authentication and RBAC authorization
const authFilePath = path.join(projectDir, 'app/auth/auth.py');
// Add the authentication and authorization logic to `auth.py` based on your chosen framework
// Step 7: Configure FastAPI to use PostgreSQL and authentication
fs.appendFileSync(
mainFilePath,
`
from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
from app.auth.auth import authenticate_user, create_access_token, get_current_user
from app.database.database import engine
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/login")
@app.get("/")
async def read_root(current_user: User = Depends(get_current_user)):
return {"message": f"Hello, {current_user.username}!"}
@app.post("/login")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
user = authenticate_user(form_data.username, form_data.password)
if not user:
# Add authentication failure logic
pass
access_token = create_access_token(user)
return {"access_token": access_token, "token_type": "bearer"}
`
);
console.log('FastAPI project initialization completed!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment