1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

update to alpha2

This commit is contained in:
Nikolay Kim
2019-03-30 09:12:42 -07:00
parent 26f7c40d5e
commit 9a80911b92
40 changed files with 106 additions and 147 deletions

View File

@ -2,7 +2,8 @@
name = "rustls-example"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
workspace = "../"
workspace = ".."
edition = "2018"
[[bin]]
name = "rustls-server"
@ -10,6 +11,6 @@ path = "src/main.rs"
[dependencies]
env_logger = "0.5"
rustls = "0.14"
actix = "0.7"
actix-web = { version = "0.7", features=["rust-tls"] }
rustls = "0.15"
actix-web = { version = "1.0.0-alpha.2", features=["rust-tls"] }
actix-files = "0.1.0-alpha.1"

View File

@ -1,32 +1,24 @@
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate rustls;
use std::fs::File;
use std::io::BufReader;
use actix_web::{http, middleware, server, App, Error, HttpRequest, HttpResponse};
use actix_files::Files;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use rustls::internal::pemfile::{certs, rsa_private_keys};
use rustls::{NoClientAuth, ServerConfig};
use server::ServerFlags;
use actix_web::fs::StaticFiles;
/// simple handle
fn index(req: &HttpRequest) -> Result<HttpResponse, Error> {
fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
println!("{:?}", req);
Ok(HttpResponse::Ok()
.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");
fn main() -> std::io::Result<()> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "actix_web=info");
}
env_logger::init();
let sys = actix::System::new("ws-example");
// load ssl keys
let mut config = ServerConfig::new(NoClientAuth::new());
@ -36,32 +28,20 @@ fn main() {
let mut keys = rsa_private_keys(key_file).unwrap();
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
// actix acceptor
let acceptor = server::RustlsAcceptor::with_flags(
config,
ServerFlags::HTTP1 | ServerFlags::HTTP2,
);
server::new(|| {
HttpServer::new(|| {
App::new()
// enable logger
.middleware(middleware::Logger::default())
.wrap(middleware::Logger::default())
// register simple handler, handle all methods
.resource("/index.html", |r| r.f(index))
.service(web::resource("/index.html").to(index))
// with path parameters
.resource("/", |r| {
r.method(http::Method::GET).f(|_| {
HttpResponse::Found()
.header("LOCATION", "/index.html")
.finish()
})
})
.handler("/static", StaticFiles::new("static").unwrap())
.service(web::resource("/").route(web::get().to(|| {
HttpResponse::Found()
.header("LOCATION", "/index.html")
.finish()
})))
.service(Files::new("/static", "static"))
})
.bind_with("127.0.0.1:8443", move || acceptor.clone())
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8443");
let _ = sys.run();
.bind_rustls("127.0.0.1:8443", config)?
.run()
}