Rocket - Two applications in separate files


Limitation of this solution:


examples/rocket/separate-files/src/main.rs
#[macro_use]
extern crate rocket;

pub(crate) mod blog;

#[get("/")]
fn index() -> &'static str {
    "Main page"
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![index])
        .mount("/blog", blog::routes())
}

#[cfg(test)]
mod tests;

examples/rocket/separate-files/src/tests.rs
use rocket::http::Status;
use rocket::local::blocking::Client;

#[test]
fn check_main() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/plain; charset=utf-8"
    );
    assert_eq!(response.into_string(), Some("Main page".into()));
}

examples/rocket/separate-files/src/blog.rs
use rocket::Route;

pub fn routes() -> Vec<Route> {
    routes![index]
}

#[get("/")]
pub fn index() -> &'static str {
    "Blog"
}

#[cfg(test)]
mod blog_tests;

examples/rocket/separate-files/src/blog/blog_tests.rs
use rocket::http::Status;
use rocket::local::blocking::Client;

#[test]
fn check_blog() {
    let client = Client::tracked(super::super::rocket()).unwrap();
    let response = client.get("/blog").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/plain; charset=utf-8"
    );
    assert_eq!(response.into_string(), Some("Blog".into()));
}