Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Compiled class file
*.class
.gradle/

# Log file
*.log
Expand Down
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.0' apply false
id 'io.spring.dependency-management' version '1.1.0' apply false
}

allprojects {
group = 'com.ofds'
version = '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
}

subprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
}

84 changes: 84 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
version: '3.8'

services:
gateway:
build:
context: .
dockerfile: gateway/Dockerfile
container_name: gateway
ports:
- "8080:8080"
depends_on:
- order-service
- restaurant-service
- delivery-service
networks:
- ofds-network

postgres:
image: postgres:15
container_name: ofds-postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: ofds
ports:
- "5432:5432"
networks:
- ofds-network

order-service:
build:
context: .
dockerfile: services/order-service/Dockerfile
container_name: order-service
depends_on:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/ofds
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: password
SPRING_JPA_HIBERNATE_DDL_AUTO: update
SPRING_JPA_SHOW_SQL: "true"
SPRING_PROFILES_ACTIVE: docker
networks:
- ofds-network

restaurant-service:
build:
context: .
dockerfile: services/restaurant-service/Dockerfile
container_name: restaurant-service
depends_on:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/ofds
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: password
SPRING_JPA_HIBERNATE_DDL_AUTO: update
SPRING_JPA_SHOW_SQL: "true"
SPRING_PROFILES_ACTIVE: docker
networks:
- ofds-network

delivery-service:
build:
context: .
dockerfile: services/delivery-service/Dockerfile
container_name: delivery-service
depends_on:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/ofds
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: password
SPRING_JPA_HIBERNATE_DDL_AUTO: update
SPRING_JPA_SHOW_SQL: "true"
SPRING_PROFILES_ACTIVE: docker
networks:
- ofds-network

networks:
ofds-network:
driver: bridge

36 changes: 36 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Stage 1: Build the application
FROM gradle:8.1-jdk17 AS builder

# Set the working directory
WORKDIR /app

# Copy the root project files
COPY ../settings.gradle ../build.gradle ./

# Copy the entire project
COPY ../ .

# Build the application using Gradle
RUN gradle :gateway:clean :gateway:bootJar -x test

# Stage 2: Create a lightweight image to run the application
FROM openjdk:17-jdk-slim

# Create a user to run the application
RUN useradd -m appuser

# Set the working directory
WORKDIR /app

# Copy the built JAR file from the build stage
COPY --from=builder /app/gateway/build/libs/*.jar app.jar

# Expose the application port
EXPOSE 8080

# Run the application as the created user
USER appuser

# Start the application
ENTRYPOINT ["java", "-jar", "app.jar"]

28 changes: 28 additions & 0 deletions gateway/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management'
id 'java'
}

springBoot {
mainClass = 'com.ofds.gateway.GatewayApplication'
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2022.0.3"
}
}

12 changes: 12 additions & 0 deletions gateway/src/main/java/com/odfs/gateway/GatewayApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ofds.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}

22 changes: 22 additions & 0 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
server:
port: 8080

spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: order-service
uri: http://order-service:8081
predicates:
- Path=/api/orders/**
- id: restaurant-service
uri: http://restaurant-service:8082
predicates:
- Path=/api/restaurants/**
- id: delivery-service
uri: http://delivery-service:8083
predicates:
- Path=/api/deliveries/**

20 changes: 20 additions & 0 deletions scripts/delivery.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

# create new delivery
curl -X POST http://localhost:8080/api/deliveries \
-H "Content-Type: application/json" \
-d '{"orderId": 1, "driverName": "John Doe", "status": "Pending"}'


# list all deliveries
curl http://localhost:8080/api/deliveries

# update delivery status
curl -X PUT http://localhost:8080/api/deliveries/1/status \
-H "Content-Type: application/json" \
-d '"Delivered"'

# get specific delivery by id
curl http://localhost:8080/api/deliveries/1


22 changes: 22 additions & 0 deletions scripts/orders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# create new order
curl -X POST http://localhost:8080/api/orders \
-H "Content-Type: application/json" \
-d '{"customerName": "John Doe", "dishName": "Margherita Pizza", "status": "Pending"}'

# list all orders
curl http://localhost:8080/api/orders

# get a specific order by id
curl http://localhost:8080/api/orders/1

# update order status
curl -X PUT http://localhost:8080/api/orders/1/status \
-H "Content-Type: application/json" \
-d '"Delivered"'

# delete an order
curl -X DELETE http://localhost:8080/api/orders/1


16 changes: 16 additions & 0 deletions scripts/restaurant.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

# create new restaurant
curl -X POST http://localhost:8080/api/restaurants \
-H "Content-Type: application/json" \
-d '{"name": "Pizza Palace", "location": "Cape Town", "cuisineType": "Italian"}'


#list all restaurants
curl http://localhost:8080/api/restaurants


# get a restaurant by id
curl http://localhost:8080/api/restaurants/1


36 changes: 36 additions & 0 deletions services/delivery-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Stage 1: Build the application
FROM gradle:8.1-jdk17 AS builder

# Set the working directory
WORKDIR /app

# Copy the root project files
COPY ../../settings.gradle ../../build.gradle ./

# Copy the entire project
COPY ../../ .

# Build the application using Gradle
RUN gradle :services:delivery-service:clean :services:delivery-service:bootJar -x test

# Stage 2: Create a lightweight image to run the application
FROM openjdk:17-jdk-slim

# Create a user to run the application
RUN useradd -m appuser

# Set the working directory
WORKDIR /app

# Copy the built JAR file from the build stage
COPY --from=builder /app/services/delivery-service/build/libs/*.jar app.jar

# Expose the application port
EXPOSE 8083

# Run the application as the created user
USER appuser

# Start the application
ENTRYPOINT ["java", "-jar", "app.jar"]

26 changes: 26 additions & 0 deletions services/delivery-service/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management'
id 'java'
}

springBoot {
mainClass = 'com.ofds.delivery.DeliveryServiceApplication'
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.postgresql:postgresql'

compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ofds.delivery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DeliveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DeliveryServiceApplication.class, args);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.ofds.delivery.controller;

import com.ofds.delivery.model.Delivery;
import com.ofds.delivery.repository.DeliveryRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/deliveries")
public class DeliveryController {

private final DeliveryRepository deliveryRepository;

public DeliveryController(DeliveryRepository deliveryRepository) {
this.deliveryRepository = deliveryRepository;
}

@PostMapping
public Delivery createDelivery(@RequestBody Delivery delivery) {
delivery.setStatus("Pending");
return deliveryRepository.save(delivery);
}

@GetMapping
public List<Delivery> listDeliveries() {
return deliveryRepository.findAll();
}

@PutMapping("/{id}/status")
public Delivery updateStatus(@PathVariable Long id, @RequestBody String status) {
return deliveryRepository.findById(id).map(delivery -> {
delivery.setStatus(status);
return deliveryRepository.save(delivery);
}).orElse(null);
}
}

Loading