GeneIT/geneit_backend/src/main.rs

117 lines
4.1 KiB
Rust

use actix_cors::Cors;
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::controllers::{
auth_controller, families_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);
HttpServer::new(|| {
App::new()
.wrap(
Cors::default()
.allowed_origin(&AppConfig::get().website_origin)
.allowed_methods(vec!["GET", "POST"])
.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(),
}))
// 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::delete().to(families_controller::delete),
)
.route(
"/family/{id}/users",
web::get().to(families_controller::users),
)
})
.bind(AppConfig::get().listen_address.as_str())?
.run()
.await
}