Note
Would like to add your own city? Feel free to create a Pull Request :)
The backend consists of three main parts:
- A script initializing the database with some static data from
facilities.json - A cronjob getting the raw HTML from the site of the Studierendenwerk, stripping the unnecessary parts away and storing it as files (see
app/src/cron/fetcher). - A second cronjob parsing the downloaded files and populating the Postgres database (see
app/src/cron/db_updater). - The actual API reading the data from the database and returning the JSON (see
app/src/routes). You can find API-Documententation on the/docsroute.
Tbh, the project needs some refactoring to make it easier to extend. By now, its only in version 1.0 :'D
- Add the static data to the
facilities.json. Give each facility a uuidv4. - Run the database initialization script like this:
python -m app.src.cron.init_db - Adjust
app/src/cron/fetcherto strip away the unnecessary parts. You can skip this step if you do not perform webscraping and access an API directly. - Extend
app/src/cron/db_updaterto write the data to the database (either by parsing the HTML or by calling an API provided by a Studierendenwerk)
Now your cities canteens should be available via the API :)
This API uses FastAPI and SQLModel. Start the development server with:
fastapi dev app.pyThe APIs documentation can be found on the /docs or /redoc route.
You can run the cronjobs from the project-root like this:
python -m app.src.cron.init_db
python -m app.src.cron.fetcher.fetcher
python -m app.src.cron.db_updater.db_updaterThis project uses Alembic for database migrations.
-
You can verify the database connection in your
alembic.iniandenv.pyfiles by running:alembic current
-
After making a change to the DB schema, commit it:
alembic revision --autogenerate -m "Add phone_number to Customer"This creates a new migration script in the
migrations/versions/directory. Each migration script includesupgrade()anddowngrade()functions:upgrade(): Defines how to apply the migration (e.g., add a column).downgrade(): Defines how to undo the migration (e.g., remove the column).
-
Run the migration to apply the changes to the database:
alembic upgrade head
-
To view all applied and pending migrations:
alembic history
Note
The migration scripts generated will probably not run due to a missing import. Just add import sqlmodel to the file.
Tip
To undo the last migration:
alembic downgrade -1To revert to a specific migration (using its revision ID):
alembic downgrade <revision_id>