Actix-Demo/src/main.rs

135 lines
3.7 KiB
Rust
Raw Normal View History

2020-05-06 13:25:54 +00:00
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate derive_new;
extern crate bcrypt;
extern crate custom_error;
extern crate regex;
extern crate validator;
2020-05-06 13:25:54 +00:00
use actix_web::{cookie::SameSite, middleware, web, App, HttpServer};
2020-05-06 13:25:54 +00:00
use actix_files as fs;
use actix_identity::{CookieIdentityPolicy, IdentityService};
2020-05-06 13:25:54 +00:00
use rand::Rng;
use diesel::prelude::*;
use diesel::r2d2::{self, ConnectionManager};
2021-05-02 11:17:23 +00:00
use std::io;
use std::io::ErrorKind;
use types::DbPool;
2020-05-06 13:25:54 +00:00
mod actions;
mod errors;
mod middlewares;
2020-05-06 13:25:54 +00:00
mod models;
mod routes;
mod schema;
mod services;
2020-05-06 13:25:54 +00:00
mod types;
mod utils;
2020-05-06 13:25:54 +00:00
#[macro_use]
extern crate log;
#[derive(Clone)]
pub struct AppConfig {
hash_cost: u32,
pool: DbPool,
}
2020-05-06 13:25:54 +00:00
#[actix_web::main]
2020-05-06 13:25:54 +00:00
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "debug");
env_logger::init();
2021-05-02 11:17:23 +00:00
dotenv::dotenv().map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("Failed to set up env: {:?}", err),
)
})?;
2020-05-06 13:25:54 +00:00
// let _basic_auth_middleware =
// HttpAuthentication::basic(utils::auth::validator);
2020-05-06 13:25:54 +00:00
// set up database connection pool
2021-05-02 11:17:23 +00:00
let connspec = std::env::var("DATABASE_URL").map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("Database url is not set: {:?}", err),
)
})?;
2020-05-06 13:25:54 +00:00
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
2021-05-02 11:17:23 +00:00
let pool = r2d2::Pool::builder().build(manager).map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("Failed to create pool: {:?}", err),
)
})?;
2020-05-06 13:25:54 +00:00
2021-05-02 11:17:23 +00:00
{
let conn = &pool.get().map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("Failed to get connection: {:?}", err),
)
})?;
diesel_migrations::run_pending_migrations(conn).map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("Error running migrations: {:?}", err),
)
})?;
}
let hash_cost = std::env::var("HASH_COST")
.map_err(|e| e.to_string())
.and_then(|x| x.parse::<u32>().map_err(|e| e.to_string()))
2021-05-02 11:17:23 +00:00
.unwrap_or_else(|err| {
info!(
"Error getting hash cost: {:?}. Using default cost of 8",
err
);
8
});
let config: AppConfig = AppConfig {
pool: pool.clone(),
hash_cost,
};
// let user_controller = UserController {
// user_service: &user_service,
// };
2021-05-02 11:36:42 +00:00
let addr = std::env::var("BIND_ADDRESS")
.unwrap_or_else(|_| "127.0.0.1:7800".to_owned());
2021-05-02 11:17:23 +00:00
info!("Starting server at {}", addr);
2020-05-06 13:25:54 +00:00
let private_key = rand::thread_rng().gen::<[u8; 32]>();
let app = move || {
App::new()
.data(config.clone())
2020-05-06 13:25:54 +00:00
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&private_key)
.name("my-app-auth")
.secure(false)
.same_site(SameSite::Lax),
2020-05-06 13:25:54 +00:00
))
.wrap(middleware::Logger::default())
.service(
2021-05-02 16:28:18 +00:00
web::scope("/api")
.service(routes::users::get_user)
.service(routes::users::get_all_users),
)
// .route("/api/users/get", web::get().to(user_controller.get_user.into()))
.service(web::scope("/api/public")) // public endpoint - not implemented yet
.service(routes::auth::login)
.service(routes::auth::logout)
.service(routes::auth::index)
.service(routes::users::add_user)
2020-05-06 13:25:54 +00:00
.service(fs::Files::new("/", "./static"))
};
2021-05-02 16:28:18 +00:00
HttpServer::new(app).bind(addr)?.run().await
2020-05-06 13:25:54 +00:00
}