use std::collections::BTreeMap;
use phoxal::api;
use phoxal::prelude::*;
struct Api;
struct AssetStoreState {
assets: BTreeMap<String, Vec<u8>>,
}
#[phoxal::service(id = "asset-store", state = AssetStoreState, api = Api)]
struct AssetStore;
impl Participant for AssetStore {
async fn setup(
&self,
ctx: &mut SetupContext<Self>,
_config: Self::Config,
) -> Result<(Self::State, Self::Api)> {
let mut assets = BTreeMap::new();
assets.insert("map.pgm".to_string(), vec![0x50, 0x35, 0x0a]);
ctx.query(api::topic::owner().asset().get(), Self::get)
.await?;
Ok((AssetStoreState { assets }, Api))
}
}
impl AssetStore {
async fn get(
&self,
_api: &Api,
request: api::asset::GetRequest,
state: &mut AssetStoreState,
) -> QueryResult<api::asset::GetResponse> {
match state.assets.get(&request.path) {
Some(bytes) => Ok(api::asset::GetResponse::Found {
bytes: bytes.clone(),
}),
None => Ok(api::asset::GetResponse::Missing),
}
}
}
fn main() -> phoxal::Result<()> {
phoxal::run::<AssetStore>()
}