diff --git a/.gitignore b/.gitignore index ea8c4bf..21ef6af 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +mi.db diff --git a/Cargo.lock b/Cargo.lock index 90e172e..37def05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" + [[package]] name = "base64" version = "0.12.3" @@ -211,6 +217,7 @@ checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" dependencies = [ "num-integer", "num-traits", + "serde", "time", ] @@ -326,6 +333,28 @@ dependencies = [ "syn", ] +[[package]] +name = "diesel" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c" +dependencies = [ + "byteorder", + "diesel_derives", + "libsqlite3-sys", +] + +[[package]] +name = "diesel_derives" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "digest" version = "0.8.1" @@ -404,6 +433,28 @@ dependencies = [ "once_cell", ] +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "fake-simd" version = "0.1.2" @@ -593,7 +644,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed18eb2459bf1a09ad2d6b1547840c3e5e62882fa09b9a6a20b1de8e3228848f" dependencies = [ - "base64", + "base64 0.12.3", "bitflags", "bytes", "headers-core", @@ -851,6 +902,27 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3286f09f7d4926fc486334f28d8d2e6ebe4f7f9994494b6dab27ddfad2c9b11b" +[[package]] +name = "libsodium-sys" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a685b64f837b339074115f2e7f7b431ac73681d08d75b389db7498b8892b8a58" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + +[[package]] +name = "libsqlite3-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e704a02bcaecd4a08b93a23f6be59d0bd79cd161e0963e9499165a0a35df7bd" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "lock_api" version = "0.4.1" @@ -895,19 +967,22 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" [[package]] name = "mi" -version = "0.1.0" +version = "1.0.0" dependencies = [ "chrono", "color-eyre", + "diesel", "hyper", "jsonfeed", "kankyo", "log", "mime", + "paseto", "pretty_env_logger", "prometheus", "rand 0.7.3", "ructe", + "rusty_ulid", "serde", "serde_dhall", "thiserror", @@ -1147,6 +1222,22 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "paseto" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c494d81e41d5a5c8168756a1b2ace83d4af61563dd54eb394f1cf8a8775d5ca" +dependencies = [ + "base64 0.11.0", + "chrono", + "failure", + "failure_derive", + "openssl", + "ring", + "serde_json", + "sodiumoxide", +] + [[package]] name = "percent-encoding" version = "2.1.0" @@ -1538,7 +1629,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9eaa17ac5d7b838b7503d118fa16ad88f440498bf9ffe5424e621f93190d61e" dependencies = [ - "base64", + "base64 0.12.3", "bytes", "encoding_rs", "futures-core", @@ -1567,6 +1658,21 @@ dependencies = [ "winreg", ] +[[package]] +name = "ring" +version = "0.16.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "952cd6b98c85bbc30efa1ba5783b8abf12fec8b3287ffa52605b9432313e34e4" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi 0.3.9", +] + [[package]] name = "rle-decode-fast" version = "1.0.1" @@ -1579,7 +1685,7 @@ version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b8ffc645837eab09b6cbb6772d18897c6309753254592e149b40ff7fb113c01" dependencies = [ - "base64", + "base64 0.12.3", "bytecount", "itertools", "md5", @@ -1593,6 +1699,18 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" +[[package]] +name = "rusty_ulid" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a116be85b3a60059fd7bd4addc463406a8be6a73513816172926c713775dfc8b" +dependencies = [ + "chrono", + "doc-comment", + "rand 0.7.3", + "serde", +] + [[package]] name = "ryu" version = "1.0.5" @@ -1797,6 +1915,23 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "sodiumoxide" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7038b67c941e23501573cb7242ffb08709abe9b11eb74bceff875bbda024a6a8" +dependencies = [ + "libc", + "libsodium-sys", + "serde", +] + +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "static_assertions" version = "1.1.0" @@ -1814,6 +1949,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -2031,7 +2178,7 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23" dependencies = [ - "base64", + "base64 0.12.3", "byteorder", "bytes", "http", @@ -2104,6 +2251,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "url" version = "2.1.1" diff --git a/Cargo.toml b/Cargo.toml index 98b043c..caedacc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,21 +1,25 @@ [package] name = "mi" -version = "0.1.0" +version = "1.0.0" authors = ["Christine Dodrill "] edition = "2018" +build = "src/build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] chrono = "0.4" -kankyo = "0.3" color-eyre = "0.5.2" -log = "0.4" +diesel = { version = "1.4.4", features = ["sqlite"] } hyper = "0.13" +kankyo = "0.3" +log = "0.4" mime = "0.3.0" +paseto = "1.0.7" pretty_env_logger = "0" prometheus = { version = "0.10", default-features = false, features = ["process"] } rand = "0" +rusty_ulid = "0.10.0" serde_dhall = "0.6.0" serde = { version = "1", features = ["derive"] } thiserror = "1" diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/2020-09-13-154110_switches/down.sql b/migrations/2020-09-13-154110_switches/down.sql new file mode 100644 index 0000000..1f32a3f --- /dev/null +++ b/migrations/2020-09-13-154110_switches/down.sql @@ -0,0 +1,2 @@ +DROP TABLE members; +DROP TABLE switches; diff --git a/migrations/2020-09-13-154110_switches/up.sql b/migrations/2020-09-13-154110_switches/up.sql new file mode 100644 index 0000000..c5121f3 --- /dev/null +++ b/migrations/2020-09-13-154110_switches/up.sql @@ -0,0 +1,17 @@ +PRAGMA foreign_keys = ON; + +CREATE TABLE IF NOT EXISTS members + ( id INT PRIMARY KEY + , cmene TEXT UNIQUE NOT NULL + , picurl TEXT UNIQUE NOT NULL + ); + +CREATE TABLE IF NOT EXISTS switches + ( id TEXT UNIQUE NOT NULL PRIMARY KEY + , who TEXT NOT NULL + , started_at TEXT NOT NULL + , ended_at TEXT + , duration TEXT NOT NULL + , FOREIGN KEY (who) + REFERENCES members(cmene) + ); diff --git a/migrations/2020-09-13-155542_system/down.sql b/migrations/2020-09-13-155542_system/down.sql new file mode 100644 index 0000000..d037d61 --- /dev/null +++ b/migrations/2020-09-13-155542_system/down.sql @@ -0,0 +1 @@ +DELETE FROM members; diff --git a/migrations/2020-09-13-155542_system/up.sql b/migrations/2020-09-13-155542_system/up.sql new file mode 100644 index 0000000..b68d2ff --- /dev/null +++ b/migrations/2020-09-13-155542_system/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here +INSERT INTO + members(id, cmene, picurl) +VALUES + (0, 'Cadey', 'https://mi.within.website/static/img/cadey.png'), + (1, 'Nicole', 'https://mi.within.website/static/img/nicole.png'), + (2, 'Jessie', 'https://mi.within.website/static/img/jessie.png'), + (3, 'Ashe', 'https://mi.within.website/static/img/ashe.png'), + (4, 'Sephie', 'https://mi.within.website/static/img/sephie.png'), + (5, 'Mai', 'https://mi.within.website/static/img/mai.png'); diff --git a/shell.nix b/shell.nix index bf62560..360e893 100644 --- a/shell.nix +++ b/shell.nix @@ -12,8 +12,13 @@ in pkgs.mkShell { cmake zlib libgit2 + diesel-cli + sqlite # keep this line if you use bash bashInteractive ]; + + RUST_LOG="info"; + DATABASE_URL="./mi.db"; } diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..e7a9467 --- /dev/null +++ b/src/build.rs @@ -0,0 +1,12 @@ +use ructe::{Result, Ructe}; +use std::process::Command; + +fn main() -> Result<()> { + Ructe::from_env()?.compile_templates("templates")?; + + let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().unwrap(); + let git_hash = String::from_utf8(output.stdout).unwrap(); + println!("cargo:rustc-env=GITHUB_SHA={}", git_hash); + Ok(()) +} + diff --git a/src/main.rs b/src/main.rs index 0cb3b21..fb1e329 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,18 @@ +#[macro_use] +extern crate diesel; + use color_eyre::eyre::Result; +use diesel::prelude::*; +use diesel::sqlite::SqliteConnection; use hyper::{header::CONTENT_TYPE, Body, Response}; use prometheus::{Encoder, TextEncoder}; use warp::Filter; const APPLICATION_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); +include!(concat!(env!("OUT_DIR"), "/templates.rs")); + +pub mod schema; +pub mod models; #[tokio::main] async fn main() -> Result<()> { @@ -11,6 +20,13 @@ async fn main() -> Result<()> { pretty_env_logger::init(); color_eyre::install()?; + log::info!("starting up {} commit {}", APPLICATION_NAME, env!("GITHUB_SHA")); + + let database_url = std::env::var("DATABASE_URL") + .expect("DATABASE_URL must be set"); + SqliteConnection::establish(&database_url) + .expect(&format!("Error connecting to {}", database_url)); + let healthcheck = warp::get().and(warp::path(".within").and(warp::path("health")).map(|| "OK")); let metrics_endpoint = warp::path("metrics").and(warp::path::end()).map(move || { let encoder = TextEncoder::new(); diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..12445fc --- /dev/null +++ b/src/models.rs @@ -0,0 +1,15 @@ +#[derive(Queryable)] +pub struct Member { + pub id: i32, + pub cmene: String, + pub picurl: String, +} + +#[derive(Queryable)] +pub struct Switch { + pub id: String, + pub who: String, + pub started_at: String, + pub ended_at: Option, + pub duration: String, +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..e0857eb --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,22 @@ +table! { + members (id) { + id -> Nullable, + cmene -> Text, + picurl -> Text, + } +} + +table! { + switches (id) { + id -> Text, + who -> Text, + started_at -> Text, + ended_at -> Nullable, + duration -> Text, + } +} + +allow_tables_to_appear_in_same_query!( + members, + switches, +); diff --git a/templates/foo.rs.html b/templates/foo.rs.html new file mode 100644 index 0000000..bb21615 --- /dev/null +++ b/templates/foo.rs.html @@ -0,0 +1 @@ +@()