Skip to content

Commit

Permalink
Use typed path for forecasts
Browse files Browse the repository at this point in the history
  • Loading branch information
kellpossible committed May 15, 2024
1 parent f304f7c commit 69a701f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ utils = { path = "./utils" }
average = "0.14.1"
axum = { version = "0.7.4", features = ["macros", "multipart"] }
headers = "0.4.0"
axum-extra = { version = "0.9.2", default-features = false, features = ["cookie"] }
axum-extra = { version = "0.9.2", default-features = false, features = ["cookie", "typed-routing"] }
base64 = { workspace = true }
bcrypt = "0.15.0"
ansi-to-html = "0.2.1"
Expand Down
11 changes: 9 additions & 2 deletions src/forecasts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::{
response::{IntoResponse, Response},
Extension, Json,
};
use axum_extra::routing::TypedPath;
use eyre::{Context, ContextCompat};
use forecast_spreadsheet::{
options::AreaDefinition, AreaId, Aspect, AspectElevation, Confidence, Distribution,
Expand All @@ -19,7 +20,7 @@ use http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
use indexmap::IndexMap;
use once_cell::sync::Lazy;
use secrecy::SecretString;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use time::{OffsetDateTime, PrimitiveDateTime};
use time_tz::{Offset, TimeZone};
use tracing::instrument;
Expand Down Expand Up @@ -139,8 +140,14 @@ fn parse_forecast_name_impl(
})
}

#[derive(Deserialize, TypedPath)]
#[typed_path("/forecasts/:file_name")]
pub struct ForecastsFilePath {
pub file_name: String,
}

pub async fn handler(
Path(file_name): Path<String>,
ForecastsFilePath { file_name }: ForecastsFilePath,
State(state): State<AppState>,
Extension(database): Extension<Database>,
Extension(i18n): Extension<I18nLoader>,
Expand Down
9 changes: 7 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use axum::{
response::{IntoResponse, Response},
Extension, Json,
};
use axum_extra::routing::TypedPath;
use color_eyre::Help;
use eyre::{bail, eyre, Context, ContextCompat};
use forecast_spreadsheet::{HazardRating, HazardRatingKind};
Expand All @@ -20,7 +21,7 @@ use crate::{
error::map_eyre_error,
forecasts::{
get_forecast_data, parse_forecast_name, Forecast, ForecastContext, ForecastData,
ForecastDetails, ForecastFileDetails, RequestedForecastData,
ForecastDetails, ForecastFileDetails, ForecastsFilePath, RequestedForecastData,
},
google_drive::{self, ListFileMetadata},
i18n::{self, I18nLoader},
Expand Down Expand Up @@ -53,7 +54,11 @@ pub struct ForecastFileContext {

impl From<ForecastFile> for ForecastFileContext {
fn from(value: ForecastFile) -> Self {
let path = format!("/forecasts/{}", urlencoding::encode(&value.file.name));
let path = ForecastsFilePath {
file_name: value.file.name,
}
.to_uri()
.to_string();
Self { path }
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use axum::{
routing::{get, post},
Extension, Router,
};
use axum_extra::routing::RouterExt;
use bytes::Bytes;
use error::map_std_error;
use eyre::Context;
Expand Down Expand Up @@ -173,7 +174,7 @@ async fn main() -> eyre::Result<()> {
Router::new()
.route("/", get(index::handler))
.route("/json", get(index::json_handler))
.route("/forecasts/:file_name", get(forecasts::handler))
.typed_get(forecasts::handler)
.nest("/observations", observations::router())
.layer(middleware::from_fn(disclaimer::middleware)),
)
Expand Down

0 comments on commit 69a701f

Please sign in to comment.