From 45c4a4e257efad0edba3d2f94149f33da591c1f8 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 16 Apr 2021 09:18:50 +0200 Subject: [PATCH] [#26] Moved lib & bin to own folders; Moved server tests; wrote some readmes --- Cargo.toml | 11 ++--- README.md | 60 ++++++++++++++++----------- src/{ => fej}/errors.rs | 0 src/{ => fej}/ivago/basic_date.rs | 0 src/{ => fej}/ivago/mod.rs | 0 src/{ => fej}/ivago/pickup_time.rs | 0 src/{ => fej}/ivago/street.rs | 0 src/{ => fej}/lib.rs | 0 src/{bin => }/server/catchers.rs | 0 src/{bin => }/server/main.rs | 4 ++ src/{bin => }/server/routes/ivago.rs | 0 src/{bin => }/server/routes/mod.rs | 0 tests/ivago.rs => src/server/tests.rs | 2 +- 13 files changed, 46 insertions(+), 31 deletions(-) rename src/{ => fej}/errors.rs (100%) rename src/{ => fej}/ivago/basic_date.rs (100%) rename src/{ => fej}/ivago/mod.rs (100%) rename src/{ => fej}/ivago/pickup_time.rs (100%) rename src/{ => fej}/ivago/street.rs (100%) rename src/{ => fej}/lib.rs (100%) rename src/{bin => }/server/catchers.rs (100%) rename src/{bin => }/server/main.rs (97%) rename src/{bin => }/server/routes/ivago.rs (100%) rename src/{bin => }/server/routes/mod.rs (100%) rename tests/ivago.rs => src/server/tests.rs (93%) diff --git a/Cargo.toml b/Cargo.toml index 46b01a1..4081b85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [lib] name = "fej" -src = "src/lib.rs" +path = "src/fej/lib.rs" test = true bench = true doc = true @@ -14,10 +14,11 @@ doctest = true [[bin]] name = "server" -src = "src/bin/server/main.rs" -test = false -bench = false -doc = false +path = "src/server/main.rs" +test = true +bench = true +doc = true +doctest = true [dependencies] rocket = "0.4.7" diff --git a/README.md b/README.md index 55d11e3..15e4e03 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,21 @@ # Fej -Fej is an API written in Rust. I started this project to learn the language, -and really just have some fun. +Fej is a RESTful API that does lots of different things. It started as an +experiment to learn Rust, but has grown into a full-on passion project. ## Project Structure -The folder structure follows the structure of the URLs, e.g. the route for -`/hello/world` is found in the module `src/hello`. +The bulk of the project consists of the main `fej` library. The `src` folder +contains the `lib.rs` file for this library, and all other binaries import from +this main library. -Each module contains the following base files: +All binaries can be found in [`/src/bin`](src/bin), with the biggest one being +`server`. This is what actually runs as the Rocket.rs server. The other +binaries (with more possibly coming) are utility tools that will most likely be +run as cron jobs inside the containers, e.g. scrapers. -* `mod.rs`: defines the modules' content, and contains the route definitions. - The route functions themselves only contain the functionality needed to - represent the data, not acquire it. -* `controller.rs`: this file contains the actual logic of each route. If the - logic becomes too complicated to be contained inside a single file, - `controller.rs` becomes its own module folder named `controller`. -* `tests.rs`: this contains tests for the specific module. This can also be a - module directory if need be. - -Every module has a `routes` function that returns its route macros. +Version 1.1 also introduces the use of a database, namely +[PostgreSQL 13](https://www.postgresql.org/). ## Roadmap @@ -27,19 +23,33 @@ See [roadmap.md](roadmap.md). ## Development -The entire toolchain runs on Alpine inside Docker. This makes building easier, -and (hopefully) eliminates any system-specific bugs. +To make development more consistent (and keep my computer a bit cleaner) I've +decided to run the entire toolchain using Docker, with Alpine Linux base +images. This also allows me to make really small final images. Technically, +Docker is the only dependency you need to contribute to this project +(not accounting for language servers etc.). -A [Makefile wrapper](Makefile) is provided for ease of use. Do check it out, as -all the commands are documented for your understanding ;) +A [Bash script](fejctl) is provided to speed up development on the various +binaries. It aids in starting up the containers, choosing which binary to run +etc. A quick rundown of its most important features: -There's also the `build` script. This script does all the "heavy" lifting. It -chooses which Dockerfile to build according to the given arguments, and -generates tags for the images (useful when pushing releases). The Makefile is -really just a wrapper around this build script, allowing you to write -`make test` instead of `./build -m dev -a run test`. +```bash +# By default, it compiles the 'server' binary (but doesn't run it) +./fejctl -tl;dr run `make run` to run your build, and `make test` to run the tests. +# The first argument is the action you wish to perform, the second on which +# binary (not all commands need a binary as input, so they just ignore it) +# For example, this will run the binary called server +./fejctl r server + +# This runs the tests (all commands also have their full name if you want) +./fejctl t +./fejctl test + +# These attach to the two containers +./fejctl sh # Opens a new root shell inside the 'fej' container +./fejctl psql # Open a new psql shell in the fej database +``` ## Docker images diff --git a/src/errors.rs b/src/fej/errors.rs similarity index 100% rename from src/errors.rs rename to src/fej/errors.rs diff --git a/src/ivago/basic_date.rs b/src/fej/ivago/basic_date.rs similarity index 100% rename from src/ivago/basic_date.rs rename to src/fej/ivago/basic_date.rs diff --git a/src/ivago/mod.rs b/src/fej/ivago/mod.rs similarity index 100% rename from src/ivago/mod.rs rename to src/fej/ivago/mod.rs diff --git a/src/ivago/pickup_time.rs b/src/fej/ivago/pickup_time.rs similarity index 100% rename from src/ivago/pickup_time.rs rename to src/fej/ivago/pickup_time.rs diff --git a/src/ivago/street.rs b/src/fej/ivago/street.rs similarity index 100% rename from src/ivago/street.rs rename to src/fej/ivago/street.rs diff --git a/src/lib.rs b/src/fej/lib.rs similarity index 100% rename from src/lib.rs rename to src/fej/lib.rs diff --git a/src/bin/server/catchers.rs b/src/server/catchers.rs similarity index 100% rename from src/bin/server/catchers.rs rename to src/server/catchers.rs diff --git a/src/bin/server/main.rs b/src/server/main.rs similarity index 97% rename from src/bin/server/main.rs rename to src/server/main.rs index 3fde2c7..58b0300 100644 --- a/src/bin/server/main.rs +++ b/src/server/main.rs @@ -2,6 +2,10 @@ #[macro_use] extern crate rocket; + +#[cfg(test)] +mod tests; + mod catchers; mod routes; diff --git a/src/bin/server/routes/ivago.rs b/src/server/routes/ivago.rs similarity index 100% rename from src/bin/server/routes/ivago.rs rename to src/server/routes/ivago.rs diff --git a/src/bin/server/routes/mod.rs b/src/server/routes/mod.rs similarity index 100% rename from src/bin/server/routes/mod.rs rename to src/server/routes/mod.rs diff --git a/tests/ivago.rs b/src/server/tests.rs similarity index 93% rename from tests/ivago.rs rename to src/server/tests.rs index d78501a..8871762 100644 --- a/tests/ivago.rs +++ b/src/server/tests.rs @@ -3,7 +3,7 @@ use rocket::http::Status; use rocket::local::Client; fn rocket() -> rocket::Rocket { - rocket::ignite().mount("/", fej_lib::ivago::routes()) + rocket::ignite().mount("/", super::routes::ivago()) } /// Test 404 response