Skip to content

Commit

Permalink
Merge pull request #47 from nawaphonOHM/messaging/graphql
Browse files Browse the repository at this point in the history
Messaging/graphql
  • Loading branch information
nawaphonOHM authored Mar 16, 2024
2 parents b928dea + 916efc8 commit 77c3092
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/docker-build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ jobs:
file: .messaging/event_sourcing/producer/Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/microservices-lab:messaging.event_sourcing.producer

- name: Build graphql image
uses: docker/build-push-action@v5
with:
context: ./messaging/graphql/gateway
file: ./messaging/graphql/gatewayDockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/microservices-lab:messaging.graphql.gateway
25 changes: 25 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,28 @@ configure(subprojects.findAll { it.name == "private_table_per_service@customer_s
useJUnitPlatform()
}
}

configure(subprojects.findAll { it.name == "messaging@graphql"}) {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

java {
sourceCompatibility = '11'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-graphql'


testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.graphql:spring-graphql-test'

}
}
19 changes: 19 additions & 0 deletions messaging/graphql/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:

gateway:
image: nawaphon2539/microservices-lab:messaging.graphql.gateway
env_file:
- gateway/gateway-vars.env
ports:
- "1001:8080"
networks:
default:
ipv4_address: 192.168.1.2


networks:
default:
ipam:
config:
- subnet: 192.168.1.0/29
gateway: 192.168.1.1
23 changes: 23 additions & 0 deletions messaging/graphql/gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM openjdk:11.0.16-jre


RUN useradd --create-home gateway

COPY ./build/libs/gateway-0.0.1.jar /home/gateway/
COPY ./entrypoint.sh /home/gateway/

RUN chown gateway /home/gateway/entrypoint.sh && \
chmod 500 /home/gateway/entrypoint.sh && \
chown gateway /home/gateway/gateway-0.0.1.jar && \
chmod 500 /home/gateway/gateway-0.0.1.jar


EXPOSE 8080

USER gateway

WORKDIR /home/gateway/

ENTRYPOINT ["./entrypoint.sh"]

LABEL authors="nawaphon"
5 changes: 5 additions & 0 deletions messaging/graphql/gateway/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
group = 'nawaphon.microservices.gateway'
version = '0.0.1'


archivesBaseName = 'gateway'
24 changes: 24 additions & 0 deletions messaging/graphql/gateway/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

main() {

export ERROR=0

mandatoryEnvCheck

if [ "$ERROR" -eq 1 ]; then
exit 1
fi

unset ERROR

java -jar /home/gateway/gateway-0.0.1.jar || exit 1
}

mandatoryEnvCheck() {
ERROR=0
}



