key_fingerprint: Option<String>,
key_description: Option<String>,
error: Option<String>,
+ post_target: Option<String>,
}
-pub async fn show_check_form() -> impl IntoResponse {
+pub async fn show_check_form(State(state): State<AppState>) -> impl IntoResponse {
let signed_doc_url = "https://socialistra.org/canary/dl/27/matt.asc";
Html(ShowCheckFormTemplate{
key_fingerprint: None,
key_description: None,
error: None,
+ post_target: Some(state.root_path),
}.render().unwrap())
}
error: Some(error),
key_fingerprint: None,
key_description: None,
+ post_target: None,
}.render().unwrap()
))
}
pub async fn check_canary_result(
- State(state): State<AppState>,
+ state: State<AppState>,
Form(form_data): Form<CheckCanaryFormData>,
) -> CheckResponse {
match ureq::get(uri.clone()).call() {
Ok(mut resp) =>
match resp.body_mut().read_to_string() {
- Ok(signed_message) => do_verify(signed_message, state.certs, uri).await,
+ Ok(signed_message) => do_verify(signed_message,state.certs.clone(), uri,state).await,
Err(e) =>
error_response(StatusCode::BAD_REQUEST,
format!("Error decoding signed message to string: {}",e))
}
-fn read_verifier(mut verifier: Verifier<'_,Helper>, url: String) -> CheckResponse {
+fn read_verifier(mut verifier: Verifier<'_,Helper>, url: String, state: State<AppState>) -> CheckResponse {
let mut content = Vec::new();
match verifier.read_to_end(&mut content) {
- Ok(_) => decode_message_bytes(content,verifier.into_helper(),url),
+ Ok(_) => decode_message_bytes(content,verifier.into_helper(),url, state),
_ => error_response(StatusCode::BAD_REQUEST,
"Unable to read signed doc".to_string())
}
}
-fn decode_message_bytes(content: Vec<u8>, helper: Helper, url: String) -> CheckResponse {
+fn decode_message_bytes(content: Vec<u8>, helper: Helper, url: String, State(state): State<AppState>) -> CheckResponse {
match String::from_utf8(content) {
Ok(content) => (StatusCode::OK,Html(ShowCheckFormTemplate{
url: Some(url),
error: None,
key_fingerprint: helper.get_fingerprint(),
key_description: helper.get_description(),
+ post_target: Some(state.root_path),
}.render().unwrap())),
_ => error_response(StatusCode::BAD_REQUEST,
"Unable to decode signed doc into UTF-8".to_string())
}
}
-async fn do_verify(signed_message: String, certs: CertMap, url: String) -> CheckResponse {
+async fn do_verify(signed_message: String, certs: CertMap, url: String, state: State<AppState>) -> CheckResponse {
let policy = &StandardPolicy::new();
let verbuilder =
verbuilder.with_policy(policy,None, Helper::new(certs));
match verifier_result {
- Ok(verifier) => read_verifier(verifier,url),
+ Ok(verifier) => read_verifier(verifier,url,state),
Err(err) => error_response(StatusCode::BAD_REQUEST,
err.to_string())
}
#[derive(Clone)]
struct AppState {
certs: CertMap,
+ root_path: String,
}
pub type CertMap = HashMap<String,(String,String)>;
#[tokio::main]
async fn main() -> Result<(),Box<dyn std::error::Error>> {
- // Set config from .env, ignoring any error (file may not be there)
- let _ = dotenvy::dotenv();
- // ... then override with defaults
+ // set defaults
unsafe {
env::set_var("BIND_HOST","127.0.0.1");
env::set_var("BIND_PORT","3000");
+ env::set_var("ROOT_PATH", "/");
}
+ // ... then override from .env, ignoring any error (file may not be there)
+ let _ = dotenvy::dotenv_override();
let certs = load_certs_from_fs().await.unwrap();
+ let root_path = env::var("ROOT_PATH")?;
let app_state = AppState{
certs,
+ root_path: root_path.clone(),
};
let all_routes = Router::new()
- .route("/", get(handler::show_check_form))
- .route("/", post(handler::check_canary_result))
+ .route(&root_path, get(handler::show_check_form))
+ .route(&root_path, post(handler::check_canary_result))
.with_state(app_state);
let addr = SocketAddr::new(