Skip to content

Commit 669681f

Browse files
authored
docs(tls): update README (#444)
1 parent 883d261 commit 669681f

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,89 @@ async fn main() {
283283
- When the binary size matters, you should prepare a feature flag activating `ohkami/openapi` in your package, and put all your codes around `openapi` behind that feature via `#[cfg(feature = ...)]` or `#[cfg_attr(feature = ...)]`.
284284
- In `rt_worker`, `.generate` is not available because `Ohkami` can't have access to your local filesystem by `wasm32` binary on Minifalre. So ohkami provides [a CLI tool](./scripts/workers_openapi.js) to generate document from `#[ohkami::worker] Ohkami` with `openapi` feature.
285285

286+
### `"tls"`
287+
288+
HTTPS support up on [rustls](https://github.com/rustls) ecosystem.
289+
290+
- Currently, only works with `rt_tokio`.
291+
- Currently, only HTTP/1.1 over TLS is supported.
292+
- You should prepare your own certificate and private key files.
293+
294+
Example :
295+
296+
```sh
297+
$ openssl req -x509 -newkey rsa:4096 -nodes -keyout server.key -out server.crt -days 365 -subj "/CN=localhost"
298+
```
299+
300+
```toml
301+
[dependencies]
302+
ohkami = { version = "0.24", features = ["rt_tokio", "tls"] }
303+
tokio = { version = "1", features = ["full"] }
304+
rustls = { version = "0.22", features = ["ring"] }
305+
rustls-pemfile = "2.2"
306+
```
307+
308+
```rust,no_run
309+
use ohkami::prelude::*;
310+
use rustls::ServerConfig;
311+
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
312+
use std::fs::File;
313+
use std::io::BufReader;
314+
315+
async fn hello() -> &'static str {
316+
"Hello, secure ohkami!"
317+
}
318+
319+
#[tokio::main]
320+
async fn main() -> std::io::Result<()> {
321+
// Initialize rustls crypto provider
322+
rustls::crypto::ring::default_provider().install_default()
323+
.expect("Failed to install rustls crypto provider");
324+
325+
// Load certificates and private key
326+
let cert_file = File::open("server.crt")?;
327+
let key_file = File::open("server.key")?;
328+
329+
let cert_chain = rustls_pemfile::certs(&mut BufReader::new(cert_file))
330+
.map(|cd| cd.map(CertificateDer::from))
331+
.collect::<Result<Vec<_>, _>>()?;
332+
333+
let key = rustls_pemfile::read_one(&mut BufReader::new(key_file))?
334+
.map(|p| match p {
335+
rustls_pemfile::Item::Pkcs1Key(k) => PrivateKeyDer::Pkcs1(k),
336+
rustls_pemfile::Item::Pkcs8Key(k) => PrivateKeyDer::Pkcs8(k),
337+
_ => panic!("Unexpected private key type"),
338+
})
339+
.expect("Failed to read private key");
340+
341+
// Build TLS configuration
342+
let tls_config = ServerConfig::builder()
343+
.with_no_client_auth()
344+
.with_single_cert(cert_chain, key)
345+
.expect("Failed to build TLS configuration");
346+
347+
// Create and run Ohkami with HTTPS
348+
Ohkami::new((
349+
"/".GET(hello),
350+
)).howl_tls("0.0.0.0:8443", tls_config).await;
351+
352+
Ok(())
353+
}
354+
```
355+
356+
```sh
357+
$ cargo run
358+
```
359+
360+
```sh
361+
$ curl https://localhost:8443 --insecure # for self-signed certificate
362+
Hello, secure ohkami!
363+
```
364+
286365
### `"nightly"` : nightly-only functionalities
287366

288367
- try response
368+
- internal performance optimizations
289369

290370
<br>
291371

@@ -712,7 +792,7 @@ async fn main() {
712792
- [x] HTTP/1.1
713793
- [ ] HTTP/2
714794
- [ ] HTTP/3
715-
- [ ] HTTPS
795+
- [x] HTTPS
716796
- [x] Server-Sent Events
717797
- [x] WebSocket
718798

0 commit comments

Comments
 (0)