This is the back-end for the webshop project. It is written in Rust, using the Actix framework. We used SQLx for database access and PostgreSQL for the database itself.
The REST api is documented using OpenAPI specification. You can find the Swagger UI here.
First of all, you will need to have Rust and Cargo installed. You can install them by following the instructions on rustup.rs.
In addition you will need a PostgreSQL database running. We recommend using Docker to run the database. You will also need to create the database schema. Refer to database README for more information.
Since we're using SQLx, you need a live database in order to compile, because the SQL queries are compile-time checked. If you however need to compile without a live database, see Build with no database.
You can put all environmental variables inside a .env
file at the root of the Rust project directory (this directory), or you can set them manually.
# Database URL
DATABASE_URL=postgresql://user:password@url/db_name
# Or if you have no live database
SQLX_OFFLINE=true
DATABASE_URL=postgresql://user:password@url/db_name
HOST=localhost # optionbal, default 'localhost'
PORT=8080 # optional, default '8080'
RUST_LOG=info,sqlx=warn # optional, default 'info,sqlx=warn' - set log level
ALLOWED_ORIGINS=https://group04.web-tek.ninja # list of allowed origins, separated with `,`
CERT_PATH=certificate.pem # path to certificate
PRIV_KEY_PATH=privatekey.pem # path to privatekey
[email protected] # gmail address used to send emails
EMAIL_PWD=googleapppassword # email password, use google's app password
See Google's Sign in with App Passwords for more information on how to generate app password to use with Gmail.
To build the project, run the following command:
cargo build --release
On build, the script build.rs
will include contents of ../webshop_frontend/dist
directory into the binary. inddex.html
will be served on any request that doesn't match backend routes. Make sure to build the frontend before building the backend. For more information, refer to webshop_frontend.
Note: If dist
directory is not present, a release build will fail. Dev build will work fine, but index.html
will be a dummy html file generated by the build script.
To run the project, you can run the generated binary:
./target/release/webshop_server
Or you can use cargo:
cargo run --release
You can also run a dev build:
cargo run
Regardless of the way you choose to run, you will need the following:
- PostgreSQL database running, with the database schema created. Refer to database README for more information.
- SSL certificate and private key - for development, you can use a self-signed certificate.
- Environmental variables set. Refer to environmental variables to run.
If you need to build the project without a live database, sqlx-data.json
file must be present in the root directory of the Rust project (this directory). This file must be regenerated if the database schema or queries change. You will also need to set SQLX_OFFLINE
environmental variable to true
. You can do this in a .env
file, or by setting the environmental variable in your shell.
sqlx-data.json
must be regenerated if the database schema or queries change.
You will need SQLx CLI, you can install it using cargo. Follow instructions on SQLx CLI.
In order to regenerate sqlx-data.json, run the following command:
cargo sqlx prepare
Note: You will need to have a database running in order to regenerate sqlx-data.json!
There are Postman tests in the postman
directory. You can import the collections into Postman and run them. There are more details on the collections' overview pages.