1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00
2023-10-29 01:18:40 +01:00

76 lines
2.7 KiB
Rust

#[macro_use]
extern crate diesel;
use actix_identity::IdentityMiddleware;
use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware};
use actix_web::{cookie::Key, middleware, web, App, HttpServer};
use diesel::{prelude::*, r2d2};
use time::Duration;
mod auth_handler;
mod email_service;
mod errors;
mod invitation_handler;
mod models;
mod register_handler;
mod schema;
mod utils;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenvy::dotenv().ok();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
// create db connection pool
let manager = r2d2::ConnectionManager::<PgConnection>::new(database_url);
let pool: models::Pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_owned());
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone()))
.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(),
)
// enable logger
.wrap(middleware::Logger::default())
// everything under '/api/' route
.service(
web::scope("/api")
.service(
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)),
)
.service(
web::resource("/auth")
.route(web::post().to(auth_handler::login))
.route(web::delete().to(auth_handler::logout))
.route(web::get().to(auth_handler::get_me)),
),
)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}