use std::sync::{Arc, RwLock}; use foxhole::{ action::Html, resolve::{Endpoint, Get, Query}, sys, App, Http1, Scope, TypeCache, TypeCacheKey, }; pub struct Counter(u32); impl TypeCacheKey for Counter { // This must be an `Arc` otherwise the object will be cloned by the query and changes will // not persist type Value = Arc>; } // The value stored inside `Query` is `Counter::Value` fn get(_get: Get, counter: Query, _e: Endpoint) -> Html { counter.0.write().unwrap().0 += 1; let page = format!( "

This page has been visited {} times!

", counter.0.read().unwrap().0 ); Html(page) } fn main() { let scope = Scope::new(sys![get]); let mut cache = TypeCache::new(); cache.insert::(Arc::new(RwLock::new(Counter(0)))); println!("Try connecting with a browser at 'http://localhost:8080'"); App::builder(scope) .cache(cache) .run::("0.0.0.0:8080"); }