|
1 | 1 | # Potion-rs
|
2 |
| -## IN DEVELOPMENT, DO NOT DEPLOY TO PRODUCTION |
| 2 | +> [!CAUTION] |
| 3 | +> Should not be used in production, under development |
3 | 4 |
|
4 |
| -## Documentation |
5 |
| -For now documentation only exists for `potion::Error`. |
| 5 | +Flask inspired general purpose fullstack web-framework with file-system based routing with support for server-side rendered template-based pages. For client side hydration and responsiveness potion includes build-in Typescript bundler *(as it is our philosophy that Typescript should be strictly used for simple DOM manipulation and)* |
6 | 6 |
|
7 |
| -## Features |
8 |
| -**DO NOT** enable `features = ["router"]`, as it does not currently compile |
| 7 | +### Features |
| 8 | +- [x] File-system based compile-time generated routing |
| 9 | + - [x] Robust state management between routers |
| 10 | + - [x] Support for accessing files both in router's own dir and in static folder. |
| 11 | +- [x] typescriot support for post-render DOM-manipulation |
| 12 | + - [x] .ts files both in /static and /routing directories are automatically compiled and linked *(sourcemaps included)* |
| 13 | +- [x] Optimised for fast rendering |
| 14 | + - [x] Minified generated HTML |
| 15 | + - [x] Minified .js bundles |
| 16 | + - [ ] Compressed HTML |
| 17 | + |
| 18 | + |
| 19 | +## Example |
| 20 | + |
| 21 | +`src/main.rs` |
| 22 | +```rust |
| 23 | +// Derive `potion::IntoContext` for global state |
| 24 | +#[derive(Clone, potion::IntoContext)] |
| 25 | +pub struct RouterContext { |
| 26 | + pub hb: Arc<Handlebars<'static>>, |
| 27 | + pub db: Pool<Postgres> |
| 28 | +} |
| 29 | + |
| 30 | +// Generate routing during compilation phase. All .rs files are automatically included during compilation |
| 31 | +potion::routing!(); |
| 32 | + |
| 33 | +#[tokio::main] |
| 34 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 35 | + dotenv::dotenv().ok(); |
| 36 | + |
| 37 | + // Initialize file-system routing |
| 38 | + let (mut hb, static_router) = potion::initialize_routing( |
| 39 | + &std::env::var("POTION_PROJECT_DIR").expect("Tried to read misconfigured .env file"), |
| 40 | + true, |
| 41 | + )?; |
| 42 | + |
| 43 | + // Create postgres connection-pool |
| 44 | + let pool = sqlx::postgres::PgPoolOptions::new() |
| 45 | + .max_connections(5) |
| 46 | + .connect(&std::env::var("POSTGRES_URL").expect("Tried to read misconfigured .env file")) |
| 47 | + .await?; |
| 48 | + |
| 49 | + // Initialize context |
| 50 | + let context = Box::new(RsContext { hb: Arc::new(hb), db: pool.clone() }); |
| 51 | + |
| 52 | + // Generate and server routing |
| 53 | + let routes = router(context) |
| 54 | + .or(static_router); |
| 55 | + |
| 56 | + warp::serve(routes).run(([0, 0, 0, 0], 3030)).await; |
| 57 | + |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +`src/routing/hello/index.rs` |
| 63 | +```rust |
| 64 | +pub fn initialize(router: potion::Router) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { |
| 65 | + // Access global state |
| 66 | + let context = router.downcast::<RouterContext>(); |
| 67 | + |
| 68 | + let routing = warp::path("hello") |
| 69 | + .and(warp::get()) |
| 70 | + .map(|| "Hello World"); |
| 71 | + |
| 72 | + routing |
| 73 | +} |
| 74 | +``` |
0 commit comments