#[macro_use]
extern crate rocket;

use auth::session;
use db::migrate::run_migrations;
use db::schema::DollTags;
use rocket::fairing::AdHoc;
use rocket::fs::{relative, FileServer};
use rocket_db_pools::Database;
use routes::form::accounts;
use routes::{account, error_handlers, form, public};

pub mod auth;
pub mod db;
pub mod ids;
pub mod pages;
pub mod routes;

#[launch]
fn rocket() -> _ {
	rocket::build()
		.attach(pages::init_templates())
		.attach(DollTags::init())
		.attach(AdHoc::try_on_ignite("SQLx migrations", run_migrations))
		.register(
			"/",
			catchers![error_handlers::not_found, session::unauthorized],
		)
		.mount("/assets", FileServer::from(relative!("/assets")))
		.mount(
			"/account",
			routes![
				account::index,
				account::show_settings,
				account::change_settings,
				account::change_password,
				form::register_tag::show_register,
				form::register_tag::handle_register,
				form::register_tag::show_edit_tag,
				form::register_tag::handle_edit_tag,
				account::ask_delete,
				account::confirm_delete,
				account::ask_terminate_account,
				account::confirm_terminate_account,
				account::export_data,
			],
		)
		.mount(
			"/",
			routes![
				public::index,
				public::short_url,
				public::show_profile,
				accounts::show_register,
				accounts::handle_register,
				accounts::show_login,
				accounts::handle_login,
				accounts::logout,
			],
		)
}
