diff --git a/.hooks/pre-commit b/.hooks/pre-commit index 6947bc7..fe55771 100755 --- a/.hooks/pre-commit +++ b/.hooks/pre-commit @@ -2,7 +2,7 @@ # This hook lints the code, and if we're on develop or master, also forces the tests to pass. ./fejctl lint &> /dev/null 2>&1 || { - >&2 echo "Format check failed, use 'make lint' for more information."; + >&2 echo "Format check failed, use './fejctl lint' for more information."; exit 1; } 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..e2573af 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,17 @@ # 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 `src` folder contains subfolders for the various binaries and the main +library, called `fej`. The biggest binary is called `server`, which is the +binary that actually runs the Rocket.rs web server. All the others are utility +programs, mostly consisting of scrapers for various services. -Each module contains the following base files: - -* `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 +19,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/roadmap.md b/roadmap.md index f03e4b5..bfdd622 100644 --- a/roadmap.md +++ b/roadmap.md @@ -69,8 +69,8 @@ setting up a database for this version anyways. ## Kissanime I like watching anime from time to time, and I've always used Kissanime for -this. However, their sites can be quite slow, and riddled with ads from time to -time. That's why I'd like to create a high-speed wrapper that extracts all the -needed info from their sites, removing the need for the user to ever actually -visit their site. The API can just act as a fast search index, complete with -indexing of the links to the videos and everything. +this. However, their sites can be quite slow, and riddled with ads. That's why +I'd like to create a high-speed wrapper that extracts all the needed info from +their sites, removing the need for the user to ever actually visit their site. +The API can just act as a fast search index, complete with indexing of the +links to the videos and everything. diff --git a/src/errors.rs b/src/fej/errors.rs similarity index 89% rename from src/errors.rs rename to src/fej/errors.rs index afcf04a..730714c 100644 --- a/src/errors.rs +++ b/src/fej/errors.rs @@ -10,6 +10,8 @@ pub enum FejError { FailedRequest, } +// I'd love to move this over to the server binary, but right now, error E0117 is making that +// imopssible impl From for Status { fn from(err: FejError) -> Status { match err { diff --git a/src/ivago/controller/basic_date.rs b/src/fej/ivago/basic_date.rs similarity index 100% rename from src/ivago/controller/basic_date.rs rename to src/fej/ivago/basic_date.rs diff --git a/src/ivago/controller/mod.rs b/src/fej/ivago/mod.rs similarity index 100% rename from src/ivago/controller/mod.rs rename to src/fej/ivago/mod.rs diff --git a/src/ivago/controller/pickup_time.rs b/src/fej/ivago/pickup_time.rs similarity index 100% rename from src/ivago/controller/pickup_time.rs rename to src/fej/ivago/pickup_time.rs diff --git a/src/ivago/controller/street.rs b/src/fej/ivago/street.rs similarity index 100% rename from src/ivago/controller/street.rs rename to src/fej/ivago/street.rs diff --git a/src/lib.rs b/src/fej/lib.rs similarity index 67% rename from src/lib.rs rename to src/fej/lib.rs index b1e2733..761c91b 100644 --- a/src/lib.rs +++ b/src/fej/lib.rs @@ -1,11 +1,7 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] -extern crate rocket; - // Route modules pub mod ivago; // Helper modules -pub mod catchers; pub mod errors; diff --git a/src/catchers.rs b/src/server/catchers.rs similarity index 100% rename from src/catchers.rs rename to src/server/catchers.rs diff --git a/src/bin/server/main.rs b/src/server/main.rs similarity index 88% rename from src/bin/server/main.rs rename to src/server/main.rs index a27e00f..58b0300 100644 --- a/src/bin/server/main.rs +++ b/src/server/main.rs @@ -1,7 +1,13 @@ +#![feature(proc_macro_hygiene, decl_macro)] + #[macro_use] extern crate rocket; -use fej::{catchers, ivago}; +#[cfg(test)] +mod tests; + +mod catchers; +mod routes; // Very temporary solution for CORS // https://stackoverflow.com/questions/62412361/how-to-set-up-cors-or-options-for-rocket-rs @@ -33,7 +39,7 @@ impl Fairing for CORS { fn rocket() -> rocket::Rocket { rocket::ignite() .attach(CORS) - .mount("/ivago", ivago::routes()) + .mount("/ivago", routes::ivago()) .register(catchers![catchers::not_found]) } diff --git a/src/ivago/mod.rs b/src/server/routes/ivago.rs similarity index 83% rename from src/ivago/mod.rs rename to src/server/routes/ivago.rs index 627e89b..d8318bd 100644 --- a/src/ivago/mod.rs +++ b/src/server/routes/ivago.rs @@ -1,14 +1,7 @@ -mod controller; - -use controller::{get_pickup_times, search_streets}; -use controller::{BasicDate, PickupTime, Street}; +use fej::ivago::{get_pickup_times, search_streets, BasicDate, PickupTime, Street}; use rocket::http::Status; use rocket_contrib::json::Json; -pub fn routes() -> Vec { - routes![route_search_streets, route_get_pickup_times] -} - /// This route handles the Ivago search endpoint. It returns a list of streets, /// consisting of a street name & a city. /// diff --git a/src/server/routes/mod.rs b/src/server/routes/mod.rs new file mode 100644 index 0000000..449f82f --- /dev/null +++ b/src/server/routes/mod.rs @@ -0,0 +1,5 @@ +mod ivago; + +pub fn ivago() -> Vec { + routes![ivago::route_search_streets, ivago::route_get_pickup_times] +} 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