Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // In this example we store an atomic value on the tide webserver
- // it is controlled by requests from the clients
- //
- use tide::Request;
- use tide::prelude::*;
- use std::sync::{Arc};
- use std::sync::atomic::{AtomicI32, Ordering};
- // the counter can be +ve or -ve so its an integer 32
- #[derive(Clone)]
- struct State {
- value: Arc<AtomicI32>,
- }
- impl State {
- fn new() -> Self {
- Self {
- value: Arc::new(AtomicI32::new(0)),
- }
- }
- }
- // we define value to add/set sent from the client can only be positive as a u16
- #[derive(Debug, Deserialize)]
- struct QueryObj {
- value: Option<u16>,
- set_by: String,
- }
- impl QueryObj {
- /// Create a new QueryObj.
- fn new(value: Option<u16>, set_by: &str) -> QueryObj {
- QueryObj { value: value, set_by: set_by.to_string() }
- }
- fn get_id(&self) -> &str {
- &self.set_by
- }
- fn get_val(&self) -> i32 {
- self.value.unwrap_or(0) as i32
- }
- }
- #[async_std::main]
- async fn main() -> tide::Result<()> {
- let mut app = tide::with_state(State::new());
- app.with(tide::log::LogMiddleware::new());
- // curl -i 'localhost:8080/add
- // returns the saved atomic value to the client
- //
- app.at("/").get(|req: tide::Request<State>| async move {
- let state = req.state();
- let value = state.value.load(Ordering::Relaxed);
- Ok(format!("{}\n", value))
- });
- // curl -i 'localhost:8080/add
- // increments the atomic value
- //
- app.at("/inc").get(|req: tide::Request<State>| async move {
- let state = req.state();
- let value = state.value.fetch_add(1, Ordering::Relaxed) + 1;
- Ok(format!("{}\n", value))
- });
- // curl -i 'localhost:8080/sub
- // subtracts from the atomic value
- //
- app.at("/sub").get(|req: tide::Request<State>| async move {
- let state = req.state();
- let value = state.value.fetch_sub(1, Ordering::Relaxed) - 1;
- Ok(format!("{}\n", value))
- });
- // curl -i 'localhost:8080/add?value=100&set_by=charlie'
- // adds to the atomic memory value to what is specified in the curl
- //
- app.at("/add").get(|req: Request<State>| {
- async move {
- let client_data = req.query::<QueryObj>().unwrap();
- let state = req.state();
- let value = state.value.fetch_add(client_data.get_val(),Ordering::Relaxed) + 1;
- Ok(tide::Response::builder(200)
- .body(format!("<html><h2>New Value set to {} by user {}!</h2></html>",
- client_data.get_val(), client_data.get_id()))
- .header("Server", "tide")
- .content_type(tide::http::mime::HTML)
- .build())
- }
- });
- // curl -i 'localhost:8080/reset
- // sets the atomic memory value to zero
- //
- app.at("/reset").get(|req: tide::Request<State>| async move {
- let state = req.state();
- let value = state.value.load(Ordering::Relaxed);
- let value = state.value.fetch_sub(value, Ordering::Relaxed);
- Ok(format!("{}\n", value))
- });
- // curl -i 'localhost:8080/set?value=900&set_by=rocksy'
- // sets the atomic memory value to what is specified in the curl
- //
- app.at("/set").get(|req: Request<State>| {
- async move {
- let client_data = req.query::<QueryObj>().unwrap();
- let state = req.state();
- let value = state.value.load(Ordering::Relaxed);
- let value = state.value.fetch_sub(value, Ordering::Relaxed);
- let value = state.value.fetch_add(client_data.get_val(),Ordering::Relaxed) + 1;
- Ok(tide::Response::builder(200)
- .body(format!("<html><h2>New Value set to {} by user {}!</h2></html>",
- client_data.get_val(), client_data.get_id()))
- .header("Server", "tide")
- .content_type(tide::http::mime::HTML)
- .build())
- }
- });
- app.listen("127.0.0.1:8080").await?;
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement