nickel-org / nickel.rs

An expressjs inspired web framework for Rust

Home Page:http://nickel-org.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat(https): add API for HTTPS

arden opened this issue · comments

commented

there have any plans for https support?
because ios9 only support the https.

There is a closed pull request that could be reopened and improved to support https.

I put this on the list for our 1.0 milestone.

commented

now i'm modify the server.rs
impl Server {
pub fn new(middleware_stack: MiddlewareStack) -> Server {
Server {
middleware_stack: middleware_stack,
templates: RwLock::new(HashMap::new())
}
}

pub fn serve<T: ToSocketAddrs>(self, addr: T) -> HttpResult<Listening> {
    let arc = ArcServer(Arc::new(self));
    let server = try!(HyperServer::http(addr));
    server.handle(arc)
}

pub fn serve_https<T: ToSocketAddrs>(self, addr: T, cert: &Path, key: &Path) -> HttpResult<Listening> {
    let arc = ArcServer(Arc::new(self));
    let ssl = Openssl::with_cert_and_key(cert, key).unwrap();
    let server = try!(HyperServer::https(addr, ssl));
    server.handle(arc)
}

pub fn serve_with_threads<T: ToSocketAddrs>(self, addr: T, n: usize) -> HttpResult<Listening> {
    let arc = ArcServer(Arc::new(self));
    let server = try!(HyperServer::http(addr));
    server.handle_threads(arc, n)
}

pub fn serve_https_with_threads<T: ToSocketAddrs>(self, addr: T, cert: &Path, key: &Path, n: usize) -> HttpResult<Listening>  {
    let arc = ArcServer(Arc::new(self));
    let ssl = Openssl::with_cert_and_key(cert, key).unwrap();
    let server = try!(HyperServer::https(addr, ssl));
    server.handle_threads(arc, n)
}

}

and modify the nickel.rs
pub fn listen<T: ToSocketAddrs>(mut self, addr: T) {
self.middleware_stack.add_middleware(middleware! {
(StatusCode::NotFound, "File Not Found")
});

    let server = Server::new(self.middleware_stack);
    let listener = server.serve(addr).unwrap();

    println!("Listening on http://{}", listener.socket);
    println!("Ctrl-C to shutdown server");
}

pub fn listen_https<T: ToSocketAddrs + Display>(mut self, addr: T, cert: &Path, key: &Path) {
    self.middleware_stack.add_middleware(middleware! {
        (StatusCode::NotFound, "File Not Found")
    });

    let server = Server::new(self.middleware_stack);
    let listener = server.serve_https(addr, cert, key).unwrap();

    println!("Listening on https://{}", listener.socket);
    println!("Ctrl-C to shutdown server");

}

pub fn listen_with_threads<T: ToSocketAddrs>(mut self, addr: T, n: usize) {
    self.middleware_stack.add_middleware(middleware! {
        (StatusCode::NotFound, "File Not Found")
    });

    let server = Server::new(self.middleware_stack);
    let listener = server.serve_with_threads(addr, n).unwrap();

    println!("Listening on http://{}", listener.socket);
    println!("Ctrl-C to shutdown server");
}

pub fn listen_https_with_threads<T: ToSocketAddrs + Display>(mut self, addr: T, cert: &Path, key: &Path, n: usize) {
    self.middleware_stack.add_middleware(middleware! {
        (StatusCode::NotFound, "File Not Found")
    });

    let server = Server::new(self.middleware_stack);
    let listener = server.serve_https_with_threads(addr, cert, key, n).unwrap();

    println!("Listening on https://{}", listener.socket);
    println!("Ctrl-C to shutdown server");

}

for support https.

commented

the 1.0 when released?

the 1.0 when released?

I'd say as soon as all features are implemented end tested :)

I'd say as soon as all features are implemented end tested

exactly

@arden

How about you create a PR and we guide you with feedback until we can land the feature?

Has there been much progress made on this?

#316 is a PR for this if you want to add feedback

Until HTTPS support is complete, you can use a reverse proxy such as nginx with nickel.rs. This allows you to use your HTTPS certificates, use caching and also supports load balancing.

Here's the nginx guide on reverse proxying. https://www.nginx.com/resources/admin-guide/reverse-proxy/

HTTPS support was added to release 0.8 🎆