first commit

This commit is contained in:
jerryjzhang
2023-06-12 18:44:01 +08:00
commit dc4fc69b57
879 changed files with 573090 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package com.tencent.supersonic.advice;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.supersonic.common.result.ResultData;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@Slf4j
@RestControllerAdvice(annotations = RestController.class)
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
@Autowired
private ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return true;
}
@SneakyThrows
@Override
public Object beforeBodyWrite(Object result, MethodParameter methodParameter, MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
if (result instanceof String) {
return objectMapper.writeValueAsString(ResultData.success(result));
}
if (result instanceof ResultData) {
return result;
}
return ResultData.success(result);
}
}

View File

@@ -0,0 +1,49 @@
package com.tencent.supersonic.advice;
import com.tencent.supersonic.common.exception.AccessException;
import com.tencent.supersonic.common.exception.CommonException;
import com.tencent.supersonic.common.exception.InvalidPermissionException;
import com.tencent.supersonic.common.result.ResultData;
import com.tencent.supersonic.common.result.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {
/**
* default global exception handler
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
public ResultData<String> exception(Exception e) {
log.error("default global exception", e);
return ResultData.fail(ReturnCode.SYSTEM_ERROR.getCode(), e.getMessage());
}
@ExceptionHandler(AccessException.class)
@ResponseStatus(HttpStatus.OK)
public ResultData<String> accessException(Exception e) {
log.error("default global exception", e);
return ResultData.fail(ReturnCode.ACCESS_ERROR.getCode(), e.getMessage());
}
@ExceptionHandler(InvalidPermissionException.class)
@ResponseStatus(HttpStatus.OK)
public ResultData<String> invalidPermissionException(Exception e) {
log.error("default global exception", e);
return ResultData.fail(ReturnCode.INVALID_PERMISSION.getCode(), e.getMessage());
}
@ExceptionHandler(CommonException.class)
@ResponseStatus(HttpStatus.OK)
public ResultData<String> commonException(CommonException e) {
log.error("default global exception", e);
return ResultData.fail(e.getCode(), e.getMessage());
}
}

View File

@@ -0,0 +1,14 @@
package com.tencent.supersonic.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@@ -0,0 +1,34 @@
package com.tencent.supersonic.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
public static String getActiveProfile() {
String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
if (profiles != null && profiles.length > 0) {
return profiles[0];
}
return "dev";
}
}

View File

@@ -0,0 +1,20 @@
package com.tencent.supersonic.web;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
/**
* error page config
*/
@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/webapp/index.html");
registry.addErrorPages(error404Page);
}
}

View File

@@ -0,0 +1,21 @@
package com.tencent.supersonic.web;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webapp/**")
.addResourceLocations("classpath:/webapp/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:webapp/index.html");
}
}