aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 5cd3492ee713aea88a4c0f6f52766c09bedf55a0 (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
// extern
use anyhow::Result;
use hyper::service::{make_service_fn, service_fn};
use hyper::Server;
use log::trace;

// internal
mod db;
use db::{init_db, open_connection};

mod service;
use service::shortner_service;

mod cli;
use cli::CONFIG;

fn main() -> Result<()> {
    pretty_env_logger::init();

    init_db(&CONFIG.db_path)?;
    smol::run(async {
        let addr = ([127, 0, 0, 1], CONFIG.port).into();
        let service = make_service_fn(|_| async {
            Ok::<_, hyper::Error>(service_fn(move |req| {
                let db_conn = open_connection(&CONFIG.db_path).unwrap();
                shortner_service(req, db_conn)
            }))
        });
        let server = Server::bind(&addr).serve(service);
        trace!("Listening on http://{}", addr);
        server.await.unwrap();
        Ok(())
    })
}