[general] Add a simple boot checker and add debug message

pull/1/head
samnyan 2020-04-20 10:48:41 +09:00
parent 14dec1e3e3
commit 7fc1127026
5 changed files with 123 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package icu.samnyan.aqua;
import icu.samnyan.aqua.sega.aimedb.AimeDbServer;
import icu.samnyan.aqua.spring.util.AutoChecker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@ -13,6 +14,9 @@ public class AquaServerApplication {
final AimeDbServer aimeDbServer = ctx.getBean(AimeDbServer.class);
aimeDbServer.start();
final AutoChecker checker = ctx.getBean(AutoChecker.class);
checker.check();
}
}

View File

@ -10,6 +10,7 @@ import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
import java.util.Collections;
/**
* @author samnyan (privateamusement@protonmail.com)
@ -21,8 +22,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Arrays.asList("*"));
config.setAllowedHeaders(Arrays.asList("*"));
config.setAllowedOrigins(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Arrays.asList("GET", "PUT", "POST", "DELETE", "OPTIONS"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", config);

View File

@ -6,6 +6,7 @@ import icu.samnyan.aqua.sega.allnet.util.Decoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@ -32,6 +33,16 @@ public class AllNetController {
this.PORT = PORT;
}
@GetMapping("/")
public String root() {
return "Server running";
}
@GetMapping("/sys/test")
public String selfTest() {
return "Server running";
}
@PostMapping(value = "/sys/servlet/PowerOn", produces = "text/plain")
public String powerOn(InputStream dataStream) throws IOException {

View File

@ -38,7 +38,9 @@ public class CompressionFilter extends OncePerRequestFilter {
byte[] reqResult;
if (encoding != null && encoding.equals("deflate")) {
logger.debug("Request length (compressed): {}", reqSrc.length);
reqResult = Compression.decompress(reqSrc);
logger.debug("Request length (decompressed): {}", reqResult.length);
} else {
reqResult = reqSrc;
}
@ -49,7 +51,9 @@ public class CompressionFilter extends OncePerRequestFilter {
filterChain.doFilter(requestWrapper, responseWrapper);
byte[] respSrc = responseWrapper.toByteArray();
logger.debug("Response length (uncompressed): {}", respSrc.length);
byte[] respResult = Compression.compress(respSrc);
logger.debug("Response length (compressed): {}", respResult.length);
response.setContentLength(respResult.length);

View File

@ -0,0 +1,101 @@
package icu.samnyan.aqua.spring.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Objects;
/**
* A simple boot check to warn user if there is some wrong config
* @author samnyan (privateamusement@protonmail.com)
*/
@Component
public class AutoChecker {
private final String LINEBREAK = System.getProperty("line.separator");
private final String ALLNET_HOST;
private final String ALLNET_PORT;
private final String AIMEDB_BIND;
private final int AIMEDB_PORT;
private final boolean AIMEDB_ENABLED;
public AutoChecker(
@Value("${allnet.server.host}") String ALLNET_HOST,
@Value("${allnet.server.port}") String ALLNET_PORT,
@Value("${aimedb.server.address}") String AIMEDB_BIND,
@Value("${aimedb.server.port}") int AIMEDB_PORT,
@Value("${aimedb.server.enable}") boolean AIMEDB_ENABLED) {
this.ALLNET_HOST = ALLNET_HOST;
this.ALLNET_PORT = ALLNET_PORT;
this.AIMEDB_BIND = AIMEDB_BIND;
this.AIMEDB_PORT = AIMEDB_PORT;
this.AIMEDB_ENABLED = AIMEDB_ENABLED;
}
public void check() {
// Boot message
System.out.println(" █████╗ ██████╗ ██╗ ██╗ █████╗ \n" +
"██╔══██╗██╔═══██╗██║ ██║██╔══██╗\n" +
"███████║██║ ██║██║ ██║███████║\n" +
"██╔══██║██║▄▄ ██║██║ ██║██╔══██║\n" +
"██║ ██║╚██████╔╝╚██████╔╝██║ ██║\n" +
"╚═╝ ╚═╝ ╚══▀▀═╝ ╚═════╝ ╚═╝ ╚═╝\n" +
" ");
System.out.println("======= Self test running =======");
// Check aimedb
System.out.print(" AimeDB : ");
if(!AIMEDB_ENABLED) {
System.out.println("DISABLED, SKIP");
} else {
String address = "127.0.0.1";
if(!AIMEDB_BIND.equals("0.0.0.0")) {
address = AIMEDB_BIND;
}
try (Socket test = new Socket(address, AIMEDB_PORT)){
System.out.println("OK");
} catch (Exception e) {
System.out.println("ERROR!!");
System.out.println(e.getMessage());
}
}
// Check http part
System.out.print(" AllNet : ");
StringBuilder allNetSb = new StringBuilder();
if(ALLNET_HOST.equals("localhost")||ALLNET_HOST.startsWith("127.0.0.")) {
System.out.print("WARNING!! ");
allNetSb.append("You are using loopback address.").append(LINEBREAK);
allNetSb.append("Some game won't connect with loopback address,").append(LINEBREAK);
allNetSb.append("please change setting in `application.properties`.").append(LINEBREAK);
}
RestTemplate restTemplate = new RestTemplate();
String url = "http://" + ALLNET_HOST + ":" + ALLNET_PORT + "/sys/test";
try{
ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
if(resp.getStatusCode().is2xxSuccessful() && Objects.equals(resp.getBody(), "Server running")) {
System.out.println("OK");
} else {
System.out.println("FAIL!");
allNetSb.append("Could not connect to ").append(url).append(LINEBREAK);
allNetSb.append("Status code: ").append(resp.getStatusCodeValue()).append(LINEBREAK);
}
} catch (Exception e) {
System.out.println("ERROR!");
System.out.println(e.getCause().getMessage());
}
System.out.println();
System.out.println(allNetSb.toString());
}
}