main.rs 1.34 KB
Newer Older
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
1 2 3
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
4 5 6
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate diesel;

7 8 9 10 11 12 13 14 15
use trust_dns_client::client::AsyncClient;
use trust_dns_client::tcp::TcpClientStream;
use trust_dns_proto::xfer::dns_multiplexer::DnsMultiplexer;
use trust_dns_proto::iocompat::AsyncIoTokioAsStd;
use trust_dns_client::rr::dnssec::Signer;
use tokio::net::TcpStream as TokioTcpStream;
use tokio::task;

use std::sync::{Arc, Mutex};
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
16

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
17
mod models;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
18
mod config;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
19
mod schema;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
20
mod routes;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
21

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
22 23
use routes::users::*;
use routes::zones::*;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
24

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
25

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
26 27
#[database("db")]
pub struct DbConn(diesel::SqliteConnection);
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
28

29
type DnsClient = Arc<Mutex<AsyncClient>>;
30

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
31

32
#[launch]
33
async fn rocket() -> rocket::Rocket {
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
34
    let app_config = config::load("config.toml".into());
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
35
    println!("{:#?}", app_config);
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
36

37 38 39 40 41
    let (stream, handle) = TcpClientStream::<AsyncIoTokioAsStd<TokioTcpStream>>::new(app_config.dns.server);
    let multiplexer = DnsMultiplexer::<_, Signer>::new(stream, handle, None);
    let client = AsyncClient::connect(multiplexer);
    let (client, bg) = client.await.expect("connection failed");
    task::spawn(bg);
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
42 43

    rocket::ignite()
44
        .manage(Arc::new(Mutex::new(client)))
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
45
        .manage(app_config)
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
46
        .attach(DbConn::fairing())
47
        .mount("/api/v1", routes![get_zone_records, create_auth_token, create_user])
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
48
}