diesel setup

This commit is contained in:
Cadey Ratio 2020-09-13 12:07:06 -04:00
parent 456d0201a0
commit 9b76fdb03b
15 changed files with 272 additions and 8 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target /target
mi.db

163
Cargo.lock generated
View File

@ -109,6 +109,12 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base64"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.12.3" version = "0.12.3"
@ -211,6 +217,7 @@ checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [ dependencies = [
"num-integer", "num-integer",
"num-traits", "num-traits",
"serde",
"time", "time",
] ]
@ -326,6 +333,28 @@ dependencies = [
"syn", "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]] [[package]]
name = "digest" name = "digest"
version = "0.8.1" version = "0.8.1"
@ -404,6 +433,28 @@ dependencies = [
"once_cell", "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]] [[package]]
name = "fake-simd" name = "fake-simd"
version = "0.1.2" version = "0.1.2"
@ -593,7 +644,7 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed18eb2459bf1a09ad2d6b1547840c3e5e62882fa09b9a6a20b1de8e3228848f" checksum = "ed18eb2459bf1a09ad2d6b1547840c3e5e62882fa09b9a6a20b1de8e3228848f"
dependencies = [ dependencies = [
"base64", "base64 0.12.3",
"bitflags", "bitflags",
"bytes", "bytes",
"headers-core", "headers-core",
@ -851,6 +902,27 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3286f09f7d4926fc486334f28d8d2e6ebe4f7f9994494b6dab27ddfad2c9b11b" 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]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.1" version = "0.4.1"
@ -895,19 +967,22 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]] [[package]]
name = "mi" name = "mi"
version = "0.1.0" version = "1.0.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"color-eyre", "color-eyre",
"diesel",
"hyper", "hyper",
"jsonfeed", "jsonfeed",
"kankyo", "kankyo",
"log", "log",
"mime", "mime",
"paseto",
"pretty_env_logger", "pretty_env_logger",
"prometheus", "prometheus",
"rand 0.7.3", "rand 0.7.3",
"ructe", "ructe",
"rusty_ulid",
"serde", "serde",
"serde_dhall", "serde_dhall",
"thiserror", "thiserror",
@ -1147,6 +1222,22 @@ dependencies = [
"winapi 0.3.9", "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]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
@ -1538,7 +1629,7 @@ version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9eaa17ac5d7b838b7503d118fa16ad88f440498bf9ffe5424e621f93190d61e" checksum = "e9eaa17ac5d7b838b7503d118fa16ad88f440498bf9ffe5424e621f93190d61e"
dependencies = [ dependencies = [
"base64", "base64 0.12.3",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
@ -1567,6 +1658,21 @@ dependencies = [
"winreg", "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]] [[package]]
name = "rle-decode-fast" name = "rle-decode-fast"
version = "1.0.1" version = "1.0.1"
@ -1579,7 +1685,7 @@ version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8ffc645837eab09b6cbb6772d18897c6309753254592e149b40ff7fb113c01" checksum = "1b8ffc645837eab09b6cbb6772d18897c6309753254592e149b40ff7fb113c01"
dependencies = [ dependencies = [
"base64", "base64 0.12.3",
"bytecount", "bytecount",
"itertools", "itertools",
"md5", "md5",
@ -1593,6 +1699,18 @@ version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" 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]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -1797,6 +1915,23 @@ dependencies = [
"winapi 0.3.9", "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]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"
@ -1814,6 +1949,18 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.1.0" version = "3.1.0"
@ -2031,7 +2178,7 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23" checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [ dependencies = [
"base64", "base64 0.12.3",
"byteorder", "byteorder",
"bytes", "bytes",
"http", "http",
@ -2104,6 +2251,12 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]] [[package]]
name = "url" name = "url"
version = "2.1.1" version = "2.1.1"

View File

@ -1,21 +1,25 @@
[package] [package]
name = "mi" name = "mi"
version = "0.1.0" version = "1.0.0"
authors = ["Christine Dodrill <me@christine.website>"] authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018" edition = "2018"
build = "src/build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
kankyo = "0.3"
color-eyre = "0.5.2" color-eyre = "0.5.2"
log = "0.4" diesel = { version = "1.4.4", features = ["sqlite"] }
hyper = "0.13" hyper = "0.13"
kankyo = "0.3"
log = "0.4"
mime = "0.3.0" mime = "0.3.0"
paseto = "1.0.7"
pretty_env_logger = "0" pretty_env_logger = "0"
prometheus = { version = "0.10", default-features = false, features = ["process"] } prometheus = { version = "0.10", default-features = false, features = ["process"] }
rand = "0" rand = "0"
rusty_ulid = "0.10.0"
serde_dhall = "0.6.0" serde_dhall = "0.6.0"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
thiserror = "1" thiserror = "1"

5
diesel.toml Normal file
View File

@ -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"

0
migrations/.gitkeep Normal file
View File

View File

@ -0,0 +1,2 @@
DROP TABLE members;
DROP TABLE switches;

View File

@ -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)
);

View File

@ -0,0 +1 @@
DELETE FROM members;

View File

@ -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');

View File

@ -12,8 +12,13 @@ in pkgs.mkShell {
cmake cmake
zlib zlib
libgit2 libgit2
diesel-cli
sqlite
# keep this line if you use bash # keep this line if you use bash
bashInteractive bashInteractive
]; ];
RUST_LOG="info";
DATABASE_URL="./mi.db";
} }

12
src/build.rs Normal file
View File

@ -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(())
}

View File

@ -1,9 +1,18 @@
#[macro_use]
extern crate diesel;
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use hyper::{header::CONTENT_TYPE, Body, Response}; use hyper::{header::CONTENT_TYPE, Body, Response};
use prometheus::{Encoder, TextEncoder}; use prometheus::{Encoder, TextEncoder};
use warp::Filter; use warp::Filter;
const APPLICATION_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); 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] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -11,6 +20,13 @@ async fn main() -> Result<()> {
pretty_env_logger::init(); pretty_env_logger::init();
color_eyre::install()?; 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 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 metrics_endpoint = warp::path("metrics").and(warp::path::end()).map(move || {
let encoder = TextEncoder::new(); let encoder = TextEncoder::new();

15
src/models.rs Normal file
View File

@ -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<String>,
pub duration: String,
}

22
src/schema.rs Normal file
View File

@ -0,0 +1,22 @@
table! {
members (id) {
id -> Nullable<Integer>,
cmene -> Text,
picurl -> Text,
}
}
table! {
switches (id) {
id -> Text,
who -> Text,
started_at -> Text,
ended_at -> Nullable<Text>,
duration -> Text,
}
}
allow_tables_to_appear_in_same_query!(
members,
switches,
);

1
templates/foo.rs.html Normal file
View File

@ -0,0 +1 @@
@()