mod bindings;
use crate::bindings::exports::pack::name::api::*;
use crate::bindings::golem::api::host::*;
use reqwest::{Client, Response};
use serde::{Deserialize, Serialize};
struct State {
total: u64,
}
static mut STATE: State = State {
total: 0
};
fn with_state<T>(f: impl FnOnce(&mut State) -> T) -> T {
unsafe { f(&mut STATE) }
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct RequestBody {
current_total: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct ResponseBody {
message: String,
}
struct Component;
impl Guest for Component {
fn add(value: u64) {
with_state(|state| state.total += value);
}
fn get() -> u64 {
with_state(|state| state.total)
}
fn publish() -> Result<(), String> {
with_state(|state| {
println!("Publishing the total count {} via HTTP", state.total);
let client = Client::builder().build()?;
let request_body = RequestBody { current_total: state.total };
let response: Response = client.post("http://localhost:9999/current-total")
.json(&request_body)
.send()?;
let response_body = response.json::<ResponseBody>()?;
println!("Result: {:?}", response_body);
Ok(())
}).map_err(|e: reqwest::Error| format!("Failed to publish: {}", e))
}
fn pause() {
let promise_id = golem_create_promise();
golem_await_promise(&promise_id);
}
}