Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

add restaurant service #14

Merged
merged 2 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .github/workflows/restaurant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Restaurant Microservice CI/CD

on:
push:
branches: [ restaurant ]
pull_request:
branches: [ dev, main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: pnpm/action-setup@v2
with:
version: latest
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: "pnpm"
cache-dependency-path: services/restaurant/pnpm-lock.yaml
- name: Install dependencies
run: |
cd ./services/restaurant
pnpm install --frozen-lockfile
- name: Build
run: |
cd ./services/restaurant
pnpm build

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: pnpm/action-setup@v2
with:
version: latest
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: "pnpm"
cache-dependency-path: services/restaurant/pnpm-lock.yaml
- name: Install dependencies
run: |
cd ./services/restaurant
pnpm install --frozen-lockfile
- name: Execute tests
run: |
cd ./services/restaurant
pnpm test

publish:
runs-on: ubuntu-latest
needs: [ build, test ]
steps:
- name: Checkout code
uses: actions/checkout@master
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: ./services/
file: ./services/restaurant/Dockerfile
push: true
tags: floriaaan/goodfood-restaurant:latest
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ a high volume of concurrent users, up to several thousand.
## Microservices ports

| Service | Port | Language | Database | Status | Assignee |
|-------------|-------|-------------|------------|--------|-----------------|
| Gateway | 50000 | Go | ❌ | ❌ | @Anatole-Godard |
| ----------- | ----- | ----------- | ---------- | ------ | --------------- |
| Gateway | 50000 | Go | ❌ | ❌ | @Anatole-Godard |
| User (auth) | 50001 | Go | PostgreSQL | ⚠️ | @Anatole-Godard |
| Basket | 50002 | NodeJS (ts) | Redis | ⚠️ | @Anatole-Godard |
| Payment | 50003 | NodeJS (ts) | PostgreSQL | ✅ | @floriaaan |
| Product | 50004 | NodeJS (ts) | PostgreSQL | ❌ | @PierreLbg |
| Restaurant | 50005 | NodeJS (ts) | PostgreSQL | ❌ | @PierreLbg |
| Promotion | 50006 | NodeJS (ts) | PostgreSQL | ❌ | @PierreLbg |
| Order | 50007 | NodeJS (ts) | PostgreSQL | ✅ | @floriaaan |
| Delivery | 50008 | NodeJS (ts) | PostgreSQL | ✅ | @floriaaan |
| Payment | 50003 | NodeJS (ts) | PostgreSQL | ✅ | @floriaaan |
| Product | 50004 | NodeJS (ts) | PostgreSQL | ❌ | @PierreLbg |
| Restaurant | 50005 | NodeJS (ts) | PostgreSQL | ❌ | @floriaaan |
| Promotion | 50006 | NodeJS (ts) | PostgreSQL | ❌ | @PierreLbg |
| Order | 50007 | NodeJS (ts) | PostgreSQL | ✅ | @floriaaan |
| Delivery | 50008 | NodeJS (ts) | PostgreSQL | ✅ | @floriaaan |
| Stock | 50009 | NodeJS (ts) | PostgreSQL | ⚠️ | @floriaaan |
| Reporting | 50020 | C# (dotnet) | PostgreSQL | ⚠️ | @floriaaan |
| Log | 50021 | Go | PostgreSQL | ✅ | @floriaaan |
| Log | 50021 | Go | PostgreSQL | ✅ | @floriaaan |
| (...) | (...) | (...) | (...) | (...) |

## File Hierarchy
Expand Down
53 changes: 53 additions & 0 deletions services/proto/restaurant.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
syntax = "proto3";
import "google/protobuf/empty.proto";

package com.goodfood.restaurant;

message Restaurant {
string id = 1;

string name = 2;
optional string description = 3;

repeated float location = 4;
optional string address = 5;

repeated string openingHours = 6;
optional string phone = 7;

string createdAt = 8;
string updatedAt = 9;
}

message RestaurantCreateInput{
string name = 1;
optional string description = 2;

repeated float location = 3;
optional string address = 4;

repeated string openingHours = 5;
optional string phone = 6;
}

message RestaurantList {
repeated Restaurant restaurants = 1;
}

message ByLocationInput {
repeated float location = 1;
}

message RestaurantId {
string id = 1;
}


service RestaurantService {
rpc CreateRestaurant (RestaurantCreateInput) returns (Restaurant) {}
rpc GetRestaurant (RestaurantId) returns (Restaurant) {}
rpc UpdateRestaurant (Restaurant) returns (Restaurant) {}
rpc DeleteRestaurant (RestaurantId) returns (google.protobuf.Empty) {}
rpc GetRestaurants (google.protobuf.Empty) returns (RestaurantList) {}
rpc GetRestaurantsByLocation (ByLocationInput) returns (RestaurantList) {}
}
2 changes: 2 additions & 0 deletions services/restaurant/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
4 changes: 4 additions & 0 deletions services/restaurant/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres
AMQP_URL=amqp://guest:guest@localhost

PORT=50005
5 changes: 5 additions & 0 deletions services/restaurant/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
bin/
*.env*
!*.env.example
38 changes: 38 additions & 0 deletions services/restaurant/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM node:18-alpine3.17 as builder

# Set working directory
WORKDIR /app

# Copy the application code
COPY ./restaurant/ .

# Install dependencies
RUN npm install

# Copy the proto files
COPY ./proto ./proto/

# Generate Prisma client
RUN npx prisma generate

# Build the application
RUN npm run build


# Create a new image with the application
FROM node:18-alpine3.17 as runner

# Set working directory
WORKDIR /app

# Copy the application package
COPY --from=builder /app/dist .
COPY --from=builder /app/prisma/ .
COPY --from=builder /app/proto/ /proto/
COPY --from=builder /app/node_modules/.prisma /.prisma

# Expose the gRPC port
EXPOSE 50005

# Start the server
CMD [ "node", "index.js"]
77 changes: 77 additions & 0 deletions services/restaurant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Restaurant Microservice

| Informations |
| ------------------------------------------------------------------ |
| **Port:** 50005 |
| **Developer:** @floriaaan |
| **Status:** In progress |
| **Last update:** 2023-08-19 |
| **Language:** NodeJS |
| **Dependencies:** TypeScript, Prisma, gRPC, Postgres |
| **Models:** (see [`prisma/schema.prisma`](./prisma/schema.prisma)) |

## gRPC Methods

TBW

## Requirements

To run this microservice, you will need to have the following installed on your system:

- NodeJS (v18.12.0 or higher) (dev. with v18.12.0)
- Postgres (v15.2 or higher) (dev. with docker image `postgres:15.2`)

You can use the following tools to help you with the setup:

- You can use nvm to set your Node version using:
- `nvm use`.
- You can use docker to run your Postgres database using:
- `docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres`

## Getting started

### 1. Clone the repository and install dependencies

1. Clone the `goodfood` repository to your local machine.
2. Navigate to the service directory (`services/promotions`) in your terminal.
3. Run `npm install` to install the necessary dependencies.
4. Create a `.env` file at the root of the project directory and add the environment variables values (
see `.env.example`).
5. Run `npm run start` to start the microservice.

You can now access the microservice at `http://localhost:50006`.

NB: If you want to run the microservice in development mode, you can run `npm run dev` instead.

### 2. Create and seed the database

Run the following command to create your Postgres database structure. This also creates the models tables that are
defined in [`prisma/schema.prisma`](./prisma/schema.prisma):

```
npx prisma migrate dev --name init
```

When `npx prisma migrate dev` is executed against a newly created database, seeding is also triggered. The seed file
in [`prisma/seed.ts`](./prisma/seed.ts) will be executed and your database will be populated with the sample data.

## Build and Run with Docker

### Build

To build the image you need to be in the **parent folder** of the service you want to build. Then run the following
command:

```
docker build -t goodfood-restaurant:1.0.0 -f ./restaurant/Dockerfile .
docker tag goodfood-restaurant:1.0.0 pierrelbg/goodfood-restaurant:1.0.0
docker push floriaaan/goodfood-restaurant:1.0.0
```

### Run

Create the .env base on the .env.example. Then run the following command:

```
docker run --env-file=.env goodfood-restaurant:1.0.0
```
8 changes: 8 additions & 0 deletions services/restaurant/k8s/configmap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: goodfood-restaurant-configmap
labels:
app: goodfood-restaurant
data:
port: "50005"
31 changes: 31 additions & 0 deletions services/restaurant/k8s/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: goodfood-restaurant
labels:
app: goodfood-restaurant
spec:
selector:
matchLabels:
app: goodfood-restaurant
template:
metadata:
labels:
app: goodfood-restaurant
spec:
containers:
- name: goodfood-restaurant
image: floriaaan/goodfood-restaurant:1.0.0
imagePullPolicy: Always
resources:
limits:
memory: "128Mi"
cpu: "200m"
env:
- name: PORT
valueFrom:
configMapKeyRef:
name: goodfood-restaurant-configmap
key: port
ports:
- containerPort: 50005
18 changes: 18 additions & 0 deletions services/restaurant/k8s/hpa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: goodfood-restaurant-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: goodfood-restaurant
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
17 changes: 17 additions & 0 deletions services/restaurant/k8s/ingress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: goodfood-restaurant
spec:
ingressClassName: nginx
rules:
- host: restaurant.localdev.me
http:
paths:
- pathType: Prefix
backend:
service:
name: goodfood-restaurant
port:
number: 50005
path: /
13 changes: 13 additions & 0 deletions services/restaurant/k8s/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: goodfood-delivery
spec:
selector:
app: goodfood-delivery
ports:
- name: "grpc"
port: 50005
targetPort: 50005
status:
loadBalancer: {}
Loading
Loading