Serve web client if configuration option is present

This commit is contained in:
silverpill 2022-12-22 20:27:56 +00:00
parent b14338d5f0
commit dea6bcad2a
7 changed files with 44 additions and 3 deletions

View file

@ -1,5 +1,6 @@
database_url: postgres://mitra:mitra@127.0.0.1:55432/mitra
storage_dir: files
web_client_dir: null
http_host: '127.0.0.1'
http_port: 8380

View file

@ -2,6 +2,8 @@
database_url: postgres://mitra:mitra@127.0.0.1:5432/mitra
storage_dir: /var/lib/mitra
# Path to web client static files
#web_client_dir: /usr/share/mitra/www
http_host: '127.0.0.1'
http_port: 8383

View file

@ -69,6 +69,7 @@ pub struct Config {
// Core settings
pub database_url: String,
pub storage_dir: PathBuf,
pub web_client_dir: Option<PathBuf>,
pub http_host: String,
pub http_port: u32,

View file

@ -18,4 +18,4 @@ mod monero;
pub mod nodeinfo;
mod utils;
pub mod webfinger;
mod web_client;
pub mod web_client;

View file

@ -33,6 +33,7 @@ use mitra::mastodon_api::timelines::views::timeline_api_scope;
use mitra::mastodon_api::UPLOAD_MAX_SIZE;
use mitra::nodeinfo::views as nodeinfo;
use mitra::webfinger::views as webfinger;
use mitra::web_client::views::web_client_service;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
@ -155,8 +156,6 @@ async fn main() -> std::io::Result<()> {
.service(activitypub::actor_scope())
.service(activitypub::instance_actor_scope())
.service(activitypub::object_view)
.service(activitypub::frontend_profile_redirect)
.service(activitypub::frontend_post_redirect)
.service(atom::get_atom_feed)
.service(nodeinfo::get_nodeinfo)
.service(nodeinfo::get_nodeinfo_2_0);
@ -169,6 +168,14 @@ async fn main() -> std::io::Result<()> {
));
};
};
if let Some(ref web_client_dir) = config.web_client_dir {
app = app.service(web_client_service(web_client_dir));
} else {
// Enable redirects only when serving of web client is disabled
app = app
.service(activitypub::frontend_profile_redirect)
.service(activitypub::frontend_post_redirect)
};
app
})
.workers(num_workers)

View file

@ -1 +1,2 @@
pub mod urls;
pub mod views;

29
src/web_client/views.rs Normal file
View file

@ -0,0 +1,29 @@
use std::path::Path;
use actix_files::{Files, NamedFile};
use actix_web::{
dev::{fn_service, ServiceRequest, ServiceResponse},
web::Data,
};
use crate::config::Config;
pub fn web_client_service(web_client_dir: &Path) -> Files {
Files::new("/", web_client_dir)
.index_file("index.html")
.prefer_utf8(true)
.default_handler(fn_service(|service_request: ServiceRequest| {
// Workaround for https://github.com/actix/actix-web/issues/2617
let (request, _) = service_request.into_parts();
let index_path = request.app_data::<Data<Config>>()
.expect("app data should contain config")
.web_client_dir.as_ref()
.expect("web_client_dir should be present in config")
.join("index.html");
async {
let index_file = NamedFile::open_async(index_path).await?;
let response = index_file.into_response(&request);
Ok(ServiceResponse::new(request, response))
}
}))
}