From bdd0256bfc3331b38508125a79a1061f597fcc18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Drouet?= Date: Mon, 23 Mar 2020 14:00:43 +0100 Subject: [PATCH] react-rust-postgres: create simple server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Drouet --- react-rust-postgres/backend/.dockerignore | 2 ++ react-rust-postgres/backend/.gitignore | 2 ++ react-rust-postgres/backend/Cargo.toml | 18 +++++++++++++++ react-rust-postgres/backend/Dockerfile | 28 +++++++++++++++++++++++ react-rust-postgres/backend/src/main.rs | 25 ++++++++++++++++++++ react-rust-postgres/docker-compose.yaml | 16 +++++++++++++ 6 files changed, 91 insertions(+) create mode 100644 react-rust-postgres/backend/.dockerignore create mode 100644 react-rust-postgres/backend/.gitignore create mode 100644 react-rust-postgres/backend/Cargo.toml create mode 100644 react-rust-postgres/backend/Dockerfile create mode 100644 react-rust-postgres/backend/src/main.rs create mode 100644 react-rust-postgres/docker-compose.yaml diff --git a/react-rust-postgres/backend/.dockerignore b/react-rust-postgres/backend/.dockerignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/react-rust-postgres/backend/.dockerignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/react-rust-postgres/backend/.gitignore b/react-rust-postgres/backend/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/react-rust-postgres/backend/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/react-rust-postgres/backend/Cargo.toml b/react-rust-postgres/backend/Cargo.toml new file mode 100644 index 0000000..99c6eb4 --- /dev/null +++ b/react-rust-postgres/backend/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "react-rust-postgres" +version = "0.1.0" +authors = ["Jérémie Drouet "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rocket = "0.4.4" +serde = "1.0" +serde_json = "1.0" +serde_derive = "1.0" + +[dependencies.rocket_contrib] +version = "0.4.4" +default-features = false +features = ["json"] diff --git a/react-rust-postgres/backend/Dockerfile b/react-rust-postgres/backend/Dockerfile new file mode 100644 index 0000000..5424f3e --- /dev/null +++ b/react-rust-postgres/backend/Dockerfile @@ -0,0 +1,28 @@ +FROM jdrouet/rust-nightly:buster-slim AS base + +ENV USER=root +ENV ROCKET_ADDRESS=0.0.0.0 + +WORKDIR /code +RUN cargo init +COPY Cargo.toml /code/Cargo.toml +RUN cargo fetch +COPY . /code + +FROM base AS development + +EXPOSE 8000 + +CMD [ "cargo", "run", "--offline" ] + +FROM base AS builder + +RUN cargo build --release --offline + +FROM debian:buster-slim + +EXPOSE 8000 + +COPY --from=builder /code/target/release/react-rust-postgres /react-rust-postgres + +CMD [ "/react-rust-postgres" ] diff --git a/react-rust-postgres/backend/src/main.rs b/react-rust-postgres/backend/src/main.rs new file mode 100644 index 0000000..8b08ae7 --- /dev/null +++ b/react-rust-postgres/backend/src/main.rs @@ -0,0 +1,25 @@ +#![feature(proc_macro_hygiene, decl_macro)] + +#[macro_use] +extern crate rocket; +#[macro_use] +extern crate serde_derive; +extern crate rocket_contrib; + +use rocket_contrib::json::Json; + +#[derive(Serialize)] +struct HelloMessage { + message: String, +} + +#[get("/")] +fn index() -> Json { + Json(HelloMessage { + message: String::from("Hello, world"), + }) +} + +fn main() { + rocket::ignite().mount("/", routes![index]).launch(); +} diff --git a/react-rust-postgres/docker-compose.yaml b/react-rust-postgres/docker-compose.yaml new file mode 100644 index 0000000..14824eb --- /dev/null +++ b/react-rust-postgres/docker-compose.yaml @@ -0,0 +1,16 @@ +version: "3.7" +services: + backend: + build: + context: backend + target: development + networks: + - server-side + volumes: + - ./backend/src:/code/src:ro + - backend-cache:/code/target +networks: + client-side: {} + server-side: {} +volumes: + backend-cache: {}