blob: b2af6de70230484db6414e95a41cf4df9177957d (
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
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
|
use std::env;
use std::net::SocketAddr;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
use tokio_rusqlite::Connection;
mod db;
mod jobs;
mod model;
mod routes;
mod templates;
mod util;
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let host = get_env("HOST");
let port = get_env("PORT");
let db_path = get_env("DB");
let authorized_key = get_env("KEY");
let files_dir = get_env("FILES_DIR");
let db_conn = Connection::open(db_path)
.await
.expect("Error while openning DB conection");
tokio::spawn(jobs::start(db_conn.clone(), files_dir.clone()));
let addr: SocketAddr = format!("{host}:{port}")
.parse()
.unwrap_or_else(|_| panic!("Invalid address: {host}:{port}"));
let listener = TcpListener::bind(addr).await?;
log::info!("Listening on http://{}", addr);
loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);
let db_conn = db_conn.clone();
let authorized_key = authorized_key.clone();
let files_dir = files_dir.clone();
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.serve_connection(
io,
service_fn(move |req| {
routes::routes(
req,
db_conn.clone(),
authorized_key.clone(),
files_dir.clone(),
)
}),
)
.await
{
log::error!("Failed to serve connection: {:?}", err);
}
});
}
}
fn get_env(key: &str) -> String {
env::var(key).unwrap_or_else(|_| panic!("Missing environment variable {key}"))
}
|