mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-01-14 12:37:08 +01:00
Made the query settings template settings
This commit is contained in:
parent
52ace5f61f
commit
b9fc472120
40
src/main.rs
40
src/main.rs
@ -37,8 +37,11 @@ use crate::geoip::QueryLocation;
|
||||
use geoip::AsnResult;
|
||||
use geoip::LocationResult;
|
||||
|
||||
use crate::templating_engine::View;
|
||||
use crate::templating_engine::ResponseFormat;
|
||||
use crate::templating_engine::{
|
||||
View,
|
||||
ResponseFormat,
|
||||
TemplateSettings,
|
||||
};
|
||||
|
||||
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
|
||||
|
||||
@ -48,12 +51,6 @@ pub struct BaseQuery {
|
||||
lang: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
||||
pub struct QuerySettings {
|
||||
format: ResponseFormat,
|
||||
lang: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
||||
pub struct IpQuery {
|
||||
ip: IpAddr,
|
||||
@ -251,7 +248,7 @@ async fn format_and_language_middleware<B>(
|
||||
next: Next<B>
|
||||
) -> Response {
|
||||
let format = query.format.unwrap_or(ResponseFormat::TextHtml);
|
||||
req.extensions_mut().insert(QuerySettings{
|
||||
req.extensions_mut().insert(TemplateSettings{
|
||||
format: format,
|
||||
lang: query.lang.unwrap_or("en".to_string()),
|
||||
});
|
||||
@ -261,20 +258,19 @@ async fn format_and_language_middleware<B>(
|
||||
#[axum::debug_handler]
|
||||
async fn hello_world_handler(
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
Extension(settings): Extension<TemplateSettings>,
|
||||
) -> Response {
|
||||
let state = Arc::clone(&arc_state);
|
||||
|
||||
|
||||
state.templating_engine.render_view(
|
||||
settings.format,
|
||||
&settings,
|
||||
&View::Message("Hello! There, You, Awesome Creature!".to_string())
|
||||
).await
|
||||
}
|
||||
|
||||
async fn handle_default_route(
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
Extension(settings): Extension<TemplateSettings>,
|
||||
SecureClientIp(address): SecureClientIp
|
||||
) -> Response {
|
||||
|
||||
@ -287,21 +283,21 @@ async fn handle_default_route(
|
||||
let result = get_ip_result(&ip_query, &settings.lang, &state).await;
|
||||
|
||||
state.templating_engine.render_view(
|
||||
settings.format,
|
||||
&settings,
|
||||
&View::Index{query: ip_query, result: result}
|
||||
).await
|
||||
}
|
||||
|
||||
async fn handle_ip_route(
|
||||
Query(ip_query): Query<IpQuery>,
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
Extension(settings): Extension<TemplateSettings>,
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
) -> Response {
|
||||
return handle_ip_request(ip_query, settings, arc_state).await
|
||||
}
|
||||
|
||||
async fn handle_ip_route_with_path(
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
Extension(settings): Extension<TemplateSettings>,
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
extract::Path(address): extract::Path<IpAddr>,
|
||||
) -> Response {
|
||||
@ -312,7 +308,7 @@ async fn handle_ip_route_with_path(
|
||||
|
||||
async fn handle_ip_request(
|
||||
ip_query: IpQuery,
|
||||
settings: QuerySettings,
|
||||
settings: TemplateSettings,
|
||||
arc_state: Arc<ServiceSharedState>,
|
||||
) -> Response {
|
||||
|
||||
@ -320,7 +316,7 @@ async fn handle_ip_request(
|
||||
let result = get_ip_result(&ip_query, &settings.lang, &state).await;
|
||||
|
||||
state.templating_engine.render_view(
|
||||
settings.format,
|
||||
&settings,
|
||||
&View::Ip{query: ip_query, result: result}
|
||||
).await
|
||||
}
|
||||
@ -383,14 +379,14 @@ async fn get_ip_result(
|
||||
|
||||
async fn handle_dig_route(
|
||||
Query(dig_query): Query<DigQuery>,
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
Extension(settings): Extension<TemplateSettings>,
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
) -> Response {
|
||||
return handle_dig_request(dig_query, settings, arc_state).await
|
||||
}
|
||||
|
||||
async fn handle_dig_route_with_path(
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
Extension(settings): Extension<TemplateSettings>,
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
extract::Path(name): extract::Path<String>,
|
||||
) -> Response {
|
||||
@ -401,7 +397,7 @@ async fn handle_dig_route_with_path(
|
||||
|
||||
async fn handle_dig_request(
|
||||
dig_query: DigQuery,
|
||||
settings: QuerySettings,
|
||||
settings: TemplateSettings,
|
||||
arc_state: Arc<ServiceSharedState>,
|
||||
) -> Response {
|
||||
|
||||
@ -410,7 +406,7 @@ async fn handle_dig_request(
|
||||
let dig_result = get_dig_result(&dig_query, &state).await;
|
||||
|
||||
state.templating_engine.render_view(
|
||||
settings.format,
|
||||
&settings,
|
||||
&View::Dig{ query: dig_query, result: dig_result}
|
||||
).await
|
||||
|
||||
|
@ -40,6 +40,14 @@ impl ToString for ResponseFormat {
|
||||
}
|
||||
}
|
||||
|
||||
/* Template Settings */
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
||||
pub struct TemplateSettings {
|
||||
pub format: ResponseFormat,
|
||||
pub lang: String,
|
||||
}
|
||||
|
||||
/* The echoip view */
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
||||
@ -76,17 +84,18 @@ pub struct Engine {
|
||||
impl Engine {
|
||||
pub async fn render_view(
|
||||
&self,
|
||||
format: ResponseFormat,
|
||||
settings: &TemplateSettings,
|
||||
view: &View,
|
||||
) -> Response {
|
||||
match format {
|
||||
match settings.format {
|
||||
ResponseFormat::TextHtml => {
|
||||
let template_name = view.template_name();
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("view", &template_name);
|
||||
//intented for shared macros
|
||||
context.insert("format", &format.to_string());
|
||||
context.insert("format", &settings.format.to_string());
|
||||
context.insert("language", &settings.lang);
|
||||
context.insert("data", &view);
|
||||
context.insert("extra", &self.template_config);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user