2018-12-09 15:55:36 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
|
2022-10-26 16:51:27 +01:00
|
|
|
use actix_identity::IdentityMiddleware;
|
|
|
|
use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware};
|
|
|
|
use actix_web::{cookie::Key, middleware, web, App, HttpServer};
|
2023-03-14 03:11:49 +00:00
|
|
|
use diesel::{prelude::*, r2d2};
|
2020-09-12 16:49:45 +01:00
|
|
|
use time::Duration;
|
2019-03-29 13:43:03 -07:00
|
|
|
|
2018-12-09 15:55:36 +00:00
|
|
|
mod auth_handler;
|
2019-03-09 18:03:09 -08:00
|
|
|
mod email_service;
|
|
|
|
mod errors;
|
2018-12-09 15:55:36 +00:00
|
|
|
mod invitation_handler;
|
2019-03-09 18:03:09 -08:00
|
|
|
mod models;
|
2018-12-09 15:55:36 +00:00
|
|
|
mod register_handler;
|
2019-03-09 18:03:09 -08:00
|
|
|
mod schema;
|
2018-12-09 15:55:36 +00:00
|
|
|
mod utils;
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2023-10-29 01:18:40 +01:00
|
|
|
dotenvy::dotenv().ok();
|
2023-03-14 03:11:49 +00:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
2019-03-29 13:43:03 -07:00
|
|
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
2018-12-09 15:55:36 +00:00
|
|
|
|
|
|
|
// create db connection pool
|
2023-03-14 03:11:49 +00:00
|
|
|
let manager = r2d2::ConnectionManager::<PgConnection>::new(database_url);
|
2019-07-18 12:55:14 +01:00
|
|
|
let pool: models::Pool = r2d2::Pool::builder()
|
2018-12-09 15:55:36 +00:00
|
|
|
.build(manager)
|
|
|
|
.expect("Failed to create pool.");
|
2023-07-09 03:32:47 +01:00
|
|
|
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_owned());
|
2018-12-09 15:55:36 +00:00
|
|
|
|
2023-03-14 03:11:49 +00:00
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
|
|
|
|
2019-03-29 13:43:03 -07:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2022-02-02 15:19:49 +00:00
|
|
|
.app_data(web::Data::new(pool.clone()))
|
2022-10-26 16:51:27 +01:00
|
|
|
.wrap(IdentityMiddleware::default())
|
|
|
|
.wrap(
|
|
|
|
SessionMiddleware::builder(
|
|
|
|
CookieSessionStore::default(),
|
|
|
|
Key::from(utils::SECRET_KEY.as_bytes()),
|
|
|
|
)
|
|
|
|
.session_lifecycle(PersistentSession::default().session_ttl(Duration::days(1)))
|
|
|
|
.cookie_name("auth-example".to_owned())
|
|
|
|
.cookie_secure(false)
|
|
|
|
.cookie_domain(Some(domain.clone()))
|
|
|
|
.cookie_path("/".to_owned())
|
|
|
|
.build(),
|
|
|
|
)
|
2019-07-18 12:55:14 +01:00
|
|
|
// enable logger
|
|
|
|
.wrap(middleware::Logger::default())
|
2019-03-29 13:43:03 -07:00
|
|
|
// everything under '/api/' route
|
|
|
|
.service(
|
|
|
|
web::scope("/api")
|
|
|
|
.service(
|
2019-12-07 23:59:24 +06:00
|
|
|
web::resource("/invitation")
|
|
|
|
.route(web::post().to(invitation_handler::post_invitation)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/register/{invitation_id}")
|
|
|
|
.route(web::post().to(register_handler::register_user)),
|
2019-03-29 13:43:03 -07:00
|
|
|
)
|
|
|
|
.service(
|
2019-07-18 12:55:14 +01:00
|
|
|
web::resource("/auth")
|
2019-12-07 23:59:24 +06:00
|
|
|
.route(web::post().to(auth_handler::login))
|
2019-07-18 12:55:14 +01:00
|
|
|
.route(web::delete().to(auth_handler::logout))
|
|
|
|
.route(web::get().to(auth_handler::get_me)),
|
2019-03-29 13:43:03 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
})
|
2022-03-06 00:41:32 +00:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
2018-12-09 15:55:36 +00:00
|
|
|
}
|