1
0
mirror of https://github.com/actix/examples synced 2024-12-02 18:02:22 +01:00
examples/simple-auth-server/src/main.rs

74 lines
2.5 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate diesel;
#[macro_use]
extern crate serde_derive;
2019-06-17 08:48:03 +02:00
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::{middleware, web, App, HttpServer};
use diesel::prelude::*;
use diesel::r2d2::{self, ConnectionManager};
2019-03-29 21:43:03 +01:00
mod auth_handler;
2019-03-10 03:03:09 +01:00
mod email_service;
mod errors;
mod invitation_handler;
2019-03-10 03:03:09 +01:00
mod models;
mod register_handler;
2019-03-10 03:03:09 +01:00
mod schema;
mod utils;
2019-03-29 21:43:03 +01:00
fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
2019-03-29 21:43:03 +01:00
std::env::set_var(
"RUST_LOG",
"simple-auth-server=debug,actix_web=info,actix_server=info",
);
env_logger::init();
2019-03-29 21:43:03 +01:00
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.");
2019-07-18 14:03:19 +02:00
let domain: String =
std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
// Start http server
2019-03-29 21:43:03 +01:00
HttpServer::new(move || {
App::new()
.data(pool.clone())
// enable logger
.wrap(middleware::Logger::default())
2019-03-29 21:43:03 +01:00
.wrap(IdentityService::new(
CookieIdentityPolicy::new(utils::SECRET_KEY.as_bytes())
2019-03-29 21:43:03 +01:00
.name("auth")
.path("/")
.domain(domain.as_str())
.max_age_time(chrono::Duration::days(1))
2019-03-29 21:43:03 +01:00
.secure(false), // this can only be true if you have https
))
.data(web::JsonConfig::default().limit(4096))
2019-03-29 21:43:03 +01:00
// everything under '/api/' route
.service(
web::scope("/api")
2019-07-18 14:03:19 +02:00
.service(web::resource("/invitation").route(
web::post().to_async(invitation_handler::post_invitation),
))
2019-03-29 21:43:03 +01:00
.service(
2019-07-18 14:03:19 +02:00
web::resource("/register/{invitation_id}").route(
web::post().to_async(register_handler::register_user),
),
2019-03-29 21:43:03 +01:00
)
.service(
web::resource("/auth")
.route(web::post().to_async(auth_handler::login))
.route(web::delete().to(auth_handler::logout))
.route(web::get().to(auth_handler::get_me)),
2019-03-29 21:43:03 +01:00
),
)
})
.bind("127.0.0.1:3000")?
.run()
}