forked from sparckles/Robyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability in robyn cli to scaffold example programs with vari…
…ous DBs (sparckles#584) * use pyinquire and added example files for various databases * move Docker file to scaffold/dockerfile and update dockerfile generation in main * move scaffold app.py files and docker files to named directories in scaffold --------- Co-authored-by: Sanskar Jethi <sansyrox@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f4a9de0
commit 9f0c23d
Showing
24 changed files
with
347 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.11-bookworm | ||
|
||
WORKDIR /workspace | ||
|
||
RUN pip install --no-cache-dir --upgrade -r requirements.txt | ||
|
||
COPY . . | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ["python3", "app.py", "--log-level=DEBUG"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pymongo import MongoClient | ||
from robyn import Robyn | ||
|
||
app = Robyn(__file__) | ||
db = MongoClient("URL HERE") | ||
|
||
users = db.users # define a collection | ||
|
||
|
||
@app.get("/") | ||
def index(): | ||
return "Hello World!" | ||
|
||
|
||
# create a route | ||
@app.get("/users") | ||
async def get_users(): | ||
all_users = await users.find().to_list(length=None) | ||
return {"users": all_users} | ||
|
||
|
||
# create a route to add a new user | ||
@app.post("/users") | ||
async def add_user(request): | ||
user_data = await request.json() | ||
result = await users.insert_one(user_data) | ||
return {"success": True, "inserted_id": str(result.inserted_id)} | ||
|
||
|
||
# create a route to fetch a single user by ID | ||
@app.get("/users/{user_id}") | ||
async def get_user(request): | ||
user_id = request.path_params["user_id"] | ||
user = await users.find_one({"_id": user_id}) | ||
if user: | ||
return user | ||
else: | ||
return {"error": "User not found"}, 404 | ||
|
||
|
||
if __name__ == "__main__": | ||
app.start(host="0.0.0.0", port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
robyn | ||
pymongo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.11-bookworm | ||
|
||
WORKDIR /workspace | ||
|
||
RUN pip install --no-cache-dir --upgrade -r requirements.txt | ||
|
||
COPY . . | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ["python3", "app.py", "--log-level=DEBUG"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from robyn import Robyn | ||
|
||
app = Robyn(__file__) | ||
|
||
|
||
@app.get("/") | ||
def index(): | ||
return "Hello World!" | ||
|
||
|
||
if __name__ == "__main__": | ||
app.start(host="0.0.0.0", port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
robyn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# ---- Build the PostgreSQL Base ---- | ||
FROM postgres:latest AS postgres-base | ||
|
||
ENV POSTGRES_USER=postgres | ||
ENV POSTGRES_PASSWORD=password | ||
ENV POSTGRES_DB=postgresDB | ||
|
||
# ---- Build the Python App ---- | ||
FROM python:3.11-bookworm | ||
|
||
# Install supervisor | ||
RUN apt-get update && apt-get install -y supervisor | ||
|
||
WORKDIR /workspace | ||
|
||
COPY requirements.txt requirements.txt | ||
RUN pip install --no-cache-dir --upgrade -r requirements.txt | ||
|
||
COPY . . | ||
|
||
# Copy PostgreSQL binaries from the first stage | ||
COPY --from=postgres-base /usr/local/bin /usr/local/bin | ||
COPY --from=postgres-base /usr/lib/postgresql /usr/lib/postgresql | ||
COPY --from=postgres-base /usr/share/postgresql /usr/share/postgresql | ||
COPY --from=postgres-base /var/lib/postgresql /var/lib/postgresql | ||
|
||
# Add supervisord config | ||
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf | ||
|
||
EXPOSE 8080 5455 | ||
|
||
CMD ["/usr/bin/supervisord"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import psycopg2 | ||
from robyn import Robyn | ||
|
||
DB_NAME = "postgresDB" | ||
DB_HOST = "localhost" | ||
DB_USER = "postgres" | ||
DB_PASS = "password" | ||
DB_PORT = "5455" | ||
|
||
conn = psycopg2.connect(database=DB_NAME, host=DB_HOST, user=DB_USER, password=DB_PASS, port=DB_PORT) | ||
|
||
app = Robyn(__file__) | ||
|
||
|
||
# create a route to fetch all users | ||
@app.get("/users") | ||
def get_users(): | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT * FROM users") | ||
all_users = cursor.fetchall() | ||
return {"users": all_users} | ||
|
||
|
||
@app.get("/") | ||
def index(): | ||
return "Hello World!" | ||
|
||
|
||
if __name__ == "__main__": | ||
app.start(url="0.0.0.0", port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
robyn | ||
psycopg2; platform_system=="Windows" | ||
psycopg2-binary; platform_system!="Windows" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[supervisord] | ||
nodaemon=true | ||
|
||
[program:python-app] | ||
command=python3 /workspace/app.py --log-level=DEBUG | ||
autostart=true | ||
autorestart=true | ||
redirect_stderr=true | ||
|
||
[program:postgres] | ||
command=postgres -c 'config_file=/etc/postgresql/postgresql.conf' | ||
autostart=true | ||
autorestart=true | ||
redirect_stderr=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM python:3.11-bookworm | ||
|
||
WORKDIR /workspace | ||
|
||
RUN pip install --no-cache-dir --upgrade -r requirements.txt | ||
|
||
COPY . . | ||
|
||
RUN python3 -m prisma generate | ||
RUN python3 -m prisma migrate dev --name init | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ["python3", "app.py", "--log-level=DEBUG"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from robyn import Robyn | ||
from prisma import Prisma | ||
from prisma.models import User | ||
|
||
app = Robyn(__file__) | ||
prisma = Prisma(auto_register=True) | ||
|
||
|
||
@app.startup_handler | ||
async def startup_handler() -> None: | ||
await prisma.connect() | ||
|
||
|
||
@app.shutdown_handler | ||
async def shutdown_handler() -> None: | ||
if prisma.is_connected(): | ||
await prisma.disconnect() | ||
|
||
|
||
@app.get("/") | ||
async def h(): | ||
user = await User.prisma().create( | ||
data={ | ||
"name": "Robert", | ||
}, | ||
) | ||
return user.json(indent=2) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.start(host="0.0.0.0", port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
robyn | ||
prisma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
datasource db { | ||
provider = "sqlite" | ||
url = "file:dev.db" | ||
} | ||
|
||
generator py { | ||
provider = "prisma-client-py" | ||
} | ||
|
||
model User { | ||
id String @id @default(cuid()) | ||
name String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.11-bookworm | ||
|
||
WORKDIR /workspace | ||
|
||
RUN pip install --no-cache-dir --upgrade -r requirements.txt | ||
|
||
COPY . . | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ["python3", "app.py", "--log-level=DEBUG"] |
Empty file.
Oops, something went wrong.