/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin */ use crate::{database::Database, federation::Federation, routes::ui::error::MyResult}; use api::{r_api_account_login, r_api_node_raw, r_api_root, r_api_version}; use base64::Engine; use jellybase::CONF; use log::warn; use rand::random; use rocket::{ catchers, config::SecretKey, fairing::AdHoc, fs::FileServer, get, http::Header, routes, Build, Config, Rocket, }; use std::fs::File; use stream::r_stream; use ui::{ account::{ r_account_login, r_account_login_post, r_account_logout, r_account_logout_post, r_account_register, r_account_register_post, settings::{r_account_settings, r_account_settings_post}, }, admin::{ log::r_admin_log, r_admin_dashboard, r_admin_delete_cache, r_admin_import, r_admin_invite, r_admin_remove_invite, r_admin_remove_user, }, assets::r_item_assets, browser::r_all_items_filter, error::{r_api_catch, r_catch}, home::{r_home, r_home_unpriv}, node::r_library_node_filter, player::r_player, style::{r_assets_font, r_assets_js, r_assets_js_map, r_assets_style}, }; pub mod api; pub mod stream; pub mod ui; #[macro_export] macro_rules! uri { ($kk:stmt) => { &rocket::uri!($kk).to_string() }; } pub fn build_rocket(database: Database, federation: Federation) -> Rocket { rocket::build() .configure(Config { address: std::env::var("BIND_ADDR") .map(|e| e.parse().unwrap()) .unwrap_or("127.0.0.1".parse().unwrap()), port: std::env::var("PORT") .map(|e| e.parse().unwrap()) .unwrap_or(8000), secret_key: SecretKey::derive_from( CONF.cookie_key .clone() .unwrap_or_else(|| { warn!("cookie_key not configured, generating a random one."); base64::engine::general_purpose::STANDARD.encode([(); 32].map(|_| random())) }) .as_bytes(), ), ip_header: Some("x-forwarded-for".into()), ..Default::default() }) .manage(database) .manage(federation) .attach(AdHoc::on_response("set server header", |_req, res| { res.set_header(Header::new("server", "jellything")); Box::pin(async {}) })) .register("/", catchers![r_catch]) .register("/api", catchers![r_api_catch]) .mount("/assets", FileServer::from(&CONF.asset_path)) .mount( "/", routes![ r_home, r_home_unpriv, r_favicon, r_item_assets, r_all_items_filter, r_library_node_filter, r_assets_style, r_assets_font, r_assets_js, r_assets_js_map, r_stream, r_player, r_account_login, r_account_login_post, r_account_register, r_account_register_post, r_account_logout, r_account_logout_post, r_admin_dashboard, r_admin_invite, r_admin_remove_user, r_admin_remove_invite, r_admin_import, r_admin_delete_cache, r_admin_log, r_account_settings, r_account_settings_post, r_api_version, r_api_account_login, r_api_root, r_api_node_raw, ], ) } #[get("/favicon.ico")] fn r_favicon() -> MyResult { Ok(File::open(CONF.asset_path.join("favicon.ico"))?) }