[*] 日常优化

This commit is contained in:
acgist
2023-07-16 09:47:09 +08:00
parent e7667f5d10
commit b5a2ad06b4
21 changed files with 539 additions and 608 deletions

View File

@@ -18,14 +18,14 @@ import lombok.Setter;
public class ScriptProperties {
@Schema(title = "是否启用", description = "是否启用")
private Boolean enabled;
private Boolean enabled;
@Schema(title = "重启系统", description = "重启系统")
private String systemReboot;
private String systemReboot;
@Schema(title = "关闭系统", description = "关闭系统")
private String systemShutdown;
private String systemShutdown;
@Schema(title = "重启平台", description = "重启平台")
private String platformReboot;
private String platformReboot;
@Schema(title = "关闭平台", description = "关闭平台")
private String platformShutdown;
private String platformShutdown;
}

View File

@@ -18,7 +18,7 @@ import lombok.Setter;
public class SocketProperties {
/**
* 密策略
* 密策略
*
* @author acgist
*/
@@ -44,30 +44,30 @@ public class SocketProperties {
}
@Schema(title = "是否启用", description = "是否启用")
private Boolean enabled;
private Boolean enabled;
@Schema(title = "监听地址", description = "监听地址")
private String host;
private String host;
@Schema(title = "监听端口", description = "监听端口")
private Integer port;
private Integer port;
@Schema(title = "加密策略", description = "加密策略")
private Encrypt encrypt;
@Schema(title = "加密密钥", description = "加密密钥:为空自动生成")
private String encryptSecret;
@Schema(title = "超时时间", description = "超时时间")
private Long timeout;
private Long timeout;
@Schema(title = "队列长度", description = "队列长度")
private Integer queueSize;
private Integer queueSize;
@Schema(title = "最小线程数量", description = "最小线程数量")
private Integer minThread;
private Integer minThread;
@Schema(title = "最大线程数量", description = "最大线程数量")
private Integer maxThread;
private Integer maxThread;
@Schema(title = "线程池的前缀", description = "线程池的前缀")
private String threadNamePrefix;
private String threadNamePrefix;
@Schema(title = "线程销毁时间", description = "线程销毁时间")
private Long keepAliveTime;
private Long keepAliveTime;
@Schema(title = "缓冲大小", description = "缓冲大小")
private Integer bufferSize;
private Integer bufferSize;
@Schema(title = "最大缓冲大小", description = "最大缓冲大小")
private Integer maxBufferSize;
}

View File

@@ -16,16 +16,16 @@ import lombok.Setter;
@Schema(title = "平台配置", description = "平台配置")
@ConfigurationProperties(prefix = "taoyao")
public class TaoyaoProperties {
@Schema(title = "项目地址", description = "项目地址")
private String url;
@Schema(title = "项目名称", description = "项目名称")
private String name;
@Schema(title = "项目版本", description = "项目版本")
private String version;
@Schema(title = "项目描述", description = "项目描述")
private String description;
@Schema(title = "超时时间", description = "超时时间")
private Long timeout;
@Schema(title = "项目地址", description = "项目地址")
private String url;
@Schema(title = "项目名称", description = "项目名称")
private String name;
@Schema(title = "项目版本", description = "项目版本")
private String version;
@Schema(title = "项目描述", description = "项目描述")
private String description;
@Schema(title = "超时时间", description = "超时时间")
private Long timeout;
}

View File

@@ -1,7 +1,6 @@
package com.acgist.taoyao.boot.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -22,33 +21,31 @@ import lombok.Setter;
@ConfigurationProperties(prefix = "taoyao.webrtc")
public class WebrtcProperties {
@Schema(title = "是否加密", description = "是否加密")
@Schema(title = "是否终端加密", description = "是否终端加密")
private Boolean encrypt;
@Schema(title = "STUN服务器", description = "STUN服务器")
private WebrtcStunProperties[] stun;
@Schema(title = "TURN服务器", description = "TURN服务器")
private WebrtcTurnProperties[] turn;
@Schema(title = "STUN服务器", description = "STUN服务器")
private WebrtcStunProperties[] stun;
@Schema(title = "TURN服务器", description = "TURN服务器")
private WebrtcTurnProperties[] turn;
@Schema(title = "IceServers", description = "IceServers")
public List<Map<String, String>> getIceServers() {
final List<Map<String, String>> list = new ArrayList<>();
if(this.stun != null) {
for (WebrtcStunProperties stun : this.stun) {
final Map<String, String> map = new HashMap<>();
map.put(Constant.URLS, stun.getAddress());
list.add(map);
list.add(Map.of(Constant.URLS, stun.getAddress()));
}
}
if(this.turn != null) {
for (WebrtcTurnProperties turn : this.turn) {
final Map<String, String> map = new HashMap<>();
map.put(Constant.URLS, turn.getAddress());
map.put(Constant.USERNAME, turn.getUsername());
map.put(Constant.CREDENTIAL, turn.getPassword());
list.add(map);
list.add(Map.of(
Constant.URLS, turn.getAddress(),
Constant.USERNAME, turn.getUsername(),
Constant.CREDENTIAL, turn.getPassword()
));
}
}
return list;
}
}

