Created
October 12, 2019 17:02
-
-
Save hvariant/d9f7c0ae5cca6503fe38858647faf033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use async_std::sync::{Arc, Mutex}; | |
| use serde::{Deserialize, Serialize}; | |
| use tide::{response, Context, EndpointResult}; | |
| #[derive(Serialize, Deserialize, Clone)] | |
| struct Message { | |
| author: Option<String>, | |
| contents: String, | |
| } | |
| async fn return_json(ctx: Context<Arc<Mutex<Message>>>) -> EndpointResult { | |
| let msg = &*ctx.state().lock().await; | |
| Ok(response::json(msg)) | |
| } | |
| fn main() { | |
| let msg = Message { | |
| author: Some("body".to_string()), | |
| contents: "once told me".to_string(), | |
| }; | |
| let mut app = tide::App::with_state(Arc::new(Mutex::new(msg))); | |
| app.at("/json").get(return_json); | |
| app.run("127.0.0.1:8000").unwrap(); | |
| } |
Author
hvariant
commented
Oct 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment