■ tide 크레이트를 사용해 값을 입력받는 웹 서버를 만드는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 |
[package] name = "test_server" version = "0.1.0" edition = "2021" [dependencies] tide = "0.16" async-std = {version = "1.8.0", features = ["attributes"]} serde = {version = "1.0", features = ["derive"]} |
▶ src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
use tide::prelude::*; const SERVER_ADDRESS : &str = "127.0.0.1:8888"; #[derive(Deserialize, Serialize)] struct UserInformation { name : String } #[async_std::main] async fn main() -> tide::Result<()> { println!("http://{}/", SERVER_ADDRESS); let mut server : tide::Server<()> = tide::new(); server.at("/") .get ( |_| async { Ok ( tide::Response::builder(200) .content_type(tide::http::mime::HTML) .body ( r#"<html> <body> <form action='hello'> name : <input name='name' value='홍길동'> <input type='submit' value='전송'> </form> </body> </html>"# ) .build() ) } ); server.at("/hello") .get ( |request : tide::Request<()>| async move { let user_information : UserInformation = request.query()?; return Ok ( tide::Response::builder(200) .content_type(tide::http::mime::HTML) .body(format!("<h1>안녕하세요, {}님</h1>", user_information.name)) .build() ); } ); server.listen(SERVER_ADDRESS).await?; return Ok(()); } |