59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
pub mod request;
|
|
pub mod schema;
|
|
pub mod util;
|
|
|
|
use actix_cors::Cors;
|
|
use actix_web::HttpServer;
|
|
use actix_web::App;
|
|
use actix_web::http;
|
|
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 || {
|
|
let cors = Cors::default()
|
|
.allowed_origin("0.0.0.0")
|
|
.allowed_origin_fn(|_origin, _req_head| { true })
|
|
.allowed_methods(vec!["GET", "POST"])
|
|
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
|
|
.allowed_header(http::header::CONTENT_TYPE)
|
|
.max_age(3600);
|
|
|
|
App::new()
|
|
.wrap(cors)
|
|
.app_data(web::Data::new(pool.clone()))
|
|
.service(login)
|
|
.service(register)
|
|
.service(gamenights)
|
|
.service(gamenight_post)
|
|
.service(gamenight_get)
|
|
})
|
|
.bind(("::1", 8080))?
|
|
.run()
|
|
.await
|
|
} |