2022-02-18 03:18:44 +01:00
|
|
|
use std::{fs::File, io::BufReader};
|
2021-10-13 16:06:42 +02:00
|
|
|
|
2022-02-18 03:18:44 +01:00
|
|
|
use actix_web::{dev::Service, get, http, App, HttpResponse, HttpServer};
|
2022-02-22 13:12:17 +01:00
|
|
|
use futures_util::future::{self, Either, FutureExt};
|
2022-01-29 18:43:00 +01:00
|
|
|
use rustls::{Certificate, PrivateKey, ServerConfig};
|
|
|
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
2021-10-13 16:06:42 +02:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
async fn index() -> String {
|
2022-02-18 03:44:02 +01:00
|
|
|
String::from("<html><head><title>FOO BAR</title></head><body><h1>FOO BAR</h1></body></html>")
|
2021-10-13 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2022-02-22 13:12:17 +01:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
2021-10-13 16:06:42 +02:00
|
|
|
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
|
|
|
|
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
|
|
|
|
|
2022-01-29 18:43:00 +01:00
|
|
|
let cert_chain: Vec<Certificate> = certs(cert_file)
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(Certificate)
|
|
|
|
.collect();
|
|
|
|
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(PrivateKey)
|
|
|
|
.collect();
|
2021-10-13 16:06:42 +02:00
|
|
|
|
2022-01-29 18:43:00 +01:00
|
|
|
let config = ServerConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(cert_chain, keys.remove(0))
|
|
|
|
.unwrap();
|
2021-10-13 16:06:42 +02:00
|
|
|
|
2022-02-22 13:12:17 +01:00
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
|
|
|
|
2021-10-13 16:06:42 +02:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.wrap_fn(|sreq, srv| {
|
|
|
|
let host = sreq.connection_info().host().to_owned();
|
|
|
|
let uri = sreq.uri().to_owned();
|
|
|
|
let url = format!("https://{}{}", host, uri);
|
|
|
|
|
|
|
|
// If the scheme is "https" then it will let other services below this wrap_fn
|
|
|
|
// handle the request and if it's "http" then a response with redirect status code
|
|
|
|
// will be sent whose "location" header will be same as before, with just "http"
|
|
|
|
// changed to "https"
|
|
|
|
//
|
|
|
|
if sreq.connection_info().scheme() == "https" {
|
|
|
|
Either::Left(srv.call(sreq).map(|res| res))
|
|
|
|
} else {
|
|
|
|
println!("An http request has arrived here, i will redirect it to use https");
|
|
|
|
return Either::Right(future::ready(Ok(sreq.into_response(
|
|
|
|
HttpResponse::MovedPermanently()
|
2022-01-29 18:43:00 +01:00
|
|
|
.append_header((http::header::LOCATION, url))
|
2021-10-13 16:06:42 +02:00
|
|
|
.finish(),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.service(index)
|
|
|
|
})
|
2022-02-18 03:18:44 +01:00
|
|
|
.bind("127.0.0.1:80")? // Port 80 to listen for HTTP request
|
|
|
|
.bind_rustls("127.0.0.1:443", config)? // Port 443 to listen for HTTPS request
|
2021-10-13 16:06:42 +02:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|