Tango Web API

Tango Web API

为你的模组打包Web服务器和Web界面的API

基础库

TangoWebAPI

一个用于 Fabric/NeoForge 的单一共享 Web 服务器模组,用于消除其他模组的样板代码。TangoWebAPI 提供一个嵌入式 HTTP 服务器,模组可以用它来暴露自己的端点或静态资源。

特性

  • 所有模组共享的 HTTP 服务器(Java HTTP),在公共代码中启动/停止。
  • 简单的路由 API:get/post/registerRoute,使用模组命名空间(/modid/...)。
  • 每个模组的静态文件服务(serveStatic,带安全路径处理)。
  • 内置健康/信息/路由列表:/tango-webapi/health、/tango-webapi/info、/tango-webapi/routes。
  • 通过文件(config/tango-webapi.properties)或环境变量/系统属性进行配置。
  • 错误页面(404/405/503),使用简单的 HTML 模板。

配置

文件:config/tango-webapi.properties

host=0.0.0.0
port=8080
maxRequestBytes=1000000

或通过环境变量/系统属性:

  • TANGO_WEBAPI_HOST / tango.webapi.host
  • TANGO_WEBAPI_PORT / tango.webapi.port
  • TANGO_WEBAPI_MAX_BYTES / tango.webapi.max_bytes

供其他模组使用的 API(示例)

// 简单的文本路由,位于 /mymod/ping
TangoWebAPI.get("mymod", "/ping", ctx -> ctx.sendText(200, "pong"));

// JSON 响应
TangoWebAPI.post("mymod", "/data", ctx -> ctx.sendJson(200, "{\"ok\":true}"));

// 从 config/mymod/static 提供静态资源,位于 /mymod/static/**
TangoWebAPI.serveStatic("mymod", "static", Path.of("config", "mymod", "static"));

内置端点

  • GET /tango-webapi/health – 状态
  • GET /tango-webapi/info – 主机/端口/最大字节数
  • GET /tango-webapi/routes – 所有已注册路由,包括前缀路由

测试

  • 游戏启动后:http://localhost:8080/tango-webapi/health / info / routes
  • 注册你自己的路由,并通过浏览器/curl 访问它。
  • 在你的模组路径下提供一个静态页面,并验证它能加载。