summaryrefslogtreecommitdiff
path: root/rest/src/main.rs
blob: ae993d92e35413cbdd2ceba2ab42cc91755b9453 (plain)
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
use std::{convert::Infallible, sync::Arc};

use crate::{config::Config, ratelimit::Ratelimiter};
use common::{
    config::Settings,
    log::{error, info},
    redis_crate::Client,
};
use hyper::{server::conn::AddrStream, service::make_service_fn, Server};
use std::net::ToSocketAddrs;
use tokio::sync::Mutex;

use crate::proxy::ServiceProxy;

mod config;
mod proxy;
mod ratelimit;

#[tokio::main]
async fn main() {
    let settings: Settings<Config> = Settings::new("rest").unwrap();
    let config = Arc::new(settings.config);
    let redis_client: Client = settings.redis.into();
    let redis = Arc::new(Mutex::new(
        redis_client.get_async_connection().await.unwrap(),
    ));
    let ratelimiter = Arc::new(Ratelimiter::new(redis));

    let addr = format!("{}:{}", config.server.address, config.server.port)
        .to_socket_addrs()
        .unwrap()
        .next()
        .unwrap();

    let service_fn = make_service_fn(move |_: &AddrStream| {
        let service_proxy = ServiceProxy::new(config.clone(), ratelimiter.clone());
        async move { Ok::<_, Infallible>(service_proxy) }
    });

    let server = Server::bind(&addr).serve(service_fn);

    info!("starting ratelimit server");
    if let Err(e) = server.await {
        error!("server error: {}", e);
    }
}