aboutsummaryrefslogtreecommitdiffstats
path: root/web/template/src
diff options
context:
space:
mode:
authormsi2025-11-14 11:59:10 -0300
committermsi2025-11-14 11:59:10 -0300
commitc806356b760c9478ac6b47e22e71917ba436bcd8 (patch)
tree10d6b42e9e4d758659b26f8c6c1afadb7f8727f1 /web/template/src
parent1cb08214cf3a82ad3acb8fec023e7fb2a404f6bd (diff)
downloadtemplates-c806356b760c9478ac6b47e22e71917ba436bcd8.tar.gz
Add ip extracter
Diffstat (limited to 'web/template/src')
-rw-r--r--web/template/src/main.rs12
-rw-r--r--web/template/src/router.rs10
-rw-r--r--web/template/src/settings.rs6
3 files changed, 21 insertions, 7 deletions
diff --git a/web/template/src/main.rs b/web/template/src/main.rs
index f9c6340..d1f56d9 100644
--- a/web/template/src/main.rs
+++ b/web/template/src/main.rs
@@ -14,6 +14,7 @@
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
+use std::net::SocketAddr;
use std::sync::Arc;
use minijinja::Environment;
@@ -23,8 +24,8 @@ use tracing::info;
mod helpers;
mod metric;
mod router;
-mod state;
mod settings;
+mod state;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
@@ -52,8 +53,11 @@ async fn start_main_server() -> anyhow::Result<()> {
// TODO(msi): from config
let listener = TcpListener::bind("0.0.0.0:3000").await?;
info!("listening on http://{}", listener.local_addr().unwrap());
- axum::serve(listener, app)
- .with_graceful_shutdown(helpers::shutdown_signal())
- .await?;
+ axum::serve(
+ listener,
+ app.into_make_service_with_connect_info::<SocketAddr>(),
+ )
+ .with_graceful_shutdown(helpers::shutdown_signal())
+ .await?;
Ok(())
}
diff --git a/web/template/src/router.rs b/web/template/src/router.rs
index 170890a..ca2f2a8 100644
--- a/web/template/src/router.rs
+++ b/web/template/src/router.rs
@@ -23,6 +23,7 @@ use axum::{
response::{Html, IntoResponse, Redirect},
routing::get,
};
+use axum_client_ip::{ClientIp, ClientIpSource};
use axum_csrf::{CsrfConfig, CsrfLayer, CsrfToken, Key};
use axum_messages::{Messages, MessagesManagerLayer};
use minijinja::context;
@@ -62,6 +63,9 @@ pub(crate) fn route(app_state: Arc<AppState>) -> Router {
.with_key(Some(cookie_key))
.with_cookie_domain(Some("127.0.0.1"));
+ // TODO(msi): from config, if debug mode
+ let ip_source = ClientIpSource::ConnectInfo;
+
Router::new()
.route("/", get(handler_home))
.route("/content", get(handler_content))
@@ -70,6 +74,7 @@ pub(crate) fn route(app_state: Arc<AppState>) -> Router {
.route("/message", get(set_messages_handler))
.route("/read-messages", get(read_messages_handler))
.route("/csrf", get(csrf_root).post(csrf_check_key))
+ .route("/ip", get(ip_handler))
.layer(MessagesManagerLayer)
// TODO(msi): from config folder asssets
.nest_service("/assets", ServeDir::new("assets"))
@@ -97,6 +102,7 @@ pub(crate) fn route(app_state: Arc<AppState>) -> Router {
.with_expiry(Expiry::OnInactivity(Duration::seconds(10))),
MessagesManagerLayer,
CsrfLayer::new(config),
+ ip_source.into_extension(),
// TODO(msi): from config
TimeoutLayer::new(std::time::Duration::from_secs(10)),
PropagateRequestIdLayer::new(x_request_id),
@@ -106,6 +112,10 @@ pub(crate) fn route(app_state: Arc<AppState>) -> Router {
.with_state(app_state)
}
+async fn ip_handler(ClientIp(ip): ClientIp) -> String {
+ ip.to_string()
+}
+
async fn csrf_root(
token: CsrfToken,
State(state): State<Arc<AppState>>,
diff --git a/web/template/src/settings.rs b/web/template/src/settings.rs
index 897cbf4..9814764 100644
--- a/web/template/src/settings.rs
+++ b/web/template/src/settings.rs
@@ -48,7 +48,8 @@ pub(crate) struct Settings {
impl Settings {
pub(crate) fn new() -> Result<Self, ConfigError> {
info!("loading settings");
- let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
+ let run_mode =
+ env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
let s = Config::builder()
// Start off by merging in the "default" configuration file
@@ -57,8 +58,7 @@ impl Settings {
// Default to 'development' env
// Note that this file is _optional_
.add_source(
- File::with_name(&format!("config/{run_mode}"))
- .required(false),
+ File::with_name(&format!("config/{run_mode}")).required(false),
)
// Add in a local configuration file
// This file shouldn't be checked in to git