main
2 changes: 2 additions & 0 deletions messaging/graphql/gateway/gateway-vars.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TZ=Asia/Bangkok
LOG_LEVEL=DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nawaphon.microservices.event_sourcing.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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package nawaphon.microservices.event_sourcing.gateway.components;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import nawaphon.microservices.event_sourcing.gateway.pojo.Customer;
import nawaphon.microservices.event_sourcing.gateway.pojo.CustomerDetail;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
public class FakeDatabaseComponent {
private static final Logger logger = LogManager.getLogger(FakeDatabaseComponent.class);

private final List<Customer> customers = new ArrayList<>();
private final List<CustomerDetail> customerDetails = new ArrayList<>();

private final ObjectMapper objectMapper;

public FakeDatabaseComponent(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public List<Customer> getCustomers() {
return customers;
}

public List<CustomerDetail> getCustomerDetails() {
return customerDetails;
}

@PostConstruct
public void makeData() {
Customer customerFake;
CustomerDetail customerDetailFake;

{
customerFake = new Customer(UUID.randomUUID(), UUID.randomUUID());
customerDetailFake = new CustomerDetail(customerFake.getDetailsId(), "John", "Doe");
customers.add(customerFake);
customerDetails.add(customerDetailFake);
try {
logger.info("Customer and CustomerDetail added: {}", objectMapper.writeValueAsString(customerFake));
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to serialize value: ", e);
}
}

{
customerFake = new Customer(UUID.randomUUID(), UUID.randomUUID());
customerDetailFake = new CustomerDetail(customerFake.getDetailsId(), "Jane", "Smith");
customers.add(customerFake);
customerDetails.add(customerDetailFake);
try {
logger.info("Customer and CustomerDetail added: {}", objectMapper.writeValueAsString(customerFake));
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to serialize value: ", e);
}
}

{
customerFake = new Customer(UUID.randomUUID(), UUID.randomUUID());
customerDetailFake = new CustomerDetail(customerFake.getDetailsId(), "Bob", "Johnson");
customers.add(customerFake);
customerDetails.add(customerDetailFake);
try {
logger.info("Customer and CustomerDetail added: {}", objectMapper.writeValueAsString(customerFake));
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to serialize value:", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nawaphon.microservices.event_sourcing.gateway.controllers;

import nawaphon.microservices.event_sourcing.gateway.components.FakeDatabaseComponent;
import nawaphon.microservices.event_sourcing.gateway.pojo.Customer;
import nawaphon.microservices.event_sourcing.gateway.pojo.CustomerDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class MainController {

private static final Logger logger = LoggerFactory.getLogger(MainController.class);

private final FakeDatabaseComponent fakeDatabaseComponent;

public MainController(final FakeDatabaseComponent fakeDatabaseComponent) {
this.fakeDatabaseComponent = fakeDatabaseComponent;
}

@QueryMapping("findCustomerById")
public Customer getCustomer(@Argument final UUID id) {
logger.info("Getting customer with id: {}", id);
return fakeDatabaseComponent.getCustomers().stream().filter((var1) -> var1.getId().compareTo(id) == 0)
.findFirst().orElse(new Customer());
}

@SchemaMapping("details")
public CustomerDetail getCustomerDetails(final Customer customer) {
logger.info("Getting customer details for customer with id: {}", customer.getId());
return fakeDatabaseComponent.getCustomerDetails().stream()
.filter((var1) -> var1.getCustomerId().compareTo(customer.getDetailsId()) == 0)
.findFirst().orElse(new CustomerDetail());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package nawaphon.microservices.event_sourcing.gateway.pojo;

import java.util.UUID;

public class Customer {

private final UUID id;

private final UUID detailsId;


public Customer(final UUID id, final UUID detailsId) {
this.id = id;
this.detailsId = detailsId;
}

public Customer() {
this(null, null);
}

public UUID getId() {
return id;
}

public UUID getDetailsId() {
return detailsId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nawaphon.microservices.event_sourcing.gateway.pojo;

import java.util.UUID;

public class CustomerDetail {

private final UUID customerId;

private final String firstName;

private final String lastName;


public CustomerDetail(final UUID customerId, final String firstName, final String lastName) {
this.customerId = customerId;
this.firstName = firstName;
this.lastName = lastName;
}

public CustomerDetail() {
this(null, null, null);
}

public UUID getCustomerId() {
return customerId;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nawaphon.microservices.event_sourcing.gateway.pojo;

public class ResponseMessage<A> {

private final Number code;
private final String message;
private final A results;

public ResponseMessage(final Number code, final String message, final A results) {
this.code = code;
this.message = message;
this.results = results;
}

public String getMessage() {
return message;
}

public A getResults() {
return results;
}

public Number getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
server.servlet.context-path=/gateway
logging.level.root=${LOG_LEVEL:INFO}

spring.graphql.graphiql.enabled=true


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Customer {
id: ID
details: CustomerDetail
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type CustomerDetail {
customerId: ID
firstName: String
lastName: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema:
- ./*.graphqls
documents: '**/*.graphql'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Query {
findCustomerById(id: ID): Customer

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nawaphon.microservices.event_sourcing.consumer;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class EventSourcingServiceConsumerApplicationTests {

@Test
void contextLoads() {
}

}
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ project(":messaging@event_sourcing@producer").projectDir = file("./messaging/eve

include("messaging@event_sourcing@consumer")
project(":messaging@event_sourcing@consumer").projectDir = file("./messaging/event_sourcing/consumer")

include("messaging@graphql")
project(":messaging@graphql").projectDir = file("messaging/graphql/gateway")

0 comments on commit 77c3092

Please sign in to comment.