app/src/main.rs

49 lines
1.3 KiB
Rust
Raw Normal View History

use actix_web::{web, App, HttpServer};
2022-02-03 21:55:10 +00:00
use sea_orm::{Database, DatabaseConnection};
use tracing::{info, instrument};
2022-02-03 21:55:10 +00:00
use tracing_subscriber::prelude::*;
mod api;
mod entity;
mod error;
2022-02-03 21:55:10 +00:00
#[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 = web::Data::new(AppState { db });
2022-02-03 21:55:10 +00:00
info!("Starting http server on 8080");
HttpServer::new(move || {
App::new()
.app_data(state.clone())
.service(api::applications::routes())
.service(api::application_categories::routes())
.service(api::bookmarks::routes())
.service(api::bookmark_categories::routes())
2022-02-03 21:55:10 +00:00
})
.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
}