2018-08-07 22:34:16 -07:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufReader;
|
|
|
|
|
2019-03-30 09:12:42 -07:00
|
|
|
use actix_files::Files;
|
2019-12-07 23:59:24 +06:00
|
|
|
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
|
2022-01-30 16:22:16 +00:00
|
|
|
use rustls::{Certificate, PrivateKey, ServerConfig};
|
|
|
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
2018-08-07 22:34:16 -07:00
|
|
|
|
|
|
|
/// simple handle
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn index(req: HttpRequest) -> HttpResponse {
|
2018-08-07 22:34:16 -07:00
|
|
|
println!("{:?}", req);
|
2019-12-07 23:59:24 +06:00
|
|
|
HttpResponse::Ok()
|
2021-10-06 22:42:44 +01:00
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body("<!DOCTYPE html><html><body><p>Welcome!</p></body></html>")
|
2018-08-07 22:34:16 -07:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-03-30 09:12:42 -07:00
|
|
|
if std::env::var("RUST_LOG").is_err() {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-08-07 22:34:16 -07:00
|
|
|
}
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
// load ssl keys
|
2022-01-30 16:22:16 +00:00
|
|
|
let config = ServerConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth();
|
2018-08-07 22:34:16 -07: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-30 16:22:16 +00:00
|
|
|
let cert_chain = 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-06 22:06:25 -04:00
|
|
|
if keys.is_empty() {
|
|
|
|
eprintln!("Could not locate PKCS 8 private keys.");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2022-01-30 16:22:16 +00:00
|
|
|
let config = config.with_single_cert(cert_chain, keys.remove(0)).unwrap();
|
2018-08-07 22:34:16 -07:00
|
|
|
|
2021-10-06 22:06:25 -04:00
|
|
|
println!("Starting https server: 127.0.0.1:8443");
|
2019-03-30 09:12:42 -07:00
|
|
|
HttpServer::new(|| {
|
2018-08-07 22:34:16 -07:00
|
|
|
App::new()
|
|
|
|
// enable logger
|
2019-03-30 09:12:42 -07:00
|
|
|
.wrap(middleware::Logger::default())
|
2018-08-07 22:34:16 -07:00
|
|
|
// register simple handler, handle all methods
|
2019-03-30 09:12:42 -07:00
|
|
|
.service(web::resource("/index.html").to(index))
|
2018-08-07 22:34:16 -07:00
|
|
|
// with path parameters
|
2022-02-02 00:44:58 +00:00
|
|
|
.service(web::resource("/").route(web::get().to(|| async {
|
2019-03-30 09:12:42 -07:00
|
|
|
HttpResponse::Found()
|
2022-01-30 16:22:16 +00:00
|
|
|
.append_header(("LOCATION", "/index.html"))
|
2019-03-30 09:12:42 -07:00
|
|
|
.finish()
|
|
|
|
})))
|
|
|
|
.service(Files::new("/static", "static"))
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
2019-03-30 09:12:42 -07:00
|
|
|
.bind_rustls("127.0.0.1:8443", config)?
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
2019-03-09 18:03:09 -08:00
|
|
|
}
|