|
| 1 | +mod error_pages; |
| 2 | +mod templates; |
| 3 | + |
| 4 | +use perseus::{Html, PerseusApp}; |
| 5 | + |
| 6 | +// pub fn get_app<G: Html>() -> PerseusApp<G> { |
| 7 | +// PerseusApp::new() |
| 8 | +// .template(crate::templates::index::get_template) |
| 9 | +// .template(crate::templates::about::get_template) |
| 10 | +// .error_pages(crate::error_pages::get_error_pages) |
| 11 | +// } |
| 12 | + |
| 13 | +// #[perseus::engine_main] |
| 14 | +// async fn main() { |
| 15 | +// use perseus::builder::{get_op, run_dflt_engine}; |
| 16 | + |
| 17 | +// let op = get_op().unwrap(); |
| 18 | +// let exit_code = run_dflt_engine(op, get_app, perseus_warp::dflt_server).await; |
| 19 | +// std::process::exit(exit_code); |
| 20 | +// } |
| 21 | + |
| 22 | +// #[perseus::browser_main] |
| 23 | +// pub fn main() -> perseus::ClientReturn { |
| 24 | +// use perseus::run_client; |
| 25 | + |
| 26 | +// run_client(get_app) |
| 27 | +// } |
| 28 | + |
| 29 | +// Note: we use fully-qualified paths in the types to this function so we don't have to target-gate some more imports |
| 30 | +#[cfg(not(target_arch = "wasm32"))] // We only have access to `warp` etc. on the engine-side, so this function should only exist there |
| 31 | +pub async fn dflt_server< |
| 32 | + M: perseus::stores::MutableStore + 'static, |
| 33 | + T: perseus::internal::i18n::TranslationsManager + 'static, |
| 34 | +>( |
| 35 | + props: perseus::internal::serve::ServerProps<M, T>, |
| 36 | + (host, port): (String, u16), |
| 37 | +) { |
| 38 | + use perseus_warp::perseus_routes; |
| 39 | + use std::net::SocketAddr; |
| 40 | + use warp::Filter; |
| 41 | + |
| 42 | + // The Warp integration takes a `SocketAddr`, so we convert the given host and port into that format |
| 43 | + let addr: SocketAddr = format!("{}:{}", host, port) |
| 44 | + .parse() |
| 45 | + .expect("Invalid address provided to bind to."); |
| 46 | + // Now, we generate the routes from the properties we were given |
| 47 | + // All integrations provide some function for setting them up that just takes those universal properties |
| 48 | + // Usually, you shouldn't ever have to worry about the value of the properties, which are set from your `PerseusApp` config |
| 49 | + let perseus_routes = perseus_routes(props).await; |
| 50 | + // And now set up our own routes |
| 51 | + // You could set up as many of these as you like in a production app |
| 52 | + // Note that they mustn't define anything under `/.perseus` or anything conflicting with any of your static aliases |
| 53 | + // This one will just echo whatever is sent to it |
| 54 | + let api_route = warp::path!("api" / "echo" / String).map(|msg| { |
| 55 | + // You can do absolutely anything in here that you can do with Warp as usual |
| 56 | + msg |
| 57 | + }); |
| 58 | + // We could add as many routes as we wanted here, but the Perseus routes, no matter what integration you're using, MUST |
| 59 | + // always come last! This is because they define a wildcard handler for pages, which has to be defined last, or none of your routes |
| 60 | + // will do anything. |
| 61 | + let routes = api_route.or(perseus_routes); |
| 62 | + |
| 63 | + warp::serve(routes).run(addr).await; |
| 64 | + |
| 65 | + // If you try interacting with the app as usual, everything will work fine |
| 66 | + // If you try going to `/api/echo/test`, you'll get `test` printed back to you! Try replacing `test` with anything else |
| 67 | + // and it'll print whatever you put in back to you! |
| 68 | +} |
| 69 | + |
| 70 | +#[perseus::main(dflt_server)] |
| 71 | +pub fn main<G: Html>() -> PerseusApp<G> { |
| 72 | + PerseusApp::new() |
| 73 | + .template(crate::templates::index::get_template) |
| 74 | + .template(crate::templates::about::get_template) |
| 75 | + .error_pages(crate::error_pages::get_error_pages) |
| 76 | +} |
0 commit comments