Compare commits

...

2 Commits

Author SHA1 Message Date
la Fleur 22bbf941a6 tera templates support 2020-11-04 01:10:20 +01:00
la Fleur 684ed3acb0 tls/SSL support 2020-11-03 23:58:03 +01:00
9 changed files with 846 additions and 26 deletions

753
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,19 @@
[package]
name = "basic-rocket"
version = "0.1.0"
version = "0.1.1"
#workspace = "../Rocket"
authors = ["lafleur <lafleur@boum.org>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.5"
rocket = {version="0.4", features=["tls"]}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["tera_templates"]

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# Fly me a rocket
This is a basic rocket web app. It uses tls to serve content, signing itself
with keys that are in `private`, along with a generation script. A tryout use
case would bring :
( cd private && bash gen_cert.sh )
cargo run
One should use Let'sEncrypt keys for a specific domain. See `Rocket.toml` for
adding specific keys for production binaries. Self-generated keys are useful for
developement environments.

20
Rocket.toml Normal file
View File

@ -0,0 +1,20 @@
# Global configuration of TLS:
[global.tls]
certs = "private/cert.pem"
key = "private/key.pem"
# Per Environment Configuration of TLS:
#[development]
#tls = { certs = "c:\\code\\lang\\rust\\proj\\rocket-auth-login\\examples\\tls_example\\private\\certs.pem", key = "c:\\code\\lang\\rust\\proj\\rocket-auth-login\\examples\\tls_example\\private\\key.pem" }
#[production]
#tls = { certs = "c:\\code\\lang\\rust\\proj\\rocket-auth-login\\examples\\tls_example\\private\\certs.pem", key = "c:\\code\\lang\\rust\\proj\\rocket-auth-login\\examples\\tls_example\\private\\key.pem" }
# Or relative paths:
# Per Environment Configuration of TLS:
# [development]
# tls = { certs = "private\\certs.pem", key = "private\\key.pem" }
# [production]
# tls = { certs = "private\\certs.pem", key = "private\\key.pem" }

21
private/gen_cert.sh Normal file
View File

@ -0,0 +1,21 @@
#! /bin/bash
# TODO: `rustls` (really, `webpki`) doesn't currently use the CN in the subject
# to check if a certificate is valid for a server name sent via SNI. It's not
# clear if this is intended, since certificates _should_ have a `subjectAltName`
# with a DNS name, or if it simply hasn't been implemented yet. See
# https://bugzilla.mozilla.org/show_bug.cgi?id=552346 for a bit more info.
CA_SUBJECT="/C=US/ST=CA/O=Rocket CA/CN=Rocket Root CA"
SUBJECT="/C=US/ST=CA/O=Rocket/CN=localhost"
ALT="DNS:localhost"
openssl genrsa -out ca_key.pem 4096
openssl req -new -x509 -days 3650 -key ca_key.pem -subj "${CA_SUBJECT}" -out ca_cert.pem
openssl req -newkey rsa:4096 -nodes -sha256 -keyout key.pem -subj "${SUBJECT}" -out server.csr
openssl x509 -req -sha256 -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
-in server.csr -out cert.pem
rm ca_cert.srl server.csr

View File

@ -2,12 +2,26 @@
#[macro_use] extern crate rocket;
use rocket_contrib::templates::Template;
#[derive(serde::Serialize)]
struct Context {
name: String,
items: Vec<&'static str>
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
fn index() -> Template {
let c = String::from("home");
let context = Context { name: c, items: vec!["One", "Two", "Three"] };
Template::render("index", &context)
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
rocket::ignite()
.attach(Template::fairing())
.mount("/", routes![index]).launch();
}

10
templates/base.html.tera Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>basic Rocket</title>
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>404</title>
</head>
<body>
<h1>404: Hey! There's nothing here.</h1>
The page at {{ path }} does not exist!
</body>
</html>

12
templates/index.html.tera Normal file
View File

@ -0,0 +1,12 @@
{% extends "base" %}
{% block content %}
<h1>Here is {{name}}</h1>
<h3>Here are your items:</h3>
<ul>
{% for s in items %}
<li>{{ s }}</li>
{% endfor %}
</ul>
{% endblock content %}