[web] [WIP] Fix sending OGP tags
This commit is contained in:
parent
56dbe50d2f
commit
2cfe9fcb5f
|
|
@ -6,6 +6,13 @@ port: 1312
|
||||||
# The address the server should bind to
|
# The address the server should bind to
|
||||||
address: 0.0.0.0
|
address: 0.0.0.0
|
||||||
|
|
||||||
|
# The base URL the server will be running under, with no trailing slash
|
||||||
|
#
|
||||||
|
# This is also your opportunity to serve requests under a subpath. For example, if you
|
||||||
|
# want pronouns.today to only run on the /pronouns route of your webserver, you can set
|
||||||
|
# this to https://example.com/pronouns
|
||||||
|
base_url: https://pronouns.today
|
||||||
|
|
||||||
# A list of pronouns recognized by the server
|
# A list of pronouns recognized by the server
|
||||||
#
|
#
|
||||||
# WARNING: When adding pronouns, only add pronouns to the bottom of the list, and do not
|
# WARNING: When adding pronouns, only add pronouns to the bottom of the list, and do not
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,11 @@ pub struct Run {
|
||||||
/// for warnings when changing this value. formatted as a list of comma seperated
|
/// for warnings when changing this value. formatted as a list of comma seperated
|
||||||
/// five-form pronouns, e.g. she/her/her/hers/herself,he/him/his/his/himself
|
/// five-form pronouns, e.g. she/her/her/hers/herself,he/him/his/his/himself
|
||||||
pub pronouns: Option<PronounList>,
|
pub pronouns: Option<PronounList>,
|
||||||
|
|
||||||
|
#[argh(option, long="base")]
|
||||||
|
/// the base url content is served under, starting with the protocol (https://) and without a
|
||||||
|
/// trailing slash
|
||||||
|
pub base_url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Run {
|
impl Run {
|
||||||
|
|
@ -131,6 +136,9 @@ pub struct Conf {
|
||||||
|
|
||||||
/// The address to bind to. Defaults to 0.0.0.0
|
/// The address to bind to. Defaults to 0.0.0.0
|
||||||
pub address: IpAddr,
|
pub address: IpAddr,
|
||||||
|
|
||||||
|
/// The base url the server will be running under, with no trailing slash
|
||||||
|
pub base_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Conf {
|
impl Conf {
|
||||||
|
|
@ -138,6 +146,7 @@ impl Conf {
|
||||||
self.port = args.port.unwrap_or(self.port);
|
self.port = args.port.unwrap_or(self.port);
|
||||||
self.address = args.address.unwrap_or(self.address);
|
self.address = args.address.unwrap_or(self.address);
|
||||||
self.instance_settings.pronoun_list = args.pronouns.map(Into::into).unwrap_or(self.instance_settings.pronoun_list);
|
self.instance_settings.pronoun_list = args.pronouns.map(Into::into).unwrap_or(self.instance_settings.pronoun_list);
|
||||||
|
self.base_url = args.base_url.unwrap_or(self.base_url);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -148,6 +157,7 @@ impl Default for Conf {
|
||||||
instance_settings: InstanceSettings::default(),
|
instance_settings: InstanceSettings::default(),
|
||||||
port: 1312,
|
port: 1312,
|
||||||
address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
||||||
|
base_url: "https://pronouns.today".into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ pub mod contrast;
|
||||||
pub mod statics;
|
pub mod statics;
|
||||||
pub mod configuration;
|
pub mod configuration;
|
||||||
|
|
||||||
|
use crate::configuration::Conf;
|
||||||
use std::io::IoSlice;
|
use std::io::IoSlice;
|
||||||
use crate::statics::StaticAsset;
|
use crate::statics::StaticAsset;
|
||||||
use smol::io::AsyncWriteExt;
|
use smol::io::AsyncWriteExt;
|
||||||
|
|
@ -105,13 +106,13 @@ async fn start_server(config: configuration::Conf, executor: &smol::LocalExecuto
|
||||||
let connection = smol::net::TcpListener::bind(socket_addr).await?;
|
let connection = smol::net::TcpListener::bind(socket_addr).await?;
|
||||||
let mut incoming = connection.incoming();
|
let mut incoming = connection.incoming();
|
||||||
|
|
||||||
// Make the instance settings immortal
|
// Make the configuration immortal
|
||||||
let instance_settings = Box::leak(Box::new(config.instance_settings));
|
let config = Box::leak(Box::new(config));
|
||||||
|
|
||||||
while let Some(stream) = incoming.next().await {
|
while let Some(stream) = incoming.next().await {
|
||||||
match stream {
|
match stream {
|
||||||
Ok(stream) => {
|
Ok(stream) => {
|
||||||
executor.spawn(handle_request(stream, instance_settings)).detach();
|
executor.spawn(handle_request(stream, config)).detach();
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("IO Error with client/server connection: {}", e);
|
log::error!("IO Error with client/server connection: {}", e);
|
||||||
|
|
@ -135,7 +136,7 @@ async fn start_server(config: configuration::Conf, executor: &smol::LocalExecuto
|
||||||
/// - Serialization of the response is done using [`Response::into_bytes()`]
|
/// - Serialization of the response is done using [`Response::into_bytes()`]
|
||||||
async fn handle_request(
|
async fn handle_request(
|
||||||
raw_stream: impl smol::io::AsyncRead + smol::io::AsyncWrite + Unpin,
|
raw_stream: impl smol::io::AsyncRead + smol::io::AsyncWrite + Unpin,
|
||||||
settings: &InstanceSettings
|
conf: &Conf,
|
||||||
) {
|
) {
|
||||||
let mut stream = smol::io::BufReader::new(raw_stream);
|
let mut stream = smol::io::BufReader::new(raw_stream);
|
||||||
|
|
||||||
|
|
@ -153,7 +154,7 @@ async fn handle_request(
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = route_request(&req)
|
let response = route_request(&req)
|
||||||
.generate_response(settings);
|
.generate_response(conf);
|
||||||
let io_slices = response.into_io_slices();
|
let io_slices = response.into_io_slices();
|
||||||
|
|
||||||
for slice in io_slices {
|
for slice in io_slices {
|
||||||
|
|
@ -194,9 +195,10 @@ fn route_request(req: &ScgiRequest) -> Route {
|
||||||
_ => return Route::BadRequest(BadRequest::MethodNotAllowed),
|
_ => return Route::BadRequest(BadRequest::MethodNotAllowed),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut segments: Vec<_> = req.headers
|
let request_uri = req.headers
|
||||||
.get("REQUEST_URI")
|
.get("REQUEST_URI")
|
||||||
.expect("Misconfigured server didn't send a REQUEST_URI header")
|
.expect("Misconfigured server didn't send a REQUEST_URI header");
|
||||||
|
let mut segments: Vec<_> = request_uri
|
||||||
.split("/")
|
.split("/")
|
||||||
.filter(|s| s.len() > 0)
|
.filter(|s| s.len() > 0)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
@ -241,13 +243,14 @@ fn route_request(req: &ScgiRequest) -> Route {
|
||||||
(None, Some(_)) => unreachable!(),
|
(None, Some(_)) => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let response_type = if is_thumb {
|
let name = name.map(|n| (*n).to_owned());
|
||||||
Route::SendThumbnail
|
|
||||||
} else {
|
|
||||||
Route::SendPronounPage
|
|
||||||
};
|
|
||||||
|
|
||||||
response_type(name.map(|n| (*n).to_owned()), prefs)
|
if is_thumb {
|
||||||
|
Route::SendThumbnail(name, prefs)
|
||||||
|
} else {
|
||||||
|
let uri = request_uri.trim_end_matches('/').to_owned();
|
||||||
|
Route::SendPronounPage(name, prefs, uri)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determines the type of route that the request should be met with, and necessary data
|
/// Determines the type of route that the request should be met with, and necessary data
|
||||||
|
|
@ -276,8 +279,9 @@ enum Route {
|
||||||
/// Respond with an HTML pronoun page.
|
/// Respond with an HTML pronoun page.
|
||||||
///
|
///
|
||||||
/// Takes the user's name (if specified), and the preferences of that user, or
|
/// Takes the user's name (if specified), and the preferences of that user, or
|
||||||
/// [`None`] for defults
|
/// [`None`] for defults. The third string is the URI of the current page,
|
||||||
SendPronounPage(Option<String>, Option<UserPreferences>),
|
/// ending without a /
|
||||||
|
SendPronounPage(Option<String>, Option<UserPreferences>, String),
|
||||||
|
|
||||||
/// Respond with an image containing the user's pronouns
|
/// Respond with an image containing the user's pronouns
|
||||||
///
|
///
|
||||||
|
|
@ -312,11 +316,12 @@ enum BadRequest {
|
||||||
|
|
||||||
impl Route {
|
impl Route {
|
||||||
/// Actually perform the action that each route entails
|
/// Actually perform the action that each route entails
|
||||||
fn generate_response(self, settings: &InstanceSettings) -> Response {
|
fn generate_response(self, conf: &Conf) -> Response {
|
||||||
|
let settings = &conf.instance_settings;
|
||||||
let result = match self {
|
let result = match self {
|
||||||
Route::SendStatic(data) => Ok(data.generate_response()),
|
Route::SendStatic(data) => Ok(data.generate_response()),
|
||||||
Route::SendPronounPage(name, prefs) =>
|
Route::SendPronounPage(name, prefs, url) =>
|
||||||
Route::send_pronoun_page(name, prefs, settings),
|
Route::send_pronoun_page(name, prefs, url, conf),
|
||||||
Route::SendThumbnail(name, prefs) =>
|
Route::SendThumbnail(name, prefs) =>
|
||||||
Route::send_thumbnail(name, prefs, settings),
|
Route::send_thumbnail(name, prefs, settings),
|
||||||
Route::GenerateLink =>
|
Route::GenerateLink =>
|
||||||
|
|
@ -332,15 +337,17 @@ impl Route {
|
||||||
fn send_pronoun_page(
|
fn send_pronoun_page(
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
prefs: Option<UserPreferences>,
|
prefs: Option<UserPreferences>,
|
||||||
settings: &InstanceSettings,
|
uri: String,
|
||||||
|
conf: &Conf,
|
||||||
) -> Result<Response, BadRequest> {
|
) -> Result<Response, BadRequest> {
|
||||||
|
let settings = &conf.instance_settings;
|
||||||
let pronoun = Route::get_pronoun(name.as_ref(), prefs, settings)?;
|
let pronoun = Route::get_pronoun(name.as_ref(), prefs, settings)?;
|
||||||
|
|
||||||
let body = IndexTemplate {
|
let body = IndexTemplate {
|
||||||
name,
|
name,
|
||||||
pronoun,
|
pronoun,
|
||||||
pronouns: settings.pronoun_list.iter().enumerate().collect(),
|
pronouns: settings.pronoun_list.iter().enumerate().collect(),
|
||||||
url: String::new(), //TODO
|
url: format!("{}{}", &conf.base_url, uri), //TODO
|
||||||
}
|
}
|
||||||
.render()
|
.render()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue