1
0
mirror of https://github.com/actix/examples synced 2024-12-18 08:23:10 +01:00
examples/rustls/src/main.rs

48 lines
1.6 KiB
Rust
Raw Normal View History

2018-08-08 07:34:16 +02:00
use std::fs::File;
use std::io::BufReader;
2019-03-30 17:12:42 +01:00
use actix_files::Files;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
2018-08-08 07:34:16 +02:00
use rustls::internal::pemfile::{certs, rsa_private_keys};
use rustls::{NoClientAuth, ServerConfig};
/// simple handle
2019-03-30 17:12:42 +01:00
fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
2018-08-08 07:34:16 +02:00
println!("{:?}", req);
Ok(HttpResponse::Ok()
.content_type("text/plain")
.body("Welcome!"))
}
2019-03-30 17:12:42 +01:00
fn main() -> std::io::Result<()> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "actix_web=info");
2018-08-08 07:34:16 +02:00
}
env_logger::init();
// load ssl keys
let mut config = ServerConfig::new(NoClientAuth::new());
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
let cert_chain = certs(cert_file).unwrap();
let mut keys = rsa_private_keys(key_file).unwrap();
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
2019-03-30 17:12:42 +01:00
HttpServer::new(|| {
2018-08-08 07:34:16 +02:00
App::new()
// enable logger
2019-03-30 17:12:42 +01:00
.wrap(middleware::Logger::default())
2018-08-08 07:34:16 +02:00
// register simple handler, handle all methods
2019-03-30 17:12:42 +01:00
.service(web::resource("/index.html").to(index))
2018-08-08 07:34:16 +02:00
// with path parameters
2019-03-30 17:12:42 +01:00
.service(web::resource("/").route(web::get().to(|| {
HttpResponse::Found()
.header("LOCATION", "/index.html")
.finish()
})))
.service(Files::new("/static", "static"))
2019-03-10 03:03:09 +01:00
})
2019-03-30 17:12:42 +01:00
.bind_rustls("127.0.0.1:8443", config)?
.run()
2019-03-10 03:03:09 +01:00
}