gamenight/backend-actix/src/main.rs

48 lines
1.4 KiB
Rust

pub mod request;
pub mod schema;
pub mod util;
use actix_web::HttpServer;
use actix_web::App;
use actix_web::web;
use diesel::PgConnection;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use request::{login, register, gamenights, gamenight_post, gamenight_get};
use util::GetConnection;
pub(crate) type DbPool = Pool<ConnectionManager<PgConnection>>;
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
fn run_migration(conn: &mut PgConnection) {
conn.run_pending_migrations(MIGRATIONS).unwrap();
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let url = "postgres://root:root@127.0.0.1/gamenight";
let manager = ConnectionManager::<PgConnection>::new(url);
// Refer to the `r2d2` documentation for more methods to use
// when building a connection pool
let pool = Pool::builder()
.test_on_check_out(true)
.build(manager)
.expect("Could not build connection pool");
let mut conn = pool.get_conn();
run_migration(&mut conn);
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone()))
.service(login)
.service(register)
.service(gamenights)
.service(gamenight_post)
.service(gamenight_get)
})
.bind(("::1", 8080))?
.run()
.await
}