react-rust-postgres: add connection to postgres with migrations

Signed-off-by: Jérémie Drouet <jeremie.drouet@gmail.com>
This commit is contained in:
Jérémie Drouet 2020-03-26 10:39:45 +01:00
parent bed22c74f5
commit b73e252639
12 changed files with 192 additions and 6 deletions

View File

@ -7,12 +7,17 @@ edition = "2018"
# 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]
diesel_migrations = "1.4.0"
rocket = "0.4.4" rocket = "0.4.4"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
[dependencies.diesel]
version = "1.0.0"
features = ["postgres"]
[dependencies.rocket_contrib] [dependencies.rocket_contrib]
version = "0.4.4" version = "0.4.4"
default-features = false default-features = false
features = ["json"] features = ["json", "diesel_postgres_pool"]

View File

@ -1,7 +1,12 @@
FROM jdrouet/rust-nightly:buster-slim AS base FROM jdrouet/rust-nightly:buster-slim AS base
RUN apt-get update \
&& apt-get install -y libpq-dev \
&& rm -rf /var/lib/apt/lists/*
ENV USER=root ENV USER=root
ENV ROCKET_ADDRESS=0.0.0.0 ENV ROCKET_ADDRESS=0.0.0.0
ENV ROCKET_ENV=development
WORKDIR /code WORKDIR /code
RUN cargo init RUN cargo init
@ -21,6 +26,8 @@ RUN cargo build --release --offline
FROM debian:buster-slim FROM debian:buster-slim
ENV ROCKET_ENV=production
EXPOSE 8000 EXPOSE 8000
COPY --from=builder /code/target/release/react-rust-postgres /react-rust-postgres COPY --from=builder /code/target/release/react-rust-postgres /react-rust-postgres

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"

View File

@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();

View File

@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1 @@
DROP TABLE users;

View File

@ -0,0 +1,4 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
login TEXT UNIQUE NOT NULL
);

View File

@ -1,12 +1,32 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
#[macro_use]
extern crate rocket_contrib; extern crate rocket_contrib;
mod schema;
mod user;
use rocket::config::{Config, Environment, Value};
use rocket::fairing::AdHoc;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use std::collections::HashMap;
use std::env;
// This macro from `diesel_migrations` defines an `embedded_migrations` module
// containing a function named `run`. This allows the example to be run and
// tested without any outside setup of the database.
embed_migrations!();
#[database("my_db")]
struct MyDBConn(diesel::PgConnection);
#[derive(Serialize)] #[derive(Serialize)]
struct HelloMessage { struct HelloMessage {
@ -14,12 +34,63 @@ struct HelloMessage {
} }
#[get("/")] #[get("/")]
fn index() -> Json<HelloMessage> { fn index(conn: MyDBConn) -> Json<HelloMessage> {
let result = match user::User::all(&*conn) {
Ok(res) => res.len(),
Err(_) => 0,
};
Json(HelloMessage { Json(HelloMessage {
message: String::from("Hello, world"), message: format!("Hello with {} users", result),
}) })
} }
fn main() { fn get_config() -> Config {
rocket::ignite().mount("/", routes![index]).launch(); let mut database_config = HashMap::new();
let mut databases = HashMap::new();
let env_address = env::var("ROCKET_ADDRESS")
.or::<String>(Ok(String::from("localhost")))
.unwrap();
let env_mode = env::var("ROCKET_ENV")
.or(Ok(String::from("development")))
.and_then(|value| value.parse::<Environment>())
.unwrap();
let database_url = match env::var("DATABASE_URL") {
Ok(value) => value,
Err(_) => String::from("postgres://localhost/postgres"),
};
database_config.insert("url", Value::from(database_url));
databases.insert("my_db", Value::from(database_config));
let config = Config::build(env_mode)
.address(env_address)
.extra("databases", databases)
.finalize()
.unwrap();
config
}
fn run_db_migrations(r: rocket::Rocket) -> Result<rocket::Rocket, rocket::Rocket> {
let conn = MyDBConn::get_one(&r).expect("database connection");
match embedded_migrations::run(&*conn) {
Ok(()) => Ok(r),
Err(e) => {
println!("Failed to run database migrations: {:?}", e);
Err(r)
}
}
}
fn main() {
let config = get_config();
rocket::custom(config)
.attach(MyDBConn::fairing())
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
.mount("/", routes![index])
.launch();
} }

View File

@ -0,0 +1,6 @@
table! {
users (id) {
id -> Int4,
login -> Text,
}
}

View File

@ -0,0 +1,18 @@
#![allow(proc_macro_derive_resolution_fallback)]
use diesel;
use diesel::prelude::*;
use super::schema::users;
#[derive(Queryable, AsChangeset, Serialize, Deserialize)]
#[table_name = "users"]
pub struct User {
pub id: i32,
pub login: String,
}
impl User {
pub fn all(connection: &PgConnection) -> QueryResult<Vec<User>> {
users::table.load::<User>(&*connection)
}
}

View File

@ -1,16 +1,43 @@
version: "3.7" version: "3.7"
services: services:
frontend:
build:
context: frontend
target: development
networks:
- client-side
ports:
- 3000:3000
volumes:
- ./frontend/src:/code/src:ro
backend: backend:
build: build:
context: backend context: backend
target: development target: development
environment:
- DATABASE_URL=postgres://postgres:mysecretpassword@db/postgres
networks: networks:
- client-side
- server-side - server-side
volumes: volumes:
- ./backend/src:/code/src:ro - ./backend/src:/code/src
- backend-cache:/code/target - backend-cache:/code/target
depends_on:
- db
db:
image: postgres:12-alpine
restart: always
environment:
- POSTGRES_PASSWORD=mysecretpassword
networks:
- server-side
ports:
- 5432:5432
volumes:
- db-data:/var/lib/postgresql/data
networks: networks:
client-side: {} client-side: {}
server-side: {} server-side: {}
volumes: volumes:
backend-cache: {} backend-cache: {}
db-data: {}