2020-05-06 13:25:54 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2020-05-09 06:37:08 +00:00
|
|
|
#[macro_use]
|
2020-05-11 19:33:11 +00:00
|
|
|
extern crate derive_new;
|
2020-05-09 06:37:08 +00:00
|
|
|
extern crate bcrypt;
|
|
|
|
extern crate custom_error;
|
|
|
|
extern crate regex;
|
|
|
|
extern crate validator;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
use actix_web::{cookie::SameSite, middleware, web, App, HttpServer};
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
use actix_files as fs;
|
2020-05-09 06:37:08 +00:00
|
|
|
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;
|
2020-08-12 07:05:26 +00:00
|
|
|
use types::DbPool;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
|
|
|
mod actions;
|
2020-05-09 06:37:08 +00:00
|
|
|
mod errors;
|
|
|
|
mod middlewares;
|
2020-05-06 13:25:54 +00:00
|
|
|
mod models;
|
|
|
|
mod routes;
|
|
|
|
mod schema;
|
2021-04-21 05:41:48 +00:00
|
|
|
mod services;
|
2020-05-06 13:25:54 +00:00
|
|
|
mod types;
|
2020-05-09 06:37:08 +00:00
|
|
|
mod utils;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2020-08-12 07:05:26 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppConfig {
|
|
|
|
hash_cost: u32,
|
|
|
|
pool: DbPool,
|
|
|
|
}
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +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
|
|
|
|
2021-04-21 05:41:48 +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),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
}
|
2020-05-11 19:33:11 +00:00
|
|
|
|
2020-08-12 07:05:26 +00:00
|
|
|
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
|
|
|
|
);
|
2020-08-12 07:05:26 +00:00
|
|
|
8
|
|
|
|
});
|
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
let config: AppConfig = AppConfig {
|
|
|
|
pool: pool.clone(),
|
|
|
|
hash_cost,
|
|
|
|
};
|
|
|
|
|
|
|
|
// let user_controller = UserController {
|
|
|
|
// user_service: &user_service,
|
|
|
|
// };
|
2020-08-12 07:05:26 +00:00
|
|
|
|
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()
|
2020-08-12 07:05:26 +00:00
|
|
|
.data(config.clone())
|
2020-05-06 13:25:54 +00:00
|
|
|
.wrap(IdentityService::new(
|
|
|
|
CookieIdentityPolicy::new(&private_key)
|
|
|
|
.name("my-app-auth")
|
|
|
|
.secure(false)
|
2021-04-21 05:41:48 +00:00
|
|
|
.same_site(SameSite::Lax),
|
2020-05-06 13:25:54 +00:00
|
|
|
))
|
|
|
|
.wrap(middleware::Logger::default())
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(
|
2021-05-02 16:28:18 +00:00
|
|
|
web::scope("/api")
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(routes::users::get_user)
|
|
|
|
.service(routes::users::get_all_users),
|
|
|
|
)
|
2021-04-21 05:41:48 +00:00
|
|
|
// .route("/api/users/get", web::get().to(user_controller.get_user.into()))
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(web::scope("/api/public")) // public endpoint - not implemented yet
|
2020-05-09 06:37:08 +00:00
|
|
|
.service(routes::auth::login)
|
|
|
|
.service(routes::auth::logout)
|
|
|
|
.service(routes::auth::index)
|
2020-08-11 19:51:20 +00:00
|
|
|
.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
|
|
|
}
|