mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
Updated basics/middleware-http-to-https to v4. (#487)
This commit is contained in:
parent
d25f75b09d
commit
a3726b0874
@ -6,8 +6,9 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = {version = "3", features = ["rustls"]}
|
actix-web = {version = "4.0.0-beta.21", features = ["rustls"]}
|
||||||
rustls = "0.18"
|
rustls = "0.20.2"
|
||||||
|
rustls-pemfile = "0.2.1" # these are now in an external library
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,8 +8,8 @@ use actix_web::{get, App, HttpServer};
|
|||||||
use actix_web::{http, HttpResponse};
|
use actix_web::{http, HttpResponse};
|
||||||
use futures::future;
|
use futures::future;
|
||||||
use futures::future::Either;
|
use futures::future::Either;
|
||||||
use rustls::internal::pemfile::{certs, pkcs8_private_keys};
|
use rustls::{Certificate, PrivateKey, ServerConfig};
|
||||||
use rustls::{NoClientAuth, ServerConfig};
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn index() -> String {
|
async fn index() -> String {
|
||||||
@ -20,14 +20,25 @@ async fn index() -> String {
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
let mut config = ServerConfig::new(NoClientAuth::new());
|
|
||||||
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
|
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
|
||||||
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
|
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
|
||||||
|
|
||||||
let cert_chain = certs(cert_file).unwrap();
|
let cert_chain: Vec<Certificate> = certs(cert_file)
|
||||||
let mut keys = pkcs8_private_keys(key_file).unwrap();
|
.unwrap()
|
||||||
|
.into_iter()
|
||||||
|
.map(Certificate)
|
||||||
|
.collect();
|
||||||
|
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)
|
||||||
|
.unwrap()
|
||||||
|
.into_iter()
|
||||||
|
.map(PrivateKey)
|
||||||
|
.collect();
|
||||||
|
|
||||||
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
|
let config = ServerConfig::builder()
|
||||||
|
.with_safe_defaults()
|
||||||
|
.with_no_client_auth()
|
||||||
|
.with_single_cert(cert_chain, keys.remove(0))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
@ -47,7 +58,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
println!("An http request has arrived here, i will redirect it to use https");
|
println!("An http request has arrived here, i will redirect it to use https");
|
||||||
return Either::Right(future::ready(Ok(sreq.into_response(
|
return Either::Right(future::ready(Ok(sreq.into_response(
|
||||||
HttpResponse::MovedPermanently()
|
HttpResponse::MovedPermanently()
|
||||||
.header(http::header::LOCATION, url)
|
.append_header((http::header::LOCATION, url))
|
||||||
.finish(),
|
.finish(),
|
||||||
))));
|
))));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user