Initial implementation of BFF, search-api integration and eureka
This commit is contained in:
112
main-app/pom.xml
Normal file
112
main-app/pom.xml
Normal file
@ -0,0 +1,112 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.5.5</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.backend.vue.bff.service</groupId>
|
||||
<artifactId>anyame-vue-bff</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>anyame-vue-bff</name>
|
||||
<description>BFF for Vue frontend</description>
|
||||
<url />
|
||||
<licenses>
|
||||
<license />
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer />
|
||||
</developers>
|
||||
<scm>
|
||||
<connection />
|
||||
<developerConnection />
|
||||
<tag />
|
||||
<url />
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
|
||||
<retrofit.version>3.0.0</retrofit.version>
|
||||
<spring-dotenv.version>4.0.0</spring-dotenv.version>
|
||||
<springdoc-openapi-starter.version>2.8.9</springdoc-openapi-starter.version>
|
||||
<spring-cloud.version>2025.0.0</spring-cloud.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-jackson</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.paulschwarz</groupId>
|
||||
<artifactId>spring-dotenv</artifactId>
|
||||
<version>${spring-dotenv.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>${springdoc-openapi-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.backend.vue.bff.service</groupId>
|
||||
<artifactId>anyame-kodik-search-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,17 @@
|
||||
package com.backend.vue.bff.service;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCaching
|
||||
@EnableEurekaServer
|
||||
public class AnyameVueBffApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AnyameVueBffApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package com.backend.vue.bff.service.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import com.backend.vue.bff.service.exception.ServiceUnavailableException;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
public static final Logger logger = LoggerFactory.getLogger("GlobalExceptionHandler");
|
||||
|
||||
@ExceptionHandler({ UnsatisfiedDependencyException.class, BeanCreationException.class,
|
||||
ServiceUnavailableException.class })
|
||||
public ResponseEntity<ErrorResponse> handleServiceUnavailable(ServiceUnavailableException e) {
|
||||
ErrorResponse error = new ErrorResponse(
|
||||
"SERVICE_UNAVAILABLE",
|
||||
e.getMessage(),
|
||||
HttpStatus.SERVICE_UNAVAILABLE.value());
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(error);
|
||||
}
|
||||
|
||||
@ExceptionHandler(IOException.class)
|
||||
public ResponseEntity<ErrorResponse> handleIOException(IOException e) {
|
||||
ErrorResponse error = new ErrorResponse(
|
||||
"API_CALL_FAILED",
|
||||
"Failed to communicate with external service: " + e.getMessage(),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception e) {
|
||||
logger.error("Exception type: {}", e.getClass().getSimpleName());
|
||||
logger.error("Exception message: {}", e.getMessage());
|
||||
|
||||
Throwable cause = e.getCause();
|
||||
while (cause != null) {
|
||||
logger.error("Cause type: {}", cause.getClass().getSimpleName());
|
||||
if (cause instanceof ServiceUnavailableException) {
|
||||
return handleServiceUnavailable((ServiceUnavailableException) cause);
|
||||
}
|
||||
cause = cause.getCause();
|
||||
}
|
||||
|
||||
ErrorResponse error = new ErrorResponse(
|
||||
"INTERNAL_ERROR",
|
||||
"An unexpected error occurred",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
|
||||
}
|
||||
|
||||
public static class ErrorResponse {
|
||||
private String code;
|
||||
private String message;
|
||||
private int status;
|
||||
private long timestamp;
|
||||
|
||||
public ErrorResponse(String code, String message, int status) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.status = status;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
// Getters and setters
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package com.backend.vue.bff.service.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import com.backend.search.kodik.ApiClient;
|
||||
import com.backend.search.kodik.JSON;
|
||||
import com.backend.search.kodik.api.SearchControllerApi;
|
||||
import com.backend.vue.bff.service.exception.ServiceUnavailableException;
|
||||
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||
|
||||
@Configuration
|
||||
@EnableDiscoveryClient
|
||||
public class SearchApiClientConfiguration {
|
||||
|
||||
private final DiscoveryClient discoveryClient;
|
||||
|
||||
public SearchApiClientConfiguration(DiscoveryClient discoveryClient) {
|
||||
this.discoveryClient = discoveryClient;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
public FactoryBean<ApiClient> apiClientFactory() {
|
||||
return new ApiClientFactoryBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
public SearchControllerApi searchApi(ApiClient apiClient) {
|
||||
return apiClient.createService(SearchControllerApi.class);
|
||||
}
|
||||
|
||||
private class ApiClientFactoryBean implements FactoryBean<ApiClient> {
|
||||
@Override
|
||||
public ApiClient getObject() throws Exception {
|
||||
ApiClient client = new ApiClient();
|
||||
|
||||
String baseUrl = resolveServiceUrl("ANYAME-KODIK-SEARCH-BACKEND");
|
||||
|
||||
JSON json = new JSON();
|
||||
|
||||
Retrofit.Builder adapterBuilder = new Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(JacksonConverterFactory.create(json.getMapper()));
|
||||
|
||||
client.setAdapterBuilder(adapterBuilder);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return ApiClient.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private String resolveServiceUrl(String serviceName) {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
||||
|
||||
if (instances.isEmpty()) {
|
||||
throw new ServiceUnavailableException();
|
||||
}
|
||||
|
||||
ServiceInstance instance = instances.get(0);
|
||||
String baseUrl = instance.getUri().toString();
|
||||
|
||||
if (!baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl + "/";
|
||||
}
|
||||
|
||||
return baseUrl;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.backend.vue.bff.service.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.backend.search.kodik.api.SearchControllerApi;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
private final ObjectProvider<SearchControllerApi> controllerApiFactory;
|
||||
|
||||
public TestController(ObjectProvider<SearchControllerApi> controllerApiFactory) {
|
||||
this.controllerApiFactory = controllerApiFactory;
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public String test() throws IOException {
|
||||
SearchControllerApi api = controllerApiFactory.getObject();
|
||||
return Integer.toString(api.search("Шарлотта").execute().body().getResults().size());
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.backend.vue.bff.service.exception;
|
||||
|
||||
public class ServiceUnavailableException extends RuntimeException {
|
||||
public ServiceUnavailableException() {
|
||||
super("Service Unavailable");
|
||||
}
|
||||
}
|
2
main-app/src/main/resources/application.properties
Normal file
2
main-app/src/main/resources/application.properties
Normal file
@ -0,0 +1,2 @@
|
||||
spring.application.name=anyame-vue-bff
|
||||
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka
|
@ -0,0 +1,13 @@
|
||||
package com.backend.vue.bff.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class AnyameVueBffApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user