-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
97 lines (84 loc) · 2.16 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use actix_web::{post, web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};
use serde_json;
use std::env;
use url::Url;
#[derive(Deserialize)]
struct ActionParams {
name: String,
}
#[derive(Deserialize)]
struct ActionRequestBody {
params: ActionParams
}
#[derive(Serialize, Deserialize)]
struct ActionResponse {
response: String,
}
#[post("/actions/hello")]
async fn hello() -> impl Responder {
println!("Hello action called.");
HttpResponse::Ok()
.json(ActionResponse {
response: "Hello world from Rust!".to_string()
})
}
#[post("/actions/welcome")]
async fn welcome(body: web::Json<ActionRequestBody>) -> impl Responder {
println!("Welcome action called.");
let msg = ["Hello ", &body.params.name, " from Rust!"].concat();
HttpResponse::Ok()
.json(ActionResponse {
response: msg
})
}
#[post("/events/sample.event")]
async fn sample_event() -> impl Responder {
println!("Sample event happened.");
HttpResponse::Ok()
}
#[actix_web::main]
async fn main() -> Result<(), std::io::Error> {
let mut sidecar_address = "http://localhost:5103".to_string();
if env::var("SIDECAR_ADDRESS").is_ok() {
sidecar_address = env::var("SIDECAR_ADDRESS").unwrap();
}
println!("Registering service schema {}...\n", sidecar_address);
let url = Url::parse(format!("{}/v1/registry/services", sidecar_address).as_str()).expect("Invalid Sidecar URL");
let echo_json: serde_json::Value = reqwest::Client::new()
.post(url)
.json(&serde_json::json!({
"name": "rust-demo",
"settings": {
"baseUrl": "http://rust-demo:5004"
},
"actions": {
"hello": "/actions/hello",
"welcome": {
"params": {
"name": "string|no-empty|trim"
},
"handler": "/actions/welcome"
}
},
"events": {
"sample.event": "/events/sample.event"
}
}))
.send()
.await.expect("Some error happened")
.json()
.await.expect("Some error happened");
println!("Response: {:#?}", echo_json);
println!("Starting server on '0.0.0.0:5004'...");
HttpServer::new(|| {
App::new()
.service(hello)
.service(welcome)
.service(sample_event)
})
.bind("0.0.0.0:5004")?
.workers(4)
.run()
.await
}