You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
Rust
61 lines
1.7 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use std::{net::IpAddr, path::Path, time::Duration};
|
|
use config::Config;
|
|
use governor::{clock::DefaultClock, state::keyed::DashMapStateStore, Quota, RateLimiter};
|
|
use mail_send::{SmtpClientBuilder, Credentials};
|
|
use rocket::data::{Limits, ToByteUnit};
|
|
|
|
mod config;
|
|
mod endpoints;
|
|
|
|
pub struct State {
|
|
pub rate_limit: RateLimiter<IpAddr,DashMapStateStore<IpAddr>,DefaultClock>,
|
|
pub smtp_builder: SmtpClientBuilder<String>,
|
|
pub config: Config,
|
|
}
|
|
|
|
#[launch]
|
|
fn rocket() -> _ {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
if args.len() - 1 != 1 {
|
|
panic!("Expected 1 argument, got {}", args.len() - 1)
|
|
}
|
|
|
|
let path = Path::new(&args[1]);
|
|
let config = crate::config::Config::parse(path).unwrap();
|
|
|
|
let smtp_builder = SmtpClientBuilder::new(config.mailer.host.clone(), config.mailer.port)
|
|
.timeout(Duration::from_secs(5))
|
|
.implicit_tls(config.mailer.implicit_tls)
|
|
.credentials(Credentials::Plain {
|
|
username: config.mailer.username.clone(),
|
|
secret: config.mailer.password.clone()
|
|
});
|
|
|
|
let quota = Quota::with_period(
|
|
Duration::from_secs(config.rate_limit.replenish_seconds.into())
|
|
).unwrap().allow_burst(config.rate_limit.burst_max);
|
|
|
|
let rate_limit : RateLimiter<IpAddr, _, _> = RateLimiter::dashmap(quota);
|
|
|
|
let state = State {
|
|
config,
|
|
rate_limit,
|
|
smtp_builder
|
|
};
|
|
|
|
let rocket_conf = rocket::config::Config {
|
|
limits: Limits::default()
|
|
.limit("json", 2.kibibytes()),
|
|
..rocket::config::Config::default()
|
|
};
|
|
|
|
rocket::custom(rocket_conf)
|
|
.manage(state)
|
|
.mount("/", routes![
|
|
endpoints::handler
|
|
])
|
|
}
|