|
| 1 | +# Backend |
| 2 | + |
| 3 | +The backend server is written in Rust using the [Rocket](https://rocket.rs/) web framework and [Diesel](https://diesel.rs/) object relational mapping (ORM) and query builder. |
| 4 | + |
| 5 | +The database uses [PostgreSQL](https://www.postgresql.org/). Although fancy features aren't used, it has only been tested with PostgreSQL 14.4. |
| 6 | + |
| 7 | +Be a good person and run `cargo clippy --fix` every so often. |
| 8 | + |
| 9 | +## Documentation |
| 10 | + |
| 11 | +If you are new to Rust, take a look at the [Rust Book](https://doc.rust-lang.org/book/). |
| 12 | + |
| 13 | +[See Rocket documentation.](https://rocket.rs/v0.5-rc/guide//) |
| 14 | + |
| 15 | +[See Diesel documentation here.](https://diesel.rs/guides/) |
| 16 | + |
| 17 | +[See PostgreSQL 14 documentation here.](https://www.postgresql.org/docs/14/index.html) |
| 18 | + |
| 19 | +## Architecture |
| 20 | + |
| 21 | +`src/main.rs` launches the backend server, including connecting to the database. If you add another endpoint, you will need to mount it here. |
| 22 | + |
| 23 | +`src/api.rs` implements all API endpoints for the backend server, including defining the API parameters, request bodies, and response bodies. If you add another endpoint, you will need to implement it here. |
| 24 | + |
| 25 | +`src/responses.rs` defines helper methods to return API responses. It is unlikely you will need to modify this file. |
| 26 | + |
| 27 | +`src/handlers.rs` defines catchers to handle 4xx and 5xx errors. It is unlikely you will need to modify this file. |
| 28 | + |
| 29 | +`src/models.rs` defines the database schema in the database and implements methods to select, insert, update, and delete from the database. This is where you would modify the database schema and add functions to query the database. |
| 30 | + |
| 31 | +`src/schema.rs` is generated by Diesel based on `src/models.rs`. Do not modify this file. |
| 32 | + |
| 33 | +`src/database.rs` implements the pool manager to the database. It is unlikely you will need to modify this file. |
| 34 | + |
| 35 | +## Environment variables |
| 36 | + |
| 37 | +A `.env` file must exist in this directory. It must contain: |
| 38 | + |
| 39 | +- `DATABASE_URL` = A [connection URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) used by Diesel to find the database. |
| 40 | +- `PASSWORD_SALT` = [A random string used as an input when generating password bashes.](<https://en.wikipedia.org/wiki/Salt_(cryptography)>) Please use a long and complex string. |
| 41 | +- `DOMAIN` = The domain name used to host the frontend. When developing locally, this will be localhost with some port. When deploying, please use [HTTPS](https://en.wikipedia.org/wiki/HTTPS). |
| 42 | +- `SMTP_USERNAME` = An email address using [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) under your control to send verification and password reset emails to user. |
| 43 | +- `SMTP_PASSWORD` = The password to the above email address. |
| 44 | +- `PROJECT_END` = A [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339.txt) timestamp of when project submission ends. |
| 45 | +- `VOTE_END` = A [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339.txt) timestamp of when voting ends. |
| 46 | + |
| 47 | +An example `.env` would look like: |
| 48 | + |
| 49 | +``` |
| 50 | +DATABASE_URL=postgres://postgres:postgres@localhost/subcomp |
| 51 | +PASSWORD_SALT=VERY_LONG_AND_COMPLEX_STRING |
| 52 | +DOMAIN=http://localhost:3000 |
| 53 | + |
| 54 | +SMTP_PASSWORD=password |
| 55 | +PROJECT_END=1999-09-09T23:59:59+10:00 |
| 56 | +VOTE_END=1999-09-19T23:59:59+10:00 |
| 57 | +``` |
0 commit comments