Json endpoint #12

Closed
sashanoraa wants to merge 18 commits from json-resp into main
2 changed files with 13 additions and 10 deletions
Showing only changes of commit 4ed748cf1e - Show all commits

View file

@ -8,7 +8,9 @@ edition = "2021"
[dependencies] [dependencies]
pronouns_today = {path = ".."} pronouns_today = {path = ".."}
async-scgi = "0.1.0" async-scgi = "0.1.0"
smol = "1.2" async-executor = "1.4"
async-net = "1.6"
futures-lite = "1.12"
log = "0.4" log = "0.4"
env_logger = "0.9" env_logger = "0.9"
askama = "0.10" askama = "0.10"

View file

@ -6,8 +6,10 @@ pub mod configuration;
use crate::configuration::Conf; 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 async_net::TcpListener;
use smol::stream::StreamExt; use async_executor::LocalExecutor;
use futures_lite::io::AsyncWriteExt;
use futures_lite::stream::StreamExt;
use std::borrow::Cow; use std::borrow::Cow;
use form_urlencoded; use form_urlencoded;
use percent_encoding::{percent_decode_str, percent_encode, NON_ALPHANUMERIC}; use percent_encoding::{percent_decode_str, percent_encode, NON_ALPHANUMERIC};
@ -24,7 +26,6 @@ use askama::Template;
use async_scgi::{ScgiReadError, ScgiRequest}; use async_scgi::{ScgiReadError, ScgiRequest};
use pronouns_today::user_preferences::Preference; use pronouns_today::user_preferences::Preference;
use pronouns_today::{InstanceSettings, Pronoun}; use pronouns_today::{InstanceSettings, Pronoun};
use smol;
#[cfg(feature = "ogp_images")] #[cfg(feature = "ogp_images")]
use image::{DynamicImage, ImageOutputFormat}; use image::{DynamicImage, ImageOutputFormat};
@ -63,9 +64,9 @@ fn main() {
} }
}; };
let executor = smol::LocalExecutor::new(); let executor = LocalExecutor::new();
log::info!("Starting with configuration {:?}", config); log::info!("Starting with configuration {:?}", config);
smol::block_on(executor.run(start_server(config, &executor))).unwrap(); futures_lite::future::block_on(executor.run(start_server(config, &executor))).unwrap();
} }
} }
} }
@ -76,13 +77,13 @@ fn main() {
/// connection is fully processed, but not in this method. Actual handling of the /// connection is fully processed, but not in this method. Actual handling of the
/// requests is delegated to the [`handle_request()`], which is the actual task that is /// requests is delegated to the [`handle_request()`], which is the actual task that is
/// spawned into the Executor. /// spawned into the Executor.
async fn start_server(config: configuration::Conf, executor: &smol::LocalExecutor<'_>) -> std::io::Result<()> { async fn start_server(config: configuration::Conf, executor: &LocalExecutor<'_>) -> std::io::Result<()> {
// Where we binding bois // Where we binding bois
let socket_addr = SocketAddr::new(config.address, config.port); let socket_addr = SocketAddr::new(config.address, config.port);
println!("Starting pronouns-today-web on {}", &socket_addr); println!("Starting pronouns-today-web on {}", &socket_addr);
let connection = smol::net::TcpListener::bind(socket_addr).await?; let connection = TcpListener::bind(socket_addr).await?;
let mut incoming = connection.incoming(); let mut incoming = connection.incoming();
// Make the configuration immortal // Make the configuration immortal
@ -114,10 +115,10 @@ async fn start_server(config: configuration::Conf, executor: &smol::LocalExecuto
/// - Generation of the response is done using [`Route::generate_response()`] /// - Generation of the response is done using [`Route::generate_response()`]
/// - 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 futures_lite::AsyncRead + futures_lite::AsyncWrite + Unpin,
conf: &Conf, conf: &Conf,
) { ) {
let mut stream = smol::io::BufReader::new(raw_stream); let mut stream = futures_lite::io::BufReader::new(raw_stream);
let req = match async_scgi::read_request(&mut stream).await { let req = match async_scgi::read_request(&mut stream).await {
Ok(req) => req, Ok(req) => req,