-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
87 lines (73 loc) · 2.64 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
#[macro_use]
extern crate lazy_static;
use std::env;
use anyhow::Result;
use hyper::{
service::{make_service_fn, service_fn},
Server,
};
use prometheus::{opts, register_counter_vec, CounterVec};
use reqwest::Url;
use resources::{
models::NodeConfig,
objects::{function::Function, service::Service},
};
use crate::{route::router, utils::create_informer};
lazy_static! {
static ref CONFIG: NodeConfig = {
dotenv::from_path("/etc/rminik8s/node.env").ok();
NodeConfig {
etcd_endpoint: match env::var("ETCD_ENDPOINT") {
Ok(url) => Url::parse(url.as_str()).unwrap(),
Err(_) => Url::parse("http://127.0.0.1:2379/").unwrap(),
},
api_server_endpoint: match env::var("API_SERVER_ENDPOINT") {
Ok(url) => Url::parse(url.as_str()).unwrap(),
Err(_) => Url::parse("http://127.0.0.1:8080/").unwrap(),
},
}
};
static ref REQUESTS_COUNTER: CounterVec = register_counter_vec!(
opts!("function_requests_total", "Total number of requests"),
&["function"] // labels
)
.unwrap();
}
mod route;
mod utils;
mod workflow;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
tracing_subscriber::fmt::init();
tracing::info!("Serverless router started");
// let (tx, mut rx) = mpsc::channel::<Notification>(16);
let func_informer = create_informer::<Function>("functions");
let func_store = func_informer.get_store();
let func_informer_handler = tokio::spawn(async move { func_informer.run().await });
let svc_informer = create_informer::<Service>("services");
let svc_store = svc_informer.get_store();
let svc_informer_handler = tokio::spawn(async move { svc_informer.run().await });
let addr = ([0, 0, 0, 0], 80).into();
let service = make_service_fn(move |_| {
let func_store = func_store.clone();
let svc_store = svc_store.clone();
async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
let func_store = func_store.clone();
let svc_store = svc_store.clone();
async move { Ok::<_, hyper::Error>(router(req, func_store, svc_store).await) }
}))
}
});
let server = Server::bind(&addr).serve(service);
let graceful = server.with_graceful_shutdown(async {
tokio::signal::ctrl_c()
.await
.expect("failed to install CTRL+C signal handler");
});
tracing::info!("Listening on http://{}", addr);
graceful.await?;
func_informer_handler.abort();
svc_informer_handler.abort();
Ok(())
}