mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2024-11-10 00:27:21 +01:00
WIP moved templating engine to mycelium
This commit is contained in:
parent
5ac056ef99
commit
5adca4fb80
14
src/main.rs
14
src/main.rs
@ -44,7 +44,6 @@ mod mycelium;
|
||||
mod ratelimit;
|
||||
mod settings;
|
||||
mod simple_dns;
|
||||
mod templating_engine;
|
||||
mod view;
|
||||
|
||||
use crate::geoip::{
|
||||
@ -54,11 +53,14 @@ use crate::geoip::{
|
||||
LocationResult,
|
||||
};
|
||||
use crate::idna::IdnaName;
|
||||
use crate::mycelium::MycEngine;
|
||||
use crate::simple_dns::DnsLookupResult;
|
||||
use crate::settings::*;
|
||||
use crate::view::View;
|
||||
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
|
||||
|
||||
type TemplatingEngine = MycEngine<View,QuerySettings,ResponseFormat>;
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
pub struct SettingsQuery {
|
||||
format: Option<ResponseFormat>,
|
||||
@ -95,7 +97,7 @@ pub struct DigResult {
|
||||
}
|
||||
|
||||
struct ServiceSharedState {
|
||||
templating_engine: templating_engine::Engine,
|
||||
templating_engine: TemplatingEngine,
|
||||
dns_resolvers: HashMap<Arc<str>,TokioAsyncResolver>,
|
||||
dns_resolver_aliases: HashMap<Arc<str>,Arc<str>>,
|
||||
asn_db: geoip::MMDBCarrier,
|
||||
@ -201,10 +203,10 @@ async fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
let templating_engine = templating_engine::Engine{
|
||||
tera: tera,
|
||||
template_config: template_extra_config,
|
||||
};
|
||||
let templating_engine = TemplatingEngine::new(
|
||||
tera,
|
||||
template_extra_config,
|
||||
);
|
||||
|
||||
// Static file directory
|
||||
|
||||
|
@ -1,7 +1,3 @@
|
||||
/*
|
||||
* This is the echoip-slatecave templating engine.
|
||||
* It wraps around tera in is specialized for echoip-slatecave.
|
||||
*/
|
||||
|
||||
use axum::{
|
||||
body::{Bytes,Full},
|
||||
@ -12,38 +8,55 @@ use axum::{
|
||||
response::IntoResponse,
|
||||
response::Response,
|
||||
};
|
||||
use axum_extra::extract::cookie::Cookie;
|
||||
use axum_extra::extract::cookie;
|
||||
use tera::Tera;
|
||||
use toml::Table;
|
||||
|
||||
use crate::view::View;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::mycelium::MycView;
|
||||
use crate::mycelium::MycFormat;
|
||||
use crate::mycelium::MycFormatFamily;
|
||||
use crate::mycelium::MycQuerySettings;
|
||||
use crate::settings::QuerySettings;
|
||||
|
||||
/* The engine itself */
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Engine {
|
||||
pub struct MycEngine<V, S, F>
|
||||
where V: MycView<S, F>, S: MycQuerySettings<F>, F: MycFormat {
|
||||
pub tera: Tera,
|
||||
pub template_config: Option<Table>,
|
||||
|
||||
phantom_view: PhantomData<V>,
|
||||
phantom_settings: PhantomData<S>,
|
||||
phantom_format: PhantomData<F>,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
impl<V, S, F> MycEngine<V, S, F>
|
||||
where V: MycView<S, F>, S: MycQuerySettings<F>, F: MycFormat {
|
||||
|
||||
pub fn new(tera: Tera, template_config: Option<Table>) -> Self {
|
||||
Self {
|
||||
tera: tera,
|
||||
template_config: template_config,
|
||||
phantom_view: PhantomData,
|
||||
phantom_settings: PhantomData,
|
||||
phantom_format: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes settings and a view, converting it to a serveable response.
|
||||
pub async fn render_view(
|
||||
&self,
|
||||
settings: &QuerySettings,
|
||||
view: View,
|
||||
settings: &S,
|
||||
view: V,
|
||||
) -> Response {
|
||||
let status_code = view.get_status_code(&settings);
|
||||
let cookie_string = view.get_cookie_header(&settings);
|
||||
let format = settings.get_format();
|
||||
|
||||
let mut response = match settings.format.get_family() {
|
||||
let mut response = match format.get_family() {
|
||||
MycFormatFamily::Template => {
|
||||
let template_name = view.get_template_name();
|
||||
let format = settings.get_format();
|
||||
let mime_type = format.get_mime_type();
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
@ -81,22 +94,29 @@ impl Engine {
|
||||
view.get_api_response(&settings)
|
||||
}
|
||||
};
|
||||
|
||||
// Everything went well and nobody did the following work for us.
|
||||
if response.status() == StatusCode::OK {
|
||||
|
||||
// Set status code
|
||||
if response.status() == StatusCode::OK && status_code != StatusCode::OK {
|
||||
if status_code != StatusCode::OK {
|
||||
*response.status_mut() = status_code;
|
||||
}
|
||||
// Set cookies
|
||||
let cookie = Cookie::build("dns_resolver",settings.dns_resolver_id.to_string())
|
||||
.path("/")
|
||||
.same_site(cookie::SameSite::Strict)
|
||||
.finish();
|
||||
if let Ok(header_value) = HeaderValue::from_str(&cookie.to_string()) {
|
||||
|
||||
// Set cookie header
|
||||
if let Some(cookie_string) = cookie_string {
|
||||
if let Ok(header_value) = HeaderValue::from_str(&cookie_string) {
|
||||
response.headers_mut().append(
|
||||
SET_COOKIE,
|
||||
header_value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// return response
|
||||
response
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
mod engine;
|
||||
mod format;
|
||||
mod query_settings;
|
||||
mod view;
|
||||
|
||||
pub use self::engine::MycEngine;
|
||||
pub use self::format::HtmlTextJsonFormat;
|
||||
pub use self::format::MycFormat;
|
||||
pub use self::format::MycFormatFamily;
|
||||
|
12
src/view.rs
12
src/view.rs
@ -3,6 +3,8 @@ use axum::http::status::StatusCode;
|
||||
use axum::Json;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::response::Response;
|
||||
use axum_extra::extract::cookie::Cookie;
|
||||
use axum_extra::extract::cookie;
|
||||
|
||||
use crate::DigResult;
|
||||
use crate::IpResult;
|
||||
@ -47,6 +49,16 @@ impl MycView<QuerySettings, ResponseFormat> for View {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cookie_header(&self, settings: &QuerySettings) -> Option<String> {
|
||||
Some(
|
||||
Cookie::build("dns_resolver",settings.dns_resolver_id.to_string())
|
||||
.path("/")
|
||||
.same_site(cookie::SameSite::Strict)
|
||||
.finish()
|
||||
.to_string()
|
||||
)
|
||||
}
|
||||
|
||||
fn get_api_response(self, settings: &QuerySettings) -> Response {
|
||||
match self {
|
||||
Self::Dig{result, ..} => {
|
||||
|
Loading…
Reference in New Issue
Block a user