我几个月来一直致力于一个业余爱好项目,这是一个 MIT 许可的 API 网关,旨在独立于任何特定供应商。老实说,我认为一切进展顺利。随着我的代码库的增长,我看到了围绕核心(即 HTTP 服务器)进行改进的机会。将核心 HTTP 服务器拆分为自己的微框架似乎是一个合乎逻辑的解决方案(也是一个很好的学习练习!)。
引入 Kindling,它将点燃您的应用程序的燃料。 Kindling 基于标准 Java 21 库,没有依赖项。它被设计为可编程的,无需使用任何魔法。
这是一个简单的 Kindling Hello World:
package io.kerosenelabs.kindling; import java.nio.file.Path; import java.util.HashMap; import io.kerosenelabs.kindling.constant.HttpMethod; import io.kerosenelabs.kindling.constant.HttpStatus; import io.kerosenelabs.kindling.exception.KindlingException; import io.kerosenelabs.kindling.handler.RequestHandler; public class Main { public static void main(String[] args) throws KindlingException { KindlingServer server = KindlingServer.getInstance(); // test request handler server.installRequestHandler(new RequestHandler() { /** * Tell the server what type of request this handler can work with */ @Override public boolean accepts(HttpMethod httpMethod, String resource) throws KindlingException { return httpMethod.equals(HttpMethod.GET) && resource.equals("/"); } /** * Do your business logic here */ @Override public HttpResponse handle(HttpRequest httpRequest) throws KindlingException { return new HttpResponse.Builder() .status(HttpStatus.OK) .headers(new HashMap() { { put("Content-Type", "text/html"); } }) .content("Hello from Kindling!
") .build(); } }); // serve our server server.serve(8443, Path.of("mykeystore.p12"), "password"); } }
向服务器发送 CURL 请求会产生以下响应:
> GET / HTTP/1.1 > Host: localhost:8443 > User-Agent: curl/7.88.1 > Accept: */* > * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4): Hello from Kindling!
...很酷,对吧?
存在一些错误,例如响应中缺少 Content-Length。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3