2019-03-06 23:16:56 -08:00
|
|
|
use std::io;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-03-06 23:16:56 -08:00
|
|
|
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
2018-05-08 11:08:43 -07:00
|
|
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
/// simple handle
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("{:?}", req);
|
|
|
|
Ok(HttpResponse::Ok()
|
2018-05-08 11:08:43 -07:00
|
|
|
.content_type("text/plain")
|
|
|
|
.body("Welcome!"))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> io::Result<()> {
|
2019-03-06 23:16:56 -08:00
|
|
|
std::env::set_var("RUST_LOG", "actix_web=debug");
|
2018-04-13 09:18:42 +08:00
|
|
|
env_logger::init();
|
2019-03-06 23:16:56 -08:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
println!("Started http server: 127.0.0.1:8443");
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
// load ssl keys
|
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
2018-05-08 11:08:43 -07:00
|
|
|
builder
|
|
|
|
.set_private_key_file("key.pem", SslFiletype::PEM)
|
|
|
|
.unwrap();
|
2018-05-20 21:03:29 -07:00
|
|
|
builder.set_certificate_chain_file("cert.pem").unwrap();
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-03-06 23:16:56 -08:00
|
|
|
HttpServer::new(|| {
|
2018-05-08 11:08:43 -07:00
|
|
|
App::new()
|
2018-04-13 09:18:42 +08:00
|
|
|
// enable logger
|
2019-03-26 04:29:00 +01:00
|
|
|
.wrap(middleware::Logger::default())
|
2018-04-13 09:18:42 +08:00
|
|
|
// register simple handler, handle all methods
|
2019-03-06 23:16:56 -08:00
|
|
|
.service(web::resource("/index.html").to(index))
|
2018-04-13 09:18:42 +08:00
|
|
|
// with path parameters
|
2019-03-06 23:16:56 -08:00
|
|
|
.service(web::resource("/").route(web::get().to(|| {
|
2018-04-13 09:18:42 +08:00
|
|
|
HttpResponse::Found()
|
|
|
|
.header("LOCATION", "/index.html")
|
|
|
|
.finish()
|
2019-03-06 23:16:56 -08:00
|
|
|
})))
|
|
|
|
})
|
2019-12-07 23:59:24 +06:00
|
|
|
.bind_openssl("127.0.0.1:8443", builder)?
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|