app/src/main.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2022-02-03 21:55:10 +00:00
use std::sync::Arc;
use actix_web::{App, HttpServer};
use sea_orm::{Database, DatabaseConnection};
use tracing::{instrument, info};
use tracing_subscriber::prelude::*;
mod api;
mod entity;
#[derive(Debug)]
pub struct AppState {
pub db: DatabaseConnection,
}
#[actix_web::main]
async fn main() {
let subscriber = tracing_subscriber::registry().with(
tracing_subscriber::fmt::Layer::new()
.pretty()
.with_writer(std::io::stdout)
.with_ansi(true)
.with_filter(tracing_subscriber::filter::LevelFilter::DEBUG),
);
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global collector");
let db = setup_database().await.unwrap();
let state = Arc::new(AppState { db });
info!("Starting http server on 8080");
HttpServer::new(move || {
App::new()
.app_data(state.clone())
.service(api::applications::list_applications)
})
.bind("127.0.0.1:8080")
.unwrap()
.run()
.await
.expect("Couldnt launch server");
}
#[instrument]
async fn setup_database() -> Result<DatabaseConnection, sea_orm::DbErr> {
Database::connect("sqlite://data.db").await
}