li7y/src/main.rs

48 lines
1.4 KiB
Rust
Raw Normal View History

2024-06-24 22:46:04 +02:00
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::env;
use actix_web::{web, App, HttpServer};
use diesel::prelude::*;
use diesel::r2d2;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use log::info;
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
let manager = r2d2::ConnectionManager::<PgConnection>::new(
env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
);
let pool = r2d2::Pool::builder()
.build(manager)
.expect("database URL must be valid");
pool.get()
.expect("couldnt get db connection from pool")
.run_pending_migrations(MIGRATIONS)
.expect("failed to run migrations");
let address = env::var("LISTEN_ADDRESS").unwrap_or("::1".to_string());
let port = env::var("LISTEN_PORT").map_or(8080, |s| {
s.parse::<u16>().expect("failed to parse LISTEN_PORT")
});
info!("Starting on {address}:{port}");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone()))
.service(web::scope("/api").configure(li7y::api::config))
.service(actix_files::Files::new("/static", &static_root))
})
.bind((address, port))?
.run()
.await
}