containers-index #6
72
Cargo.lock
generated
72
Cargo.lock
generated
@ -514,23 +514,26 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "diesel"
|
||||
version = "1.4.8"
|
||||
version = "2.0.0-rc.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d"
|
||||
checksum = "876a12f2d98c35d1dfaf74083664be5bb71606b72f4756f3792cdb1ddb192b46"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"byteorder",
|
||||
"diesel_derives",
|
||||
"itoa",
|
||||
"pq-sys",
|
||||
"r2d2",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "diesel_derives"
|
||||
version = "1.4.1"
|
||||
version = "2.0.0-rc.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3"
|
||||
checksum = "2497d9cefebedc40492310f3c94493e1d0bd5b11b5e1bfc0a7f4b106012203ea"
|
||||
dependencies = [
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
@ -538,10 +541,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "diesel_migrations"
|
||||
version = "1.4.0"
|
||||
version = "2.0.0-rc.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf3cde8413353dc7f5d72fa8ce0b99a560a359d2c5ef1e5817ca731cd9008f4c"
|
||||
checksum = "af3e284b00439e362c3f55849a103f38b4c5a9918ae3cf34fca2268b6eb52fef"
|
||||
dependencies = [
|
||||
"diesel",
|
||||
"migrations_internals",
|
||||
"migrations_macros",
|
||||
]
|
||||
@ -867,8 +871,10 @@ dependencies = [
|
||||
"diesel_migrations",
|
||||
"log",
|
||||
"rust-embed",
|
||||
"serde",
|
||||
"thiserror",
|
||||
"tide",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -942,23 +948,23 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "migrations_internals"
|
||||
version = "1.4.1"
|
||||
version = "2.0.0-rc.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b4fc84e4af020b837029e017966f86a1c2d5e83e64b589963d5047525995860"
|
||||
checksum = "e03913f1a1c044aa53b37c8f34011599bbf98914092a0444081555e9ca35022c"
|
||||
dependencies = [
|
||||
"diesel",
|
||||
"serde",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "migrations_macros"
|
||||
version = "1.4.2"
|
||||
version = "2.0.0-rc.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9753f12909fd8d923f75ae5c3258cae1ed3c8ec052e1b38c93c21a6d157f789c"
|
||||
checksum = "9ccfa634200e9de9fe6497f568d54951e933e9e0ddf61058e52e24bda896de48"
|
||||
dependencies = [
|
||||
"migrations_internals",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1114,6 +1120,30 @@ dependencies = [
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
@ -1696,6 +1726,15 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.15.0"
|
||||
@ -1746,6 +1785,15 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "value-bag"
|
||||
version = "1.0.0-alpha.9"
|
||||
|
@ -11,8 +11,11 @@ thiserror = "1"
|
||||
|
||||
async-std = "1"
|
||||
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
tide = "0.17.0-beta.1"
|
||||
diesel = { version = "1.4", features = ["postgres", "r2d2"] }
|
||||
diesel_migrations = "1.4"
|
||||
diesel = { version = "2.0.0-rc.1", features = ["postgres", "r2d2", "uuid"] }
|
||||
diesel_migrations = "2.0.0-rc.1"
|
||||
|
||||
uuid = { version = "1.1.2", features = ["serde"] }
|
||||
|
||||
rust-embed = { version = "6.2.0", features = ["interpolate-folder-path"]}
|
||||
|
3
migrations/2022-07-23-163034_create_containers/down.sql
Normal file
3
migrations/2022-07-23-163034_create_containers/down.sql
Normal file
@ -0,0 +1,3 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
drop table if exists containers;
|
11
migrations/2022-07-23-163034_create_containers/up.sql
Normal file
11
migrations/2022-07-23-163034_create_containers/up.sql
Normal file
@ -0,0 +1,11 @@
|
||||
-- Your SQL goes here
|
||||
|
||||
create table containers
|
||||
(
|
||||
id uuid default gen_random_uuid() not null
|
||||
primary key,
|
||||
parent uuid
|
||||
constraint foreign_key_name
|
||||
references containers,
|
||||
name varchar not null
|
||||
);
|
33
src/api/containers.rs
Normal file
33
src/api/containers.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use super::State;
|
||||
use crate::db;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use diesel::Queryable;
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn setup_routes(mut route: tide::Route<Arc<State>>) {
|
||||
route.at("/").get(index);
|
||||
}
|
||||
|
||||
#[derive(Queryable, Serialize)]
|
||||
pub struct Container {
|
||||
pub id: Uuid,
|
||||
pub parent: Option<Uuid>,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
async fn index(req: tide::Request<Arc<State>>) -> tide::Result {
|
||||
use db::schema::containers::dsl::*;
|
||||
use diesel::{QueryDsl, RunQueryDsl};
|
||||
|
||||
let mut connection = req.state().db.get().unwrap();
|
||||
|
||||
let items: Vec<Container> = containers
|
||||
.limit(5)
|
||||
.load::<Container>(&mut connection)
|
||||
.expect("Error loading containers");
|
||||
|
||||
Ok(tide::Body::from_json(&items).unwrap().into())
|
||||
}
|
@ -1,17 +1,20 @@
|
||||
mod containers;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
|
||||
pub struct State {
|
||||
_db: Pool<ConnectionManager<PgConnection>>,
|
||||
db: Pool<ConnectionManager<PgConnection>>,
|
||||
}
|
||||
|
||||
pub fn serve(db: Pool<ConnectionManager<PgConnection>>) -> tide::Server<Arc<State>> {
|
||||
let state = Arc::new(State { _db: db });
|
||||
let state = Arc::new(State { db });
|
||||
|
||||
let mut app = tide::with_state(state);
|
||||
app.at("/hello").get(|_| async { Ok("Hello, Jake!") });
|
||||
|
||||
containers::setup_routes(app.at("/containers/"));
|
||||
|
||||
app
|
||||
}
|
@ -6,5 +6,5 @@ pub enum Error {
|
||||
Pool(#[from] diesel::r2d2::PoolError),
|
||||
|
||||
#[error("database migration failed: {0}")]
|
||||
Migration(#[from] diesel::migration::RunMigrationsError),
|
||||
Migration(Box<dyn std::error::Error + Send + Sync>),
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ mod error;
|
||||
#[rustfmt::skip]
|
||||
pub mod schema;
|
||||
|
||||
use crate::embedded_migrations;
|
||||
pub use error::Error;
|
||||
|
||||
use std::env;
|
||||
@ -12,13 +11,18 @@ use std::env;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
pub(super) fn new() -> Result<Pool<ConnectionManager<PgConnection>>, Error> {
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let pool = Pool::new(ConnectionManager::new(database_url))?;
|
||||
let pool = Pool::new(ConnectionManager::<PgConnection>::new(database_url))?;
|
||||
|
||||
info!("starting db migrations...");
|
||||
embedded_migrations::run(&pool.get()?)?;
|
||||
pool.get()?
|
||||
.run_pending_migrations(MIGRATIONS)
|
||||
.map_err(|e| Error::Migration(e))?;
|
||||
info!("db migrations complete");
|
||||
|
||||
Ok(pool)
|
||||
|
@ -1,6 +1,19 @@
|
||||
table! {
|
||||
containers (id) {
|
||||
id -> Uuid,
|
||||
parent -> Nullable<Uuid>,
|
||||
name -> Varchar,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Uuid,
|
||||
nickname -> Varchar,
|
||||
}
|
||||
}
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
containers,
|
||||
users,
|
||||
);
|
||||
|
@ -13,12 +13,6 @@ use std::io;
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
// embed diesel migrations
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
embed_migrations!();
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
log::with_level(
|
||||
std::env::var("LOG")
|
||||
|
Loading…
Reference in New Issue
Block a user