2021-03-06 23:28:15 +08:00
Thanks for taking the time to contribute! 🎉👍
2022-07-04 22:01:04 +08:00
Before working on a new feature, please get in touch with us at dev[at]simplelogin.io to avoid duplication.
We can also discuss the best way to implement it.
2022-04-20 00:38:15 +08:00
2022-07-04 22:01:04 +08:00
The project uses Flask, Python3.7+ and requires Postgres 12+ as dependency.
2021-03-08 22:08:41 +08:00
## General Architecture
< p align = "center" >
< img src = "./docs/archi.png" height = "450px" >
< / p >
SimpleLogin backend consists of 2 main components:
2021-08-15 23:41:16 +08:00
- the `webapp` used by several clients: the web app, the browser extensions (Chrome & Firefox for now), OAuth clients (apps that integrate "Sign in with SimpleLogin" button) and mobile apps.
2021-03-08 22:08:41 +08:00
- the `email handler` : implements the email forwarding (i.e. alias receiving email) and email sending (i.e. alias sending email).
2021-08-15 23:41:16 +08:00
## Install dependencies
2021-03-06 22:55:18 +08:00
2021-08-15 23:41:16 +08:00
The project requires:
2021-03-06 22:55:18 +08:00
- Python 3.7+ and [poetry ](https://python-poetry.org/ ) to manage dependencies
- Node v10 for front-end.
2021-08-15 23:41:16 +08:00
- Postgres 12+
2021-03-06 22:55:18 +08:00
First, install all dependencies by running the following command.
Feel free to use `virtualenv` or similar tools to isolate development environment.
```bash
poetry install
```
2021-08-15 23:41:16 +08:00
On Mac, sometimes you might need to install some other packages via `brew` :
2021-03-06 22:55:18 +08:00
```bash
2023-05-03 05:01:55 +08:00
brew install pkg-config libffi openssl postgresql@13
2021-03-06 22:55:18 +08:00
```
2021-08-15 23:41:16 +08:00
You also need to install `gpg` tool, on Mac it can be done with:
2021-03-06 22:55:18 +08:00
```bash
brew install gnupg
```
2022-07-04 22:01:04 +08:00
If you see the `pyre2` package in the error message, you might need to install its dependencies with `brew` .
2021-10-04 22:48:44 +08:00
More info on https://github.com/andreasvc/pyre2
```bash
brew install -s re2 pybind11
```
2022-07-04 22:01:04 +08:00
## Linting and static analysis
We use pre-commit to run all our linting and static analysis checks. Please run
```bash
poetry run pre-commit install
```
To install it in your development environment.
2021-08-15 23:41:16 +08:00
## Run tests
2022-11-23 20:51:08 +08:00
For most tests, you will need to have ``redis`` installed and started on your machine (listening on port 6379).
2021-03-06 22:55:18 +08:00
```bash
2021-08-20 18:00:45 +08:00
sh scripts/run-test.sh
2021-03-06 22:55:18 +08:00
```
2021-08-15 23:41:16 +08:00
## Run the code locally
2021-03-06 22:55:18 +08:00
Install npm packages
```bash
cd static & & npm install
```
To run the code locally, please create a local setting file based on `example.env` :
```
cp example.env .env
```
2022-11-23 20:51:08 +08:00
You need to edit your .env to reflect the postgres exposed port, edit the `DB_URI` to:
```
DB_URI=postgresql://myuser:mypassword@localhost:35432/simplelogin
```
2021-08-15 23:41:16 +08:00
Run the postgres database:
2021-03-06 22:55:18 +08:00
2021-08-15 23:41:16 +08:00
```bash
2022-12-03 00:31:31 +08:00
docker run -e POSTGRES_PASSWORD=mypassword -e POSTGRES_USER=myuser -e POSTGRES_DB=simplelogin -p 15432:5432 postgres:13
2021-03-06 22:55:18 +08:00
```
2021-08-15 23:41:16 +08:00
To run the server:
2021-03-06 22:55:18 +08:00
```
2021-10-14 21:42:19 +08:00
alembic upgrade head & & flask dummy-data & & python3 server.py
2021-03-06 22:55:18 +08:00
```
2021-08-15 23:41:16 +08:00
then open http://localhost:7777, you should be able to login with `john@wick.com / password` account.
2021-03-06 23:28:15 +08:00
You might need to change the `.env` file for developing certain features. This file is ignored by git.
2021-03-08 21:53:53 +08:00
## Database migration
2021-03-06 22:55:18 +08:00
The database migration is handled by `alembic`
Whenever the model changes, a new migration has to be created.
If you have Docker installed, you can create the migration by the following script:
```bash
2021-08-20 18:00:45 +08:00
sh scripts/new-migration.sh
2021-03-06 22:55:18 +08:00
```
Make sure to review the migration script before committing it.
Sometimes (very rarely though), the automatically generated script can be incorrect.
We cannot use the local database to generate migration script as the local database doesn't use migration.
It is created via `db.create_all()` (cf `fake_data()` method). This is convenient for development and
unit tests as we don't have to wait for the migration.
2022-05-13 20:42:20 +08:00
## Reset database
There are two scripts to reset your local db to an empty state:
- `scripts/reset_local_db.sh` will reset your development db to the latest migration version and add the development data needed to run the
server.py locally.
- `scripts/reset_test_db.sh` will reset your test db to the latest migration without adding the dev server data to prevent interferring with
the tests.
2021-03-08 21:53:53 +08:00
## Code structure
2021-03-06 22:55:18 +08:00
The repo consists of the three following entry points:
- wsgi.py and server.py: the webapp.
- email_handler.py: the email handler.
- cron.py: the cronjob.
Here are the small sum-ups of the directory structures and their roles:
- app/: main Flask app. It is structured into different packages representing different features like oauth, api, dashboard, etc.
- local_data/: contains files to facilitate the local development. They are replaced during the deployment.
- migrations/: generated by flask-migrate. Edit these files will be only edited when you spot (very rare) errors on the database migration files.
- static/: files available at `/static` url.
- templates/: contains both html and email templates.
- tests/: tests. We don't really distinguish unit, functional or integration test. A test is simply here to make sure a feature works correctly.
2021-03-08 21:53:53 +08:00
## Pull request
2021-03-06 22:55:18 +08:00
The code is formatted using https://github.com/psf/black, to format the code, simply run
```
2021-03-08 21:53:53 +08:00
poetry run black .
```
The code is also checked with `flake8` , make sure to run `flake8` before creating the pull request by
```bash
poetry run flake8
2021-03-06 22:55:18 +08:00
```
2022-06-29 17:28:26 +08:00
For HTML templates, we use `djlint` . Before creating a pull request, please run
```bash
poetry run djlint --check templates
```
2023-07-10 20:41:52 +08:00
If some files aren't properly formatted, you can format all files with
```bash
poetry run djlint --reformat .
```
2022-02-15 01:06:41 +08:00
2021-03-08 21:53:53 +08:00
## Test sending email
2021-03-06 22:55:18 +08:00
[swaks ](http://www.jetmore.org/john/code/swaks/ ) is used for sending test emails to the `email_handler` .
2021-03-06 23:28:15 +08:00
[mailcatcher ](https://github.com/sj26/mailcatcher ) or [MailHog ](https://github.com/mailhog/MailHog ) can be used as a MTA to receive emails.
2021-03-06 22:55:18 +08:00
2021-03-06 23:28:15 +08:00
Here's how set up the email handler:
2021-03-06 22:55:18 +08:00
2021-03-06 23:28:15 +08:00
1) run mailcatcher or MailHog
2021-03-06 22:55:18 +08:00
```bash
mailcatcher
```
2) Make sure to set the following variables in the `.env` file
```
2021-03-06 23:28:15 +08:00
# comment out this variable
# NOT_SEND_EMAIL=true
# So the emails will be sent to mailcatcher/MailHog
2021-03-06 22:55:18 +08:00
POSTFIX_SERVER=localhost
POSTFIX_PORT=1025
```
3) Run email_handler
```bash
python email_handler.py
```
4) Send a test email
```bash
2021-03-06 23:28:15 +08:00
swaks --to e1@sl.local --from hey@google.com --server 127.0.0.1:20381
2021-03-06 22:55:18 +08:00
```
2022-11-23 20:51:08 +08:00
Now open http://localhost:1080/ (or http://localhost:1080/ for MailHog), you should see the forwarded email.
## Job runner
Some features require a job handler (such as GDPR data export). To test such feature you need to run the job_runner
```bash
python job_runner.py
```