74 lines
2.7 KiB
Rust
74 lines
2.7 KiB
Rust
use rouille::{router, Request, Response};
|
|
|
|
use crate::{
|
|
private_router, routes,
|
|
utils::{flash::FlashResponse, sbot},
|
|
SessionData,
|
|
};
|
|
|
|
/// Receive an incoming request, mount the fileservers for static assets and
|
|
/// define the publically-accessible routes.
|
|
///
|
|
/// If the request is for a private route (ie. a route requiring successful
|
|
/// authentication to view), check the authentication status of the user
|
|
/// by querying the `session_data`. If the user is authenticated, pass their
|
|
/// request to the private router. Otherwise, redirect them to the login page.
|
|
pub fn handle_route(request: &Request, session_data: &mut Option<SessionData>) -> Response {
|
|
// static file server
|
|
// matches on assets in the `static` directory
|
|
let static_response = rouille::match_assets(request, "static");
|
|
if static_response.is_success() {
|
|
return static_response;
|
|
}
|
|
|
|
// set the `.ssb-go` path in order to mount the blob fileserver
|
|
let ssb_path = sbot::get_go_ssb_path().expect("define ssb-go dir path");
|
|
let blobstore = format!("{}/blobs/sha256", ssb_path);
|
|
|
|
// blobstore file server
|
|
// removes the /blob url prefix and serves blobs from blobstore
|
|
// matches on assets in the `static` directory
|
|
if let Some(request) = request.remove_prefix("/blob") {
|
|
return rouille::match_assets(&request, &blobstore);
|
|
}
|
|
|
|
// handle the routes which are always accessible (ie. whether logged-in
|
|
// or not)
|
|
router!(request,
|
|
(GET) (/auth/forgot) => {
|
|
Response::html(routes::authentication::forgot::build_template())
|
|
},
|
|
|
|
(GET) (/auth/login) => {
|
|
Response::html(routes::authentication::login::build_template(request))
|
|
.reset_flash()
|
|
},
|
|
|
|
(POST) (/auth/login) => {
|
|
routes::authentication::login::handle_form(request, session_data)
|
|
},
|
|
|
|
(GET) (/auth/reset) => {
|
|
Response::html(routes::authentication::reset::build_template(request))
|
|
.reset_flash()
|
|
},
|
|
|
|
(POST) (/auth/reset) => {
|
|
routes::authentication::reset::handle_form(request)
|
|
},
|
|
|
|
_ => {
|
|
// now that we handled all the routes that are accessible in all
|
|
// circumstances, we check that the user is logged in before proceeding
|
|
if let Some(_session) = session_data.as_ref() {
|
|
// logged in:
|
|
// mount the routes which require authentication to view
|
|
private_router::mount_peachpub_routes(request, session_data)
|
|
} else {
|
|
// not logged in:
|
|
Response::redirect_303("/auth/login")
|
|
}
|
|
}
|
|
)
|
|
}
|