View File

@@ -1,7 +1,6 @@
package com.acgist.taoyao.boot.configuration;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
@@ -45,8 +44,8 @@ import org.springframework.web.servlet.NoHandlerFoundException;
import com.acgist.taoyao.boot.config.FfmpegProperties;
import com.acgist.taoyao.boot.config.IdProperties;
import com.acgist.taoyao.boot.config.RewriteProperties;
import com.acgist.taoyao.boot.config.MediaProperties;
import com.acgist.taoyao.boot.config.RewriteProperties;
import com.acgist.taoyao.boot.config.ScriptProperties;
import com.acgist.taoyao.boot.config.SecurityProperties;
import com.acgist.taoyao.boot.config.SocketProperties;
@@ -70,6 +69,7 @@ import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
@@ -95,18 +95,13 @@ import lombok.extern.slf4j.Slf4j;
RewriteProperties.class,
SecurityProperties.class,
})
@RequiredArgsConstructor
public class BootAutoConfiguration {
private final FfmpegProperties ffmpegProperties;
private final TaoyaoProperties taoyaoProperties;
private final FfmpegProperties ffmpegProperties;
private final TaoyaoProperties taoyaoProperties;
private final ApplicationContext applicationContext;
public BootAutoConfiguration(FfmpegProperties ffmpegProperties, TaoyaoProperties taoyaoProperties, ApplicationContext applicationContext) {
this.ffmpegProperties = ffmpegProperties;
this.taoyaoProperties = taoyaoProperties;
this.applicationContext = applicationContext;
}
@Bean
@ConditionalOnMissingBean
public IdService idService(IdProperties idProperties) {
@@ -148,8 +143,8 @@ public class BootAutoConfiguration {
@Bean
public CommandLineRunner successCommandLineRunner(
TaskExecutor taskExecutor,
TaoyaoProperties taoyaoProperties,
TaskExecutor taskExecutor,
TaoyaoProperties taoyaoProperties,
RewriteProperties rewriteProperties
) {
return new OrderedCommandLineRunner() {
@@ -164,10 +159,9 @@ public class BootAutoConfiguration {
@PostConstruct
public void init() {
final Runtime runtime = Runtime.getRuntime();
final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
final String maxMemory = FileUtils.formatSize(runtime.maxMemory());
final String freeMemory = FileUtils.formatSize(runtime.freeMemory());
final Runtime runtime = Runtime.getRuntime();
final String maxMemory = FileUtils.formatSize(runtime.maxMemory());
final String freeMemory = FileUtils.formatSize(runtime.freeMemory());
final String totalMemory = FileUtils.formatSize(runtime.totalMemory());
log.info("操作系统架构:{}", System.getProperty("os.arch"));
log.info("操作系统名称:{}", System.getProperty("os.name"));
@@ -178,7 +172,7 @@ public class BootAutoConfiguration {
log.info("Java库目录{}", System.getProperty("java.library.path"));
log.info("ClassPath{}", System.getProperty("java.class.path"));
log.info("虚拟机名称:{}", System.getProperty("java.vm.name"));
log.info("虚拟机参数:{}", runtimeMXBean.getInputArguments().stream().collect(Collectors.joining(" ")));
log.info("虚拟机参数:{}", ManagementFactory.getRuntimeMXBean().getInputArguments().stream().collect(Collectors.joining(" ")));
log.info("虚拟机最大内存:{}", maxMemory);
log.info("虚拟机空闲内存:{}", freeMemory);
log.info("虚拟机已用内存:{}", totalMemory);

View File

@@ -19,6 +19,7 @@ import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
/**
* 文档自动配置
@@ -28,6 +29,7 @@ import io.swagger.v3.oas.models.security.SecurityScheme;
@Profile({ "dev", "sit" })
@AutoConfiguration
@ConditionalOnClass(OpenAPI.class)
@RequiredArgsConstructor
public class SpringDocAutoConfiguration {
@Value("${server.port:8888}")
@@ -37,95 +39,91 @@ public class SpringDocAutoConfiguration {
private final TaoyaoProperties taoyaoProperties;
public SpringDocAutoConfiguration(TaoyaoProperties taoyaoProperties) {
this.taoyaoProperties = taoyaoProperties;
@Bean
public GroupedOpenApi signalApi() {
return GroupedOpenApi.builder()
.group("signal")
.displayName("信令")
.packagesToScan("com.acgist.taoyao.signal")
.build();
}
@Bean
public GroupedOpenApi signalApi() {
return GroupedOpenApi.builder()
.group("signal")
.displayName("信令")
.packagesToScan("com.acgist.taoyao.signal")
.build();
}
@Bean
public GroupedOpenApi taoyaoApi() {
return GroupedOpenApi.builder()
.group("taoyao")
.displayName("桃夭")
.packagesToScan("com.acgist.taoyao")
.build();
}
@Bean
@ConditionalOnMissingBean
public OpenAPI openAPI() {
return new OpenAPI()
@Bean
public GroupedOpenApi taoyaoApi() {
return GroupedOpenApi.builder()
.group("taoyao")
.displayName("桃夭")
.packagesToScan("com.acgist.taoyao")
.build();
}
@Bean
@ConditionalOnMissingBean
public OpenAPI openAPI() {
return new OpenAPI()
// .servers(null)
.info(this.buildInfo())
.security(this.buildSecurity())
.components(this.buildComponents());
}
.info(this.buildInfo())
.security(this.buildSecurity())
.components(this.buildComponents());
}
/**
* @return 基本信息
*/
private Info buildInfo() {
return new Info()
.contact(this.buildContact())
.license(this.buildLicense())
.title(this.taoyaoProperties.getName())
.version(this.taoyaoProperties.getVersion())
.description(this.taoyaoProperties.getDescription());
}
/**
* @return 联系方式
*/
private Contact buildContact() {
return new Contact()
.url(this.taoyaoProperties.getUrl())
.name(this.taoyaoProperties.getName());
}
/**
* @return 基本信息
*/
private Info buildInfo() {
return new Info()
.contact(this.buildContact())
.license(this.buildLicense())
.title(this.taoyaoProperties.getName())
.version(this.taoyaoProperties.getVersion())
.description(this.taoyaoProperties.getDescription());
}
/**
* @return 联系方式
*/
private Contact buildContact() {
return new Contact()
.url(this.taoyaoProperties.getUrl())
.name(this.taoyaoProperties.getName());
}
/**
* @return 开源信息
*/
private License buildLicense() {
return new License()
.url("https://www.apache.org/licenses/LICENSE-2.0.html")
.name("Apache 2.0");
}
/**
* @return 开源信息
*/
private License buildLicense() {
return new License()
.url("https://www.apache.org/licenses/LICENSE-2.0.html")
.name("Apache 2.0");
}
/**
* @return 安全授权
*/
private List<SecurityRequirement> buildSecurity() {
return List.of(
new SecurityRequirement()
.addList(this.scheme)
);
}
/**
* @return 安全授权
*/
private Components buildComponents() {
return new Components()
.addSecuritySchemes(this.scheme, this.buildSecurityScheme());
}
/**
* @return 授权模式
*/
private SecurityScheme buildSecurityScheme() {
return new SecurityScheme()
.name(this.scheme)
.scheme(this.scheme)
.in(SecurityScheme.In.HEADER)
.type(SecurityScheme.Type.HTTP);
}
/**
* @return 安全授权
*/
private List<SecurityRequirement> buildSecurity() {
return List.of(
new SecurityRequirement()
.addList(this.scheme)
);
}
/**
* @return 安全授权
*/
private Components buildComponents() {
return new Components()
.addSecuritySchemes(this.scheme, this.buildSecurityScheme());
}
/**
* @return 授权模式
*/
private SecurityScheme buildSecurityScheme() {
return new SecurityScheme()
.name(this.scheme)
.scheme(this.scheme)
.in(SecurityScheme.In.HEADER)
.type(SecurityScheme.Type.HTTP);
}
}

View File

@@ -7,41 +7,32 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.acgist.taoyao.boot.interceptor.InterceptorAdapter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* MVC自动配置
*
* TODOErrorPageRegistrar拦截400错误https://localhost:8888/?\%3E%3C
*
* @author acgist
*/
@Slf4j
@AutoConfiguration
@RequiredArgsConstructor
public class WebMvcConfigurerAutoConfiguration implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
public WebMvcConfigurerAutoConfiguration(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
this.applicationContext.getBeansOfType(InterceptorAdapter.class).entrySet().stream()
.sorted((a, z) -> a.getValue().compareTo(z.getValue()))
.forEach(entry -> {
final InterceptorAdapter value = entry.getValue();
if(log.isDebugEnabled()) {
log.debug("注册MVC拦截器{} - {}", String.format("%-32s", entry.getKey()), value.name());
}
registry.addInterceptor(value).addPathPatterns(value.pathPattern());
});
}
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// WebMvcConfigurer.super.addCorsMappings(registry);
// }
private final ApplicationContext applicationContext;
@Override
public void addInterceptors(InterceptorRegistry registry) {
this.applicationContext.getBeansOfType(InterceptorAdapter.class).entrySet().stream()
.sorted((a, z) -> a.getValue().compareTo(z.getValue()))
.forEach(entry -> {
final InterceptorAdapter value = entry.getValue();
if(log.isDebugEnabled()) {
log.debug("注册MVC拦截器{} - {}", String.format("%-32s", entry.getKey()), value.name());
}
registry.addInterceptor(value).addPathPatterns(value.pathPattern());
});
}
}

View File

@@ -14,6 +14,8 @@ import jakarta.servlet.http.HttpServletResponse;
/**
* 统一异常处理
*
* 不能处理https://localhost:8888/?\%3E%3C
*
* @author acgist
*/
@Tag(name = "统一异常处理", description = "全局统一异常处理")
@@ -21,9 +23,9 @@ import jakarta.servlet.http.HttpServletResponse;
public class TaoyaoControllerAdvice {
@Operation(summary = "统一异常处理", description = "全局统一异常处理")
@ExceptionHandler(Exception.class)
public Message exception(Exception e, HttpServletRequest request, HttpServletResponse response) {
return ErrorUtils.message(e, request, response);
}
@ExceptionHandler(Exception.class)
public Message exception(Exception e, HttpServletRequest request, HttpServletResponse response) {
return ErrorUtils.message(e, request, response);
}
}

View File

@@ -21,10 +21,10 @@ import jakarta.servlet.http.HttpServletResponse;
@RestController
public class TaoyaoErrorController implements ErrorController {
@Operation(summary = "统一错误页面", description = "全局统一错误页面")
@RequestMapping(value = ErrorUtils.ERROR_PATH)
public Message index(HttpServletRequest request, HttpServletResponse response) {
return ErrorUtils.message(request, response);
}
@Operation(summary = "统一错误页面", description = "全局统一错误页面")
@RequestMapping(value = ErrorUtils.ERROR_PATH)
public Message index(HttpServletRequest request, HttpServletResponse response) {
return ErrorUtils.message(request, response);
}
}

View File

@@ -10,19 +10,19 @@ import org.springframework.web.servlet.HandlerInterceptor;
*/
public abstract class InterceptorAdapter implements Ordered, Comparable<InterceptorAdapter>, HandlerInterceptor {
/**
* @return 名称
*/
public abstract String name();
/**
* @return 拦截地址
*/
public abstract String[] pathPattern();
@Override
public int compareTo(InterceptorAdapter target) {
return Integer.compare(this.getOrder(), target.getOrder());
}
/**
* @return 名称
*/
public abstract String name();
/**
* @return 拦截地址
*/
public abstract String[] pathPattern();
@Override
public int compareTo(InterceptorAdapter target) {
return Integer.compare(this.getOrder(), target.getOrder());
}
}

View File

@@ -22,27 +22,27 @@ import lombok.Setter;
@AllArgsConstructor
public class Header implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
/**
* 消息版本
*/
@Schema(title = "消息版本", description = "消息版本")
private String v;
/**
* 消息标识
*/
@Schema(title = "消息标识", description = "消息标识")
private Long id;
/**
* 信令标识
*/
@Schema(title = "信令标识", description = "信令标识")
private String signal;
@Override
public Header clone() {
return new Header(this.v, this.id, this.signal);
}
private static final long serialVersionUID = 1L;
/**
* 消息版本
*/
@Schema(title = "消息版本", description = "消息版本")
private String v;
/**
* 消息标识
*/
@Schema(title = "消息标识", description = "消息标识")
private Long id;
/**
* 信令标识
*/
@Schema(title = "信令标识", description = "信令标识")
private String signal;
@Override
public Header clone() {
return new Header(this.v, this.id, this.signal);
}
}

View File

@@ -25,36 +25,36 @@ import lombok.Setter;
@JsonIncludeProperties(value = { "code", "message", "header", "body" })
public class Message implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
/**
* 成功标识
*/
public static final String CODE_0000 = "0000";
/**
* 状态编码
*/
@Schema(title = "状态编码", description = "状态编码")
private String code;
/**
* 状态描述
*/
@Schema(title = "状态描述", description = "状态描述")
private String message;
/**
* 消息头部
*/
@Schema(title = "消息头部", description = "消息头部")
private Header header;
/**
* 消息主体
*/
@Schema(title = "消息主体", description = "消息主体")
private Object body;
/**
* 状态编码
*/
@Schema(title = "状态编码", description = "状态编码")
private String code;
/**
* 状态描述
*/
@Schema(title = "状态描述", description = "状态描述")
private String message;
/**
* 消息头部
*/
@Schema(title = "消息头部", description = "消息头部")
private Header header;
/**
* 消息主体
*/
@Schema(title = "消息主体", description = "消息主体")
private Object body;
public Message() {
// 默认消息都是成功
// 消息默认都是成功
final MessageCode messageCode = MessageCode.CODE_0000;
this.code = messageCode.getCode();
this.message = messageCode.getMessage();
@@ -66,99 +66,99 @@ public class Message implements Cloneable, Serializable {
this.header = header;
this.body = body;
}
/**
* @return 成功消息
*/
public static final Message success() {
return Message.success(null);
}
/**
* @return 成功消息
*/
public static final Message success() {
return Message.success(null);
}
/**
* @param body 消息主体
*
* @return 成功消息
*/
public static final Message success(Object body) {
final Message message = new Message();
message.setCode(MessageCode.CODE_0000, null);
message.body = body;
return message;
}
/**
* @param body 消息主体
*
* @return 成功消息
*/
public static final Message success(Object body) {
final Message message = new Message();
message.setCode(MessageCode.CODE_0000, null);
message.body = body;
return message;
}
/**
* @return 失败消息
*/
public static final Message fail() {
return Message.fail(null, null, null);
}
/**
* @return 失败消息
*/
public static final Message fail() {
return Message.fail(null, null, null);
}
/**
* @param messageCode 状态编码
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode) {
return Message.fail(messageCode, null, null);
}
/**
* @param messageCode 状态编码
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode) {
return Message.fail(messageCode, null, null);
}
/**
* @param messageCode 状态编码
* @param body 消息主体
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode, Object body) {
return Message.fail(messageCode, null, body);
}
/**
* @param message 状态描述
*
* @return 失败消息
*/
public static final Message fail(String message) {
return Message.fail(null, message, null);
}
/**
* @param message 状态描述
* @param body 消息主体
*
* @return 失败消息
*/
public static final Message fail(String message, Object body) {
return Message.fail(null, message, body);
}
/**
* @param messageCode 状态编码
* @param message 状态描述
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode, String message) {
return Message.fail(messageCode, message, null);
}
/**
* @param messageCode 状态编码
* @param body 消息主体
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode, Object body) {
return Message.fail(messageCode, null, body);
}
/**
* @param message 状态描述
*
* @return 失败消息
*/
public static final Message fail(String message) {
return Message.fail(null, message, null);
}
/**
* @param message 状态描述
* @param body 消息主体
*
* @return 失败消息
*/
public static final Message fail(String message, Object body) {
return Message.fail(null, message, body);
}
/**
* @param messageCode 状态编码
* @param message 状态描述
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode, String message) {
return Message.fail(messageCode, message, null);
}
/**
* @param messageCode 状态编码
* @param message 状态描述
* @param body 消息主体
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode, String message, Object body) {
final Message failMessage = new Message();
failMessage.setCode(messageCode == null ? MessageCode.CODE_9999 : messageCode, message);
failMessage.body = body;
return failMessage;
}
/**
* @param messageCode 状态编码
* @param message 状态描述
* @param body 消息主体
*
* @return 失败消息
*/
public static final Message fail(MessageCode messageCode, String message, Object body) {
final Message failMessage = new Message();
failMessage.setCode(messageCode == null ? MessageCode.CODE_9999 : messageCode, message);
failMessage.body = body;
return failMessage;
}
@Override
public Message clone() {
return new Message(this.code, this.message, this.header.clone(), this.body);
}
@Override
public Message clone() {
return new Message(this.code, this.message, this.header.clone(), this.body);
}
/**
* @param messageCode 状态编码
@@ -178,23 +178,23 @@ public class Message implements Cloneable, Serializable {
this.message = StringUtils.isEmpty(message) ? messageCode.getMessage() : message;
return this;
}
/**
* 克隆消息排除消息主体
*
* @return 克隆消息
*/
public Message cloneWithoutBody() {
return new Message(this.code, this.message, this.header.clone(), null);
}
/**
* @return 消息主体
*/
/**
* 克隆消息排除消息主体
*
* @return 克隆消息
*/
public Message cloneWithoutBody() {
return new Message(this.code, this.message, this.header.clone(), null);
}
/**
* @return 消息主体
*/
@SuppressWarnings("unchecked")
public <T> T body() {
return (T) this.body;
}
return (T) this.body;
}
/**
* 注解不会自动生成
@@ -205,16 +205,9 @@ public class Message implements Cloneable, Serializable {
this.code = code;
}
/**
* @return 是否成功
*/
public boolean isSuccess() {
return CODE_0000.equals(this.code);
@Override
public String toString() {
return JSONUtils.toJSON(this);
}
@Override
public String toString() {
return JSONUtils.toJSON(this);
}
}

View File

@@ -14,78 +14,78 @@ import lombok.Getter;
*/
@Getter
public enum MessageCode {
// 成功
CODE_0000("0000", 200, "成功"),
// 1xxx
CODE_1000("1000", 404, "未知接口"),
CODE_1001("1001", 400, "上次请求没有完成"),
CODE_1002("1002", 400, "数据格式错误"),
CODE_1003("1003", 400, "验签失败"),
// 2xxx
CODE_2000("2000", 500, "服务错误"),
CODE_2001("2001", 504, "服务超时"),
// 3xxx
CODE_3400("3400", 400, "请求错误"),
CODE_3401("3401", 401, "没有授权"),
CODE_3403("3403", 403, "请求拒绝"),
CODE_3404("3404", 404, "资源失效"),
CODE_3405("3405", 405, "请求方法错误"),
CODE_3406("3406", 406, "请求不可接受"),
CODE_3415("3415", 415, "请求资源类型错误"),
CODE_3500("3500", 500, "系统异常"),
CODE_3502("3502", 502, "服务无效"),
CODE_3503("3503", 503, "服务正在维护"),
CODE_3504("3504", 504, "服务超时"),
// 9999
CODE_9999("9999", 500, "未知错误");
/**
* HTTP Status前缀
*/
private static final String HTTP_STATUS = "3";
/**
* 状态编码
*/
private final String code;
/**
* 状态数值HTTP Status
*/
private final Integer status;
/**
* 状态描述
*/
private final String message;
// 成功
CODE_0000("0000", 200, "成功"),
// 1xxx
CODE_1000("1000", 404, "未知接口"),
CODE_1001("1001", 400, "上次请求没有完成"),
CODE_1002("1002", 400, "数据格式错误"),
CODE_1003("1003", 400, "验签失败"),
// 2xxx
CODE_2000("2000", 500, "服务错误"),
CODE_2001("2001", 504, "服务超时"),
// 3xxx
CODE_3400("3400", 400, "请求错误"),
CODE_3401("3401", 401, "没有授权"),
CODE_3403("3403", 403, "请求拒绝"),
CODE_3404("3404", 404, "资源失效"),
CODE_3405("3405", 405, "请求方法错误"),
CODE_3406("3406", 406, "请求不可接受"),
CODE_3415("3415", 415, "请求资源类型错误"),
CODE_3500("3500", 500, "系统异常"),
CODE_3502("3502", 502, "服务无效"),
CODE_3503("3503", 503, "服务正在维护"),
CODE_3504("3504", 504, "服务超时"),
// 9999
CODE_9999("9999", 500, "未知错误");
/**
* HTTP Status前缀
*/
private static final String HTTP_STATUS = "3";
/**
* 状态编码
*/
private final String code;
/**
* 状态数值HTTP Status
*/
private final Integer status;
/**
* 状态描述
*/
private final String message;
private MessageCode(String code, Integer status, String message) {
this.code = code;
this.status = status;
this.message = message;
}
private MessageCode(String code, Integer status, String message) {
this.code = code;
this.status = status;
this.message = message;
}
/**
* @param code 状态编码
*
* @return 状态编码
*/
public static final MessageCode of(String code) {
final MessageCode[] values = MessageCode.values();
for (final MessageCode value : values) {
if (value.code.equals(code)) {
return value;
}
}
return CODE_9999;
}
/**
* @param code 状态编码
*
* @return 状态编码
*/
public static final MessageCode of(String code) {
final MessageCode[] values = MessageCode.values();
for (final MessageCode value : values) {
if (value.code.equals(code)) {
return value;
}
}
return CODE_9999;
}
/**
* @param status HTTP Status
*
* @return 状态编码
*/
public static final MessageCode of(Integer status) {
return MessageCode.of(HTTP_STATUS + status);
}
/**
* @param status HTTP Status
*
* @return 状态编码
*/
public static final MessageCode of(Integer status) {
return MessageCode.of(HTTP_STATUS + status);
}
}

View File

@@ -14,67 +14,67 @@ import lombok.Getter;
@Getter
public class MessageCodeException extends RuntimeException {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
/**
* 状态编码
*/
private final MessageCode messageCode;
/**
* 状态编码
*/
private final MessageCode messageCode;
/**
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(String message) {
return of(null, null, message);
}
/**
* @param t 异常
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(Throwable t, String message) {
return of(t, null, message);
}
/**
* @param messageCode 状态编码
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(MessageCode messageCode, String message) {
return of(null, messageCode, message);
}
/**
* @param t 异常
* @param messageCode 状态编码
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(Throwable t, MessageCode messageCode, String message) {
if(messageCode == null) {
messageCode = MessageCode.CODE_9999;
}
if(StringUtils.isEmpty(message)) {
message = Objects.isNull(t) ? messageCode.getMessage() : t.getMessage();
}
return new MessageCodeException(t, messageCode, message);
}
/**
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(String message) {
return of(null, null, message);
}
/**
* @param t 异常
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(Throwable t, String message) {
return of(t, null, message);
}
/**
* @param messageCode 状态编码
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(MessageCode messageCode, String message) {
return of(null, messageCode, message);
}
/**
* @param t 异常
* @param messageCode 状态编码
* @param message 异常消息
*
* @return 状态编码异常
*/
public static final MessageCodeException of(Throwable t, MessageCode messageCode, String message) {
if(messageCode == null) {
messageCode = MessageCode.CODE_9999;
}
if(StringUtils.isEmpty(message)) {
message = Objects.isNull(t) ? messageCode.getMessage() : t.getMessage();
}
return new MessageCodeException(t, messageCode, message);
}
/**
* @param t 异常
* @param messageCode 状态编码
* @param message 异常消息
*/
public MessageCodeException(Throwable t, MessageCode messageCode, String message) {
super(message, t);
this.messageCode = messageCode;
}
/**
* @param t 异常
* @param messageCode 状态编码
* @param message 异常消息
*/
public MessageCodeException(Throwable t, MessageCode messageCode, String message) {
super(message, t);
this.messageCode = messageCode;
}
}

View File

@@ -1,52 +0,0 @@
package com.acgist.taoyao.boot.model;
import java.util.function.Supplier;
/**
* 可修改的Optional
*
* @author acgist
*/
public final class ModifyOptional<T> {
/**
* 值
*/
private T t;
/**
* 生产者
*/
private Supplier<T> supplier;
private ModifyOptional(T t, Supplier<T> supplier) {
this.t = t;
this.supplier = supplier;
}
public static final <T> ModifyOptional<T> of(T t) {
return new ModifyOptional<>(t, null);
}
public static final <T> ModifyOptional<T> of(Supplier<T> supplier) {
return new ModifyOptional<>(null, supplier);
}
/**
* @return 值
*/
public T get() {
return this.t;
}
/**
* @return 值
*/
public T build() {
if(this.t != null) {
return this.t;
}
this.t = this.supplier.get();
return this.t;
}
}

View File

@@ -7,24 +7,24 @@ package com.acgist.taoyao.boot.service;
*/
public interface IdService {
/**
* 生成十六位的消息IDddHHmmss(8) + index(2) + xxxxxx(6)
* 只能用于生成消息IDJS超过`Number.MAX_SAFE_INTEGER`丢失精度
*
* @return ID
*/
long buildId();
/**
* @return 终端索引
*/
long buildClientIndex();
/**
* 生成三十二位唯一ID
*
* @return ID
*/
String buildUuid();
/**
* 生成十六位的消息IDddHHmmss(8) + index(2) + xxxxxx(6)
* 只能用于生成消息IDJS超过`Number.MAX_SAFE_INTEGER`丢失精度
*
* @return ID
*/
long buildId();
/**
* @return 终端索引
*/
long buildClientIndex();
/**
* 生成三十二位唯一ID
*
* @return ID
*/
String buildUuid();
}

View File

@@ -7,59 +7,59 @@ import com.acgist.taoyao.boot.config.IdProperties;
import com.acgist.taoyao.boot.service.IdService;
public class IdServiceImpl implements IdService {
public IdServiceImpl(IdProperties idProperties) {
this.index = 0;
this.serverIndex = idProperties.getServerIndex();
this.maxIndex = idProperties.getMaxIndex();
this.clientIndex = idProperties.getMinClientIndex();
public IdServiceImpl(IdProperties idProperties) {
this.index = 0;
this.serverIndex = idProperties.getServerIndex();
this.maxIndex = idProperties.getMaxIndex();
this.clientIndex = idProperties.getMinClientIndex();
this.minClientIndex = idProperties.getMinClientIndex();
this.maxClientIndex = idProperties.getMaxClientIndex();
}
/**
* 当前索引
*/
private int index;
/**
* 机器序号
*/
private final int serverIndex;
/**
* 最大序号
*/
private final int maxIndex;
/**
* 当前终端索引
*/
private int clientIndex;
/**
* 最小终端序号
*/
private final int minClientIndex;
/**
* 最大终端序号
*/
private final int maxClientIndex;
/**
* 当前索引
*/
private int index;
/**
* 机器序号
*/
private final int serverIndex;
/**
* 最大序号
*/
private final int maxIndex;
/**
* 当前终端索引
*/
private int clientIndex;
/**
* 最小终端序号
*/
private final int minClientIndex;
/**
* 最大终端序号
*/
private final int maxClientIndex;
@Override
public long buildId() {
public long buildId() {
int index;
synchronized (this) {
if (++this.index > this.maxIndex) {
this.index = 0;
}
index = this.index;
}
final LocalDateTime time = LocalDateTime.now();
return
100000000000000L * time.getDayOfMonth() +
1000000000000L * time.getHour() +
10000000000L * time.getMinute() +
100000000L * time.getSecond() +
1000000L * this.serverIndex +
index;
}
synchronized (this) {
if (++this.index > this.maxIndex) {
this.index = 0;
}
index = this.index;
}
final LocalDateTime time = LocalDateTime.now();
return
100000000000000L * time.getDayOfMonth() +
1000000000000L * time.getHour() +
10000000000L * time.getMinute() +
100000000L * time.getSecond() +
1000000L * this.serverIndex +
index;
}
@Override
public long buildClientIndex() {

View File

@@ -48,7 +48,7 @@ public final class HTTPUtils {
* @param executor 线程池
*/
public static final void init(long timeout, Executor executor) {
HTTPUtils.timeout = timeout;
HTTPUtils.timeout = timeout;
HTTPUtils.executor = executor;
}

View File

@@ -0,0 +1,6 @@
taoyao:
script:
system-reboot: shutdown /r /t 0
system-shutdown: shutdown /s /t 0
platform-reboot: net stop taoyao-signal-server && net start taoyao-signal-server
platform-shutdown: net stop taoyao-signal-server

View File

@@ -214,7 +214,7 @@ taoyao:
platform-shutdown: systemctl stop taoyao-signal-server
# WebRTC配置
webrtc:
# 是否加密E2E
# 是否终端加密E2E
encrypt: false
# STUN服务
stun:

View File

@@ -119,7 +119,9 @@ public class SocketSignal {
public void destroy() {
log.debug("关闭Socket信令服务{}", this.channel);
CloseableUtils.close(this.channel);
this.group.shutdown();
if(this.group != null) {
this.group.shutdown();
}
}
}