更新代码和文档

This commit is contained in:
q
2026-01-03 21:11:04 +08:00
parent 48aa5b6148
commit d8f0dc4f8e
25 changed files with 789 additions and 161 deletions

View File

@@ -122,13 +122,23 @@ public class AppMain {
if (parser.getBoolean("enabled", false)) {
try {
String jsCode = parser.getString("jsCode");
if (jsCode == null || jsCode.trim().isEmpty()) {
log.error("加载演练场解析器失败: {} - JavaScript代码为空", parser.getString("name"));
continue;
}
CustomParserConfig config = JsScriptMetadataParser.parseScript(jsCode);
CustomParserRegistry.register(config);
loadedCount++;
log.info("已加载演练场解析器: {} ({})",
config.getDisplayName(), config.getType());
} catch (Exception e) {
log.error("加载演练场解析器失败: {}", parser.getString("name"), e);
String parserName = parser.getString("name");
String errorMsg = e.getMessage();
log.error("加载演练场解析器失败: {} - {}", parserName, errorMsg, e);
// 如果是require相关错误提供更详细的提示
if (errorMsg != null && errorMsg.contains("require")) {
log.error("提示演练场解析器不支持CommonJS模块系统require请确保代码使用ES5.1语法");
}
}
}
}

View File

@@ -18,6 +18,12 @@ public class PlaygroundConfig {
*/
private static PlaygroundConfig instance;
/**
* 是否启用演练场
* 默认false不启用
*/
private boolean enabled = false;
/**
* 是否公开模式(不需要密码)
* 默认false需要密码访问
@@ -57,17 +63,20 @@ public class PlaygroundConfig {
PlaygroundConfig cfg = getInstance();
if (config != null && config.containsKey("playground")) {
JsonObject playgroundConfig = config.getJsonObject("playground");
cfg.enabled = playgroundConfig.getBoolean("enabled", false);
cfg.isPublic = playgroundConfig.getBoolean("public", false);
cfg.password = playgroundConfig.getString("password", "nfd_playground_2024");
log.info("Playground配置已加载: public={}, password={}",
cfg.isPublic, cfg.isPublic ? "N/A" : "已设置");
log.info("Playground配置已加载: enabled={}, public={}, password={}",
cfg.enabled, cfg.isPublic, cfg.isPublic ? "N/A" : "已设置");
if (!cfg.isPublic && "nfd_playground_2024".equals(cfg.password)) {
if (!cfg.enabled) {
log.info("演练场功能已禁用");
} else if (!cfg.isPublic && "nfd_playground_2024".equals(cfg.password)) {
log.warn("⚠️ 警告:您正在使用默认密码,建议修改配置文件中的 playground.password 以确保安全!");
}
} else {
log.info("未找到playground配置使用默认值: public=false");
log.info("未找到playground配置使用默认值: enabled=false, public=false");
}
}
}

View File

@@ -50,10 +50,23 @@ public class PlaygroundApi {
private static final String SESSION_AUTH_KEY = "playgroundAuthed";
private final DbService dbService = AsyncServiceUtil.getAsyncServiceInstance(DbService.class);
/**
* 检查Playground是否启用
*/
private boolean checkEnabled() {
PlaygroundConfig config = PlaygroundConfig.getInstance();
return config.isEnabled();
}
/**
* 检查Playground访问权限
*/
private boolean checkAuth(RoutingContext ctx) {
// 首先检查是否启用
if (!checkEnabled()) {
return false;
}
PlaygroundConfig config = PlaygroundConfig.getInstance();
// 如果是公开模式,直接允许访问
@@ -77,9 +90,11 @@ public class PlaygroundApi {
@RouteMapping(value = "/status", method = RouteMethod.GET)
public Future<JsonObject> getStatus(RoutingContext ctx) {
PlaygroundConfig config = PlaygroundConfig.getInstance();
boolean authed = checkAuth(ctx);
boolean enabled = config.isEnabled();
boolean authed = enabled && checkAuth(ctx);
JsonObject result = new JsonObject()
.put("enabled", enabled)
.put("public", config.isPublic())
.put("authed", authed);
@@ -91,6 +106,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/login", method = RouteMethod.POST)
public Future<JsonObject> login(RoutingContext ctx) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
Promise<JsonObject> promise = Promise.promise();
try {
@@ -142,6 +162,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/test", method = RouteMethod.POST)
public Future<JsonObject> test(RoutingContext ctx) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
// 权限检查
if (!checkAuth(ctx)) {
return Future.succeededFuture(JsonResult.error("未授权访问").toJsonObject());
@@ -345,6 +370,12 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/types.js", method = RouteMethod.GET)
public void getTypesJs(RoutingContext ctx, HttpServerResponse response) {
// 检查是否启用
if (!checkEnabled()) {
ResponseUtil.fireJsonResultResponse(response, JsonResult.error("演练场功能已禁用"));
return;
}
// 权限检查
if (!checkAuth(ctx)) {
ResponseUtil.fireJsonResultResponse(response, JsonResult.error("未授权访问"));
@@ -377,6 +408,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/parsers", method = RouteMethod.GET)
public Future<JsonObject> getParserList(RoutingContext ctx) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
// 权限检查
if (!checkAuth(ctx)) {
return Future.succeededFuture(JsonResult.error("未授权访问").toJsonObject());
@@ -389,6 +425,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/parsers", method = RouteMethod.POST)
public Future<JsonObject> saveParser(RoutingContext ctx) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
// 权限检查
if (!checkAuth(ctx)) {
return Future.succeededFuture(JsonResult.error("未授权访问").toJsonObject());
@@ -504,6 +545,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/parsers/:id", method = RouteMethod.PUT)
public Future<JsonObject> updateParser(RoutingContext ctx, Long id) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
// 权限检查
if (!checkAuth(ctx)) {
return Future.succeededFuture(JsonResult.error("未授权访问").toJsonObject());
@@ -585,6 +631,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/parsers/:id", method = RouteMethod.DELETE)
public Future<JsonObject> deleteParser(RoutingContext ctx, Long id) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
// 权限检查
if (!checkAuth(ctx)) {
return Future.succeededFuture(JsonResult.error("未授权访问").toJsonObject());
@@ -628,6 +679,11 @@ public class PlaygroundApi {
*/
@RouteMapping(value = "/parsers/:id", method = RouteMethod.GET)
public Future<JsonObject> getParserById(RoutingContext ctx, Long id) {
// 检查是否启用
if (!checkEnabled()) {
return Future.succeededFuture(JsonResult.error("演练场功能已禁用").toJsonObject());
}
// 权限检查
if (!checkAuth(ctx)) {
return Future.succeededFuture(JsonResult.error("未授权访问").toJsonObject());

View File

@@ -15,6 +15,8 @@ proxyConf: server-proxy
# JS演练场配置
playground:
# 是否启用演练场默认false不启用
enabled: false
# 公开模式默认false需要密码访问设为true则无需密码
public: false
# 访问密码,建议修改默认密码!

View File

@@ -4,7 +4,7 @@ server-name: Vert.x-proxy-server(v4.1.2)
proxy:
- listen: 6401
# 404的路径
page404: webroot/err/404.html
page404: webroot/nfd-front/index.html
static:
path: /
add-headers: