Skip to content

Commit 1306516

Browse files
committed
add dockerized Piccolo Admin
1 parent d627aa4 commit 1306516

File tree

7 files changed

+138
-1
lines changed

7 files changed

+138
-1
lines changed

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.11
2+
3+
WORKDIR /code
4+
5+
COPY ./requirements.txt /code/requirements.txt
6+
7+
RUN pip install --no-cache-dir -r /code/requirements.txt
8+
9+
COPY ./app /code/app
10+
11+
CMD ["python", "app/main.py"]

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
# piccolo-admin-docker
1+
Experimenting with using a dockerized Piccolo Admin with an existing (legacy) database.
2+
3+
### Usage
4+
5+
Clone repository.
6+
7+
```bash
8+
git clone https://github.com/sinisaos/piccolo-admin-docker.git
9+
```
10+
11+
Creating an `.env` file.
12+
13+
```bash
14+
cd app
15+
cp .env.example .env && rm .env.example
16+
cd ..
17+
```
18+
Creating a Docker image.
19+
20+
```bash
21+
docker build -t piccolo_admin .
22+
```
23+
24+
Running a Docker image (using the `--network=host` flag because I'm using an existing database from the local machine).
25+
26+
```bash
27+
docker run -d --network=host --name admin_container piccolo_admin
28+
```
29+
30+
After site is running log in as admin user on [localhost:8000](http://localhost:8000/admin/) and use legacy database.

app/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DB_NAME="your_legacy_db"
2+
DB_USER="your_db_user"
3+
DB_PASSWORD="your_db_password"
4+
DB_HOST="your_host"
5+
DB_PORT=5432

app/__init__.py

Whitespace-only changes.

app/admin_user.db

20 KB
Binary file not shown.

app/main.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import asyncio
2+
import os
3+
4+
from dotenv import find_dotenv, load_dotenv
5+
from hypercorn import Config
6+
from hypercorn.asyncio import serve
7+
from piccolo.apps.user.tables import BaseUser
8+
from piccolo.engine import PostgresEngine
9+
from piccolo.engine.sqlite import SQLiteEngine
10+
from piccolo.table import create_db_tables
11+
from piccolo.table_reflection import TableStorage
12+
from piccolo_admin import create_admin
13+
from piccolo_api.session_auth.tables import SessionsBase
14+
15+
DB = SQLiteEngine("app/admin_user.db")
16+
17+
18+
load_dotenv(find_dotenv())
19+
20+
21+
USERNAME = "piccolo"
22+
PASSWORD = "piccolo123"
23+
24+
25+
class Sessions(SessionsBase, db=DB):
26+
pass
27+
28+
29+
class User(BaseUser, tablename="piccolo_user", db=DB):
30+
pass
31+
32+
33+
async def main():
34+
# Create auth tables in separate Sqlite DB
35+
await create_db_tables(*[User, Sessions], if_not_exists=True)
36+
# Create a admin user in separate Sqlite DB
37+
if not await User.exists().where(User.email == "[email protected]"):
38+
user = User(
39+
username=USERNAME,
40+
password=PASSWORD,
41+
42+
admin=True,
43+
active=True,
44+
superuser=True,
45+
)
46+
await user.save()
47+
48+
db = PostgresEngine(
49+
config={
50+
"database": os.environ["DB_NAME"],
51+
"user": os.environ["DB_USER"],
52+
"password": os.environ["DB_PASSWORD"],
53+
"host": os.environ["DB_HOST"],
54+
"port": int(os.environ["DB_PORT"]),
55+
},
56+
extensions=tuple(),
57+
)
58+
59+
storage = TableStorage(engine=db)
60+
await storage.reflect(schema_name="public")
61+
62+
# This tuple IS unique
63+
# however auto_include_related within
64+
# create_admin makes it non unique TableConfigs
65+
found_tables = storage.tables.values()
66+
67+
for table_class in found_tables:
68+
table_class._meta._db = db
69+
70+
app = create_admin(
71+
found_tables,
72+
auth_table=User,
73+
session_table=Sessions,
74+
auto_include_related=False,
75+
)
76+
77+
# Server
78+
class CustomConfig(Config):
79+
use_reloader = True
80+
accesslog = "-"
81+
82+
await serve(app, CustomConfig())
83+
84+
85+
if __name__ == "__main__":
86+
asyncio.run(main())

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
https://github.com/piccolo-orm/piccolo/archive/master.zip
2+
piccolo-admin
3+
asyncpg
4+
aiosqlite
5+
hypercorn
6+
python-dotenv

0 commit comments

Comments
 (0)