Skip to content

Commit

Permalink
Redirect unhandled requests to https
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Sep 9, 2023
1 parent cde80e9 commit 3c8f4c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
16 changes: 8 additions & 8 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
@@ -1,6 +1,6 @@
[package]
name = "httpserve"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use actix_web::{App, HttpServer, guard, web};
use actix_web::{App, HttpServer, guard, web, HttpRequest, Responder};
use actix_web::web::Redirect;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
Expand All @@ -25,4 +26,19 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
);
}
}

if let Ok(allow_redirect) = env::var("REDIRECT_TO_HTTPS") {
if allow_redirect.ne("") {
cfg.default_service(web::route().to(https_redirect_handler));
}
}
}

async fn https_redirect_handler(request: HttpRequest) -> impl Responder {
let target_url = format!(
"https://{}{}",
request.connection_info().host(),
request.uri().path()
);
Redirect::to(target_url).permanent()
}

0 comments on commit 3c8f4c3

Please sign in to comment.