Merge branch '24-diesel' into develop

master^2
Jef Roosens 2021-04-17 11:03:09 +02:00
commit 758a332138
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
10 changed files with 127 additions and 7 deletions

View File

@ -6,3 +6,6 @@
# Cargo files
!Cargo.toml
!Cargo.lock
# Entrypoint for devop container
!docker/entrypoint_dev.sh

85
Cargo.lock generated
View File

@ -331,6 +331,7 @@ dependencies = [
"byteorder",
"diesel_derives",
"pq-sys",
"r2d2",
]
[[package]]
@ -741,6 +742,15 @@ dependencies = [
"libc",
]
[[package]]
name = "instant"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "iovec"
version = "0.1.4"
@ -805,6 +815,15 @@ version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
[[package]]
name = "lock_api"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3c91c24eae6777794bb1997ad98bbb87daf92890acab859f7eaa4320333176"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.3.9"
@ -1045,6 +1064,31 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
dependencies = [
"instant",
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018"
dependencies = [
"cfg-if 1.0.0",
"instant",
"libc",
"redox_syscall",
"smallvec",
"winapi 0.3.9",
]
[[package]]
name = "parse-zoneinfo"
version = "0.3.0"
@ -1204,6 +1248,17 @@ dependencies = [
"proc-macro2 1.0.26",
]
[[package]]
name = "r2d2"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f"
dependencies = [
"log 0.4.14",
"parking_lot",
"scheduled-thread-pool",
]
[[package]]
name = "rand"
version = "0.8.3"
@ -1359,13 +1414,28 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7954a707f9ca18aa74ca8c1f5d1f900f52a4dceb68e96e3112143f759cfd20e"
dependencies = [
"diesel",
"log 0.4.14",
"notify",
"r2d2",
"rocket",
"rocket_contrib_codegen",
"serde",
"serde_json",
]
[[package]]
name = "rocket_contrib_codegen"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30deb6dec53b91fac3538a2a3935cf13e0f462745f9f33bf27bedffbe7265b5d"
dependencies = [
"devise",
"quote 0.6.13",
"version_check 0.9.3",
"yansi",
]
[[package]]
name = "rocket_http"
version = "0.4.7"
@ -1423,6 +1493,21 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "scheduled-thread-pool"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7"
dependencies = [
"parking_lot",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "security-framework"
version = "2.2.0"

View File

@ -32,4 +32,4 @@ diesel = { version = "1.4.6", features = ["postgres"] }
[dependencies.rocket_contrib]
version = "0.4.7"
default-features = false
features = ["json"]
features = ["json", "diesel_postgres_pool"]

11
Rocket.toml 100644
View File

@ -0,0 +1,11 @@
[development]
address = "0.0.0.0"
port = 8000
keep_alive = 5
read_timeout = 5
write_timeout = 5
log = "normal"
limits = { forms = 32768 }
[global.databases]
postgres_fej = { url = "postgres://fej:fej@fej_db:5432/fej" }

View File

@ -4,14 +4,13 @@ FROM alpine:latest AS builder
ENV PATH "$PATH:/root/.cargo/bin"
# Needed for proper compiling of openssl-dev
ENV RUSTFLAGS "-C target-feature=-crt-static"
# Otherwise, the debug build can't be used from the container
ENV ROCKET_ADDRESS "0.0.0.0"
WORKDIR /usr/src/app
# Install build dependencies, rustup & rust's nightly build & toolchain
RUN apk update && apk add --no-cache openssl-dev build-base curl && \
{ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; }
RUN apk update && apk add --no-cache openssl-dev curl postgresql-dev libgcc musl-dev gcc && \
{ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; } && \
rustup target add x86_64-unknown-linux-musl --toolchain nightly
# Copy source code over to builder
COPY Cargo.toml Cargo.lock ./

View File

@ -2,5 +2,7 @@ FROM chewingbever/fej-builder:latest
ENV RUST_BACKTRACE 1
ENTRYPOINT ["cargo"]
COPY ./docker/entrypoint_dev.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["run", "--bin", "server"]

View File

@ -7,7 +7,10 @@ FROM chewingbever/fej-builder:latest AS builder
# NOTE: cargo install auto-appends bin to the path
RUN --mount=type=cache,target=/usr/src/app/target \
--mount=type=cache,target=/root/.cargo/registry \
cargo install --path . --root /usr/local
cargo install \
--path . \
--root /usr/local \
--target x86_64-unknown-linux-musl
# Now, we create the actual image

View File

@ -0,0 +1,4 @@
#!/usr/bin/env sh
# All this file does is inject the target
cargo "$@" --target x86_64-unknown-linux-musl

5
fejctl
View File

@ -28,6 +28,10 @@ function create_images() {
#
# $@: the arguments to pass to the image (passed as arguments to cargo)
function run_image() {
docker volume create fej_build-cache
docker volume create fej_registry-cache
docker volume create fej_db-data
# Run the database image
docker run --rm \
--detach \
@ -52,6 +56,7 @@ function run_image() {
--network fej \
-v 'fej_build-cache:/usr/src/app/target' \
-v 'fej_registry-cache:/root/.cargo/registry' \
-v "$PWD/Rocket.toml:/usr/src/app/Rocket.toml:ro" \
"$image:dev" "$@"
}

View File

@ -2,6 +2,8 @@
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
#[cfg(test)]
mod tests;
@ -14,6 +16,7 @@ mod routes;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::{Request, Response};
use rocket_contrib::databases::diesel;
pub struct CORS;
@ -36,9 +39,14 @@ impl Fairing for CORS {
}
}
// This defines a connection to the database
#[database("postgres_fej")]
struct FejDbConn(diesel::PgConnection);
fn rocket() -> rocket::Rocket {
rocket::ignite()
.attach(CORS)
.attach(FejDbConn::fairing())
.mount("/ivago", routes::ivago())
.register(catchers![catchers::not_found])
}