mirror of
https://github.com/actix/examples
synced 2024-12-02 18:02:22 +01:00
81 lines
2.7 KiB
Rust
81 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::{self, ConnectionManager},
|
|
};
|
|
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<()> {
|
|
dotenv::dotenv().ok();
|
|
std::env::set_var(
|
|
"RUST_LOG",
|
|
"simple-auth-server=debug,actix_web=info,actix_server=info",
|
|
);
|
|
env_logger::init();
|
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
|
|
|
// create db connection pool
|
|
let manager = 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_string());
|
|
|
|
// Start http server
|
|
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
|
|
}
|