-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonsrc_make.sh
executable file
·100 lines (81 loc) · 2.3 KB
/
pythonsrc_make.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# Initialize error count
error_count=0
if [ $# -eq 1 ]; then
BASE_DIR="$1"
PROJECT_NAME="default_project"
DESCRIPTION="A FastAPI project"
elif [ $# -ge 2 ]; then
BASE_DIR="$1"
PROJECT_NAME="$2"
DESCRIPTION="${3:-A FastAPI project}"
else
echo "Invalid arguments."
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 <base_directory>"
exit 1
fi
# Set the base project directory
BASE_DIR="$1"
# Function to create directories with error checking
create_directory() {
local dir_path="$1"
if mkdir -p "$dir_path"; then
echo "Created directory: $dir_path"
else
echo "Failed to create directory: $dir_path" >&2
((error_count++))
fi
}
# Function to create files with error checking
create_file() {
local file_path="$1"
if touch "$file_path"; then
echo "Created file: $file_path"
else
echo "Failed to create file: $file_path" >&2
((error_count++))
fi
}
# Ensure base directory exists
echo "Checking if base directory exists..."
create_directory "$BASE_DIR"
# Python files for the app
echo "Creating Python files for the app..."
create_file "$BASE_DIR/app/__init__.py"
# MongoDB Client with pfmongo
create_directory "$BASE_DIR/app/db"
cat > "$BASE_DIR/app/db/mongo_client.py" << 'EOF'
import os
# from pfmongo import MongoClient
mongo_uri = os.getenv("MONGO_URI", "mongodb://admin:admin@localhost:27017")
# client = MongoClient(mongo_uri)
# db = client.get_database("$PROJECT_NAME")
EOF
# Main entry point
cat > "$BASE_DIR/app/main.py" << 'EOF'
from fastapi import FastAPI
from app.api.v1.routes import users
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Welcome to $PROJECT_NAME!"}
app.include_router(users.router, prefix="/users", tags=["users"])
EOF
# API Route Example
create_directory "$BASE_DIR/app/api/v1/routes"
cat > "$BASE_DIR/app/api/v1/routes/users.py" << 'EOF'
from fastapi import APIRouter
# from app.db.mongo_client import db
router = APIRouter()
@router.get("/")
async def get_users():
# users = db.users.find()
users = {"name": "Some One", "email": "[email protected]"}
return [{"name": user["name"], "email": user["email"]} for user in users]
EOF
echo "Python files created with pfmongo integration in $BASE_DIR."
echo "Total errors encountered: $error_count"
# Check if a base directory is provided as an argument