kochab/examples/serve_dir.rs

30 lines
1.1 KiB
Rust

use std::path::PathBuf;
use anyhow::*;
use log::LevelFilter;
use kochab::Server;
#[tokio::main]
/// Serving some static content from the filesystem is easy with Kochab
///
/// This example serves from the `./public` directory on the base route, and adds a
/// special one-page bind to `/about` that always serves `README.md`
///
/// Note, use this module with a little bit of caution. The directory serving feature is
/// currently unfinished, and the API is subject to change dramatically in future updates.
/// It should be secure, but you may need to do some refactoring in coming updates.
async fn main() -> Result<()> {
// We set up logging so we can see what's happening. This isn't technically required,
// and you can use a simpler solution (like env_logger::init()) during production
env_logger::builder()
.filter_module("kochab", LevelFilter::Debug)
.init();
Server::new()
.add_route("/", PathBuf::from("public")) // Serve directory listings & file contents
.add_route("/about", PathBuf::from("README.md")) // Serve a single file
.serve_ip("localhost:1965")
.await
}