-
Notifications
You must be signed in to change notification settings - Fork 12
/
actix-tarantool-proxy.rs
71 lines (61 loc) · 2.11 KB
/
actix-tarantool-proxy.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
66
67
68
69
70
71
//extern crate rusty_tarantool;
extern crate serde_derive;
extern crate serde_json;
use serde_derive::{Deserialize, Serialize};
use actix_web::{get, web, App, HttpResponse, HttpServer};
use futures::{select, FutureExt};
use futures::stream::{StreamExt};
use rusty_tarantool::tarantool::{Client, ClientConfig, ExecWithParamaters};
use std::io;
use actix_web::web::Data;
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct CountryInfo {
#[serde(rename = "country-code", default)]
country_code: u16,
name: String,
region: Option<String>,
#[serde(rename = "sub-region", default)]
sub_region: Option<String>,
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct CountryResponse {
countries: Vec<CountryInfo>,
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct CountryRequest {
country_name: Option<String>,
region: Option<String>,
sub_region: Option<String>,
}
#[get("/countries/query")]
async fn index(
params: web::Query<CountryRequest>,
tarantool_client: web::Data<Client>,
) -> io::Result<HttpResponse> {
let response = tarantool_client.prepare_fn_call("test_search")
.bind_ref(¶ms.country_name)?
.bind_ref(¶ms.region)?
.bind_ref(¶ms.sub_region)?
.execute().await?;
Ok(HttpResponse::Ok().json(CountryResponse {
countries: response.decode_single()?,
}))
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
let tarantool_client = ClientConfig::new("127.0.0.1:3301", "rust", "rust")
.set_timeout_time_ms(2000)
.set_reconnect_time_ms(2000)
.build();
let mut notify_future = Box::pin(tarantool_client
.subscribe_to_notify_stream()
.for_each_concurrent(0, |s| async move { println!("current status {:?}", s) }));
let mut server_future = HttpServer::new(move || App::new().app_data(Data::new(tarantool_client.clone())).service(index))
.bind("127.0.0.1:8080")?
.run().fuse();
select! {
_r = notify_future => println!("Status notify stream is finished!"),
_r = server_future => println!("Server is stopped!"),
};
Ok(())
}