mirror of
https://github.com/fafhrd91/actix-web
synced 2024-12-18 18:03:11 +01:00
51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
#![allow(unused_variables)]
|
|
extern crate actix;
|
|
extern crate actix_web;
|
|
extern crate env_logger;
|
|
extern crate openssl;
|
|
|
|
use actix_web::*;
|
|
use openssl::ssl::{SslMethod, SslAcceptor, SslFiletype};
|
|
|
|
|
|
/// simple handle
|
|
fn index(req: HttpRequest) -> Result<HttpResponse> {
|
|
println!("{:?}", req);
|
|
Ok(httpcodes::HTTPOk
|
|
.build()
|
|
.content_type("text/plain")
|
|
.body("Welcome!")?)
|
|
}
|
|
|
|
fn main() {
|
|
if ::std::env::var("RUST_LOG").is_err() {
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
|
}
|
|
let _ = env_logger::init();
|
|
let sys = actix::System::new("ws-example");
|
|
|
|
// load ssl keys
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
|
builder.set_private_key_file("key.pem", SslFiletype::PEM).unwrap();
|
|
builder.set_certificate_chain_file("cert.pem").unwrap();
|
|
|
|
let addr = HttpServer::new(
|
|
|| Application::new()
|
|
// enable logger
|
|
.middleware(middleware::Logger::default())
|
|
// register simple handler, handle all methods
|
|
.resource("/index.html", |r| r.f(index))
|
|
// with path parameters
|
|
.resource("/", |r| r.method(Method::GET).f(|req| {
|
|
httpcodes::HTTPFound
|
|
.build()
|
|
.header("LOCATION", "/index.html")
|
|
.body(Body::Empty)
|
|
})))
|
|
.bind("127.0.0.1:8443").unwrap()
|
|
.start_ssl(builder).unwrap();
|
|
|
|
println!("Started http server: 127.0.0.1:8443");
|
|
let _ = sys.run();
|
|
}
|