Skip to content

Commit

Permalink
Merge pull request #1 from MiSArch/initial-setup
Browse files Browse the repository at this point in the history
Initial setup of review service
  • Loading branch information
legendofa authored Feb 6, 2024
2 parents b871219 + ca5bd0a commit 98bdcab
Show file tree
Hide file tree
Showing 28 changed files with 1,466 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .dapr/components/pubsub.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.in-memory
version: v1
metadata: []
5 changes: 5 additions & 0 deletions .dapr/dapr-config-minimal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: daprConfig
namespace: default
43 changes: 43 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
{
"name": "Existing Docker Compose (Extend)",

// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
"dockerComposeFile": ["../compose.yaml", "docker-compose.yml"],

// The 'service' property is the name of the service for the container that VS Code should
// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
"service": "review",

// The optional 'workspaceFolder' property is the path VS Code should open by default when
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"customizations": {
"vscode": {
"extensions": ["rust-lang.rust-analyzer"]
}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],

// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",

// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "cat /etc/os-release",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "devcontainer"
}
25 changes: 25 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.8"
services:
# Update this to the name of the service you want to work with in your docker-compose.yml file
review:
# Uncomment if you want to override the service's Dockerfile to one in the .devcontainer
# folder. Note that the path of the Dockerfile and context is relative to the *primary*
# docker-compose.yml file (the first in the devcontainer.json "dockerComposeFile"
# array). The sample below assumes your primary file is in the root of your project.
#
# build:
# context: .
# dockerfile: .devcontainer/Dockerfile

volumes:
# Update this to wherever you want VS Code to mount the folder of your project
- ..:/workspaces:cached

# Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
# cap_add:
# - SYS_PTRACE
# security_opt:
# - seccomp:unconfined

# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
50 changes: 50 additions & 0 deletions .github/workflows/publish-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Create and publish a Docker image on Release

on:
push:
branches:
- "main"
tags:
- "v*"
pull_request:
branches:
- "main"

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:

build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: base-dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
24 changes: 24 additions & 0 deletions .github/workflows/test-update-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test update GraphQL schema on pull request

on:
pull_request:

jobs:
schema:
name: Test update GraphQL schema on pull request
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install latest stable
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
- uses: actions/checkout@v4
with:
repository: "misarch/schemas"
path: "schemas"
- name: Save graphql schemas
run: |
sudo apt install -y protobuf-compiler
cargo run -- --generate-schema
29 changes: 29 additions & 0 deletions .github/workflows/update-infrastructure-docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update infrastructure-docker submodule

on:
push:
branches:
- main

jobs:
schema:
name: Update infrastructure-docker submodule
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
repository: "misarch/infrastructure-docker"
submodules: true
- name: Update submodule
run: |
cd review
git checkout ${{ github.sha }}
- uses: peter-evans/create-pull-request@v5
with:
commit-message: Update review schema
branch: update/review
token: ${{ secrets.INFRASTRUCTURE_DOCKER_PUSH_SECRET }}
- name: Set to auto merge
run: gh pr merge update/review --auto --merge -R misarch/infrastructure-docker
env:
GH_TOKEN: ${{ secrets.INFRASTRUCTURE_DOCKER_PUSH_SECRET }}
32 changes: 32 additions & 0 deletions .github/workflows/update-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Update GraphQL schema

on:
push:
branches:
- main

jobs:
schema:
name: Update GraphQL schema
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install latest stable
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
- uses: actions/checkout@v4
with:
repository: "misarch/schemas"
path: "schemas"
- name: Save graphql schemas
run: |
sudo apt install -y protobuf-compiler
cargo run -- --generate-schema
- uses: peter-evans/create-pull-request@v5
with:
path: ./schemas
commit-message: Update review schema
branch: update/review
token: ${{ secrets.SCHEMAS_PUSH_SECRET }}
26 changes: 26 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "misarch-review"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-graphql = { version = "6.0.11", features = ["bson", "chrono", "uuid", "log"] }
async-graphql-axum = "6.0.11"
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
hyper = "1.0.1"
axum = { version = "0.6.0", features = ["headers", "macros"] }
slab = "0.4.2"
mongodb = "2.8.0"
serde = "1.0.193"
futures = "0.3.30"
bson = "2.8.1"
clap = { version = "4.4.13", features = ["derive"] }
uuid = { version = "1.6.1", features = ["v4", "serde"] }
mongodb-cursor-pagination = "0.3.2"
dapr = "0.13.0"
tonic = "0.8"
json = "0.12.4"
log = "0.4.20"
simple_logger = "4.3.3"
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Review service for MiSArch

### Quickstart (DevContainer)

1. Open VSCode Development Container
2. `cargo run` starts the GraphQL service + GraphiQL on port `8080`

### Quickstart (Docker Compose)

1. `docker compose -f docker-compose-dev.yaml up --build` in the repository root directory. **IMPORTANT:** MongoDB credentials should be configured for production.

### What it can do

- CRUD reviews:

```rust
pub struct Review {
pub id: Uuid,
pub user_id: Uuid,
pub product_variants: HashSet<ProductVariant>,
pub name: String,
pub created_at: DateTime,
pub last_updated_at: DateTime,
}

/// Foreign ProductVariant
pub struct ProductVariant{
id: Uuid
}
```

- Validates all UUIDs input as strings
- Error prop to GraphQL
27 changes: 27 additions & 0 deletions base-dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Source: https://github.com/LukeMathWalker/cargo-chef

FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /misarch-review

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /misarch-review/recipe.json recipe.json

RUN apt update && apt install -y protobuf-compiler && rm -rf /var/lib/apt/lists/*

# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .

RUN cargo build --release --bin misarch-review

# We do not need the Rust toolchain to run the binary!
FROM debian:bookworm-slim AS runtime

WORKDIR /misarch-review
COPY --from=builder /misarch-review/target/release/misarch-review /usr/local/bin
ENTRYPOINT ["/usr/local/bin/misarch-review"]
36 changes: 36 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
review:
extends:
file: docker-compose-base.yaml
service: review
build:
context: .
dockerfile: devcontainer-dockerfile
ports:
- 8080:8080
review-db:
extends:
file: docker-compose-base.yaml
service: review-db
review-mongoexpress:
image: mongo-express
ports:
- 8081:8081
depends_on:
- review-db
environment:
ME_CONFIG_MONGODB_URL: mongodb://review-db:27017
review-dapr:
extends:
file: docker-compose-base.yaml
service: review-dapr
volumes:
- "./.dapr/dapr-config-minimal.yaml:/config.yaml"
- "./.dapr/components:/components"
placement:
image: "daprio/dapr"
command: ["./placement", "-port", "50006"]
ports:
- 50006:50006
volumes:
review-db-data:
6 changes: 6 additions & 0 deletions dev-dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM rust:1.75-slim-bookworm

WORKDIR /usr/src/misarch-review

COPY . .
CMD ["cargo", "run"]
3 changes: 3 additions & 0 deletions devcontainer-dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM rust:1.75-slim-bookworm

WORKDIR /usr/src/misarch-review
Loading

0 comments on commit 98bdcab

Please sign in to comment.