Initial implementation of BFF, search-api integration and eureka
This commit is contained in:
@ -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