GeneIT/geneit_backend/src/main.rs

200 lines
7.2 KiB
Rust

use actix_cors::Cors;
use actix_multipart::form::tempfile::TempFileConfig;
use actix_remote_ip::RemoteIPConfig;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use geneit_backend::app_config::AppConfig;
use geneit_backend::connections::s3_connection;
use geneit_backend::controllers::{
auth_controller, couples_controller, families_controller, members_controller,
photos_controller, server_controller, users_controller,
};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("Start to listen on {}", AppConfig::get().listen_address);
// Initialize bucket
log::info!("Initialize bucket");
s3_connection::create_bucket_if_required()
.await
.expect("Failed to initialize S3 bucket!");
HttpServer::new(|| {
App::new()
.wrap(
Cors::default()
.allowed_origin(&AppConfig::get().website_origin)
.allowed_methods(vec!["GET", "POST", "PATCH", "DELETE"])
.allowed_header("X-Auth-Token")
.allow_any_header()
.supports_credentials()
.max_age(3600),
)
.wrap(Logger::default())
.app_data(web::Data::new(RemoteIPConfig {
proxy: AppConfig::get().proxy_ip.clone(),
}))
// Uploaded files
.app_data(TempFileConfig::default().directory(&AppConfig::get().temp_dir))
// Config controller
.route("/", web::get().to(server_controller::home))
.route(
"/server/config",
web::get().to(server_controller::server_config),
)
// Auth controller
.route(
"/auth/create_account",
web::post().to(auth_controller::create_account),
)
.route(
"/auth/request_reset_password",
web::post().to(auth_controller::request_reset_password),
)
.route(
"/auth/check_reset_password_token",
web::post().to(auth_controller::check_reset_password_token),
)
.route(
"/auth/reset_password",
web::post().to(auth_controller::reset_password),
)
.route(
"/auth/password_login",
web::post().to(auth_controller::password_login),
)
.route(
"/auth/start_openid_login",
web::post().to(auth_controller::start_openid_login),
)
.route(
"/auth/finish_openid_login",
web::post().to(auth_controller::finish_openid_login),
)
.route("/auth/logout", web::get().to(auth_controller::logout))
// User controller
.route("/user/info", web::get().to(users_controller::auth_info))
.route(
"/user/update_profile",
web::post().to(users_controller::update_profile),
)
.route(
"/user/replace_password",
web::post().to(users_controller::replace_password),
)
.route(
"/user/request_delete",
web::get().to(users_controller::request_delete_account),
)
.route(
"/user/check_delete_token",
web::post().to(users_controller::check_delete_token),
)
.route(
"/user/delete_account",
web::post().to(users_controller::delete_account),
)
// Families controller
.route(
"/family/create",
web::post().to(families_controller::create),
)
.route("/family/join", web::post().to(families_controller::join))
.route("/family/list", web::get().to(families_controller::list))
.route(
"/family/{id}",
web::get().to(families_controller::single_info),
)
.route(
"/family/{id}/leave",
web::post().to(families_controller::leave),
)
.route("/family/{id}", web::patch().to(families_controller::update))
.route(
"/family/{id}",
web::delete().to(families_controller::delete),
)
.route(
"/family/{id}/renew_invitation_code",
web::post().to(families_controller::renew_invitation_code),
)
.route(
"/family/{id}/users",
web::get().to(families_controller::users),
)
.route(
"/family/{id}/user/{user_id}",
web::patch().to(families_controller::update_membership),
)
.route(
"/family/{id}/user/{user_id}",
web::delete().to(families_controller::delete_membership),
)
// Members controller
.route(
"/family/{id}/member/create",
web::post().to(members_controller::create),
)
.route(
"/family/{id}/members",
web::get().to(members_controller::get_all),
)
.route(
"/family/{id}/member/{member_id}",
web::get().to(members_controller::get_single),
)
.route(
"/family/{id}/member/{member_id}",
web::put().to(members_controller::update),
)
.route(
"/family/{id}/member/{member_id}",
web::delete().to(members_controller::delete),
)
.route(
"/family/{id}/member/{member_id}/photo",
web::put().to(members_controller::set_photo),
)
.route(
"/family/{id}/member/{member_id}/photo",
web::delete().to(members_controller::remove_photo),
)
// Couples controller
.route(
"/family/{id}/couple/create",
web::post().to(couples_controller::create),
)
.route(
"/family/{id}/couples",
web::get().to(couples_controller::get_all),
)
.route(
"/family/{id}/couple/{couple_id}",
web::get().to(couples_controller::get_single),
)
.route(
"/family/{id}/couple/{couple_id}",
web::put().to(couples_controller::update),
)
.route(
"/family/{id}/couple/{couple_id}",
web::delete().to(couples_controller::delete),
)
// Photos controller
.route(
"/photo/{id}",
web::get().to(photos_controller::get_full_size),
)
.route(
"/photo/{id}/thumbnail",
web::get().to(photos_controller::get_thumbnail),
)
})
.bind(AppConfig::get().listen_address.as_str())?
.run()
.await
}