|
@@ -18,34 +18,11 @@ import org.smartboot.http.server.HttpServerHandler;
|
|
|
import org.smartboot.http.server.handler.HttpRouteHandler;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
public class Bootstrap {
|
|
|
static byte[] body = "Hello, World!".getBytes();
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- HttpRouteHandler routeHandle = new HttpRouteHandler();
|
|
|
- routeHandle
|
|
|
- .route("/plaintext", new HttpServerHandler() {
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public void handle(HttpRequest request, HttpResponse response) throws IOException {
|
|
|
- response.setContentLength(body.length);
|
|
|
- response.setContentType("text/plain; charset=UTF-8");
|
|
|
- response.write(body);
|
|
|
- }
|
|
|
- })
|
|
|
- .route("/json", new HttpServerHandler() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void handle(HttpRequest request, HttpResponse response) throws IOException {
|
|
|
-
|
|
|
- response.setContentType("application/json");
|
|
|
- JsonUtil.writeJsonBytes(response, new Message("Hello, World!"));
|
|
|
- }
|
|
|
- });
|
|
|
- initDB(routeHandle);
|
|
|
int cpuNum = Runtime.getRuntime().availableProcessors();
|
|
|
// 定义服务器接受的消息类型以及各类消息对应的处理器
|
|
|
HttpBootstrap bootstrap = new HttpBootstrap();
|
|
@@ -54,7 +31,19 @@ public class Bootstrap {
|
|
|
.headerLimiter(0)
|
|
|
.readBufferSize(1024 * 4)
|
|
|
.writeBufferSize(1024 * 4);
|
|
|
- bootstrap.httpHandler(routeHandle).setPort(8080).start();
|
|
|
+ bootstrap.httpHandler(new HttpServerHandler() {
|
|
|
+ @Override
|
|
|
+ public void handle(HttpRequest request, HttpResponse response) throws Throwable {
|
|
|
+ if ("/plaintext".equals(request.getRequestURI())) {
|
|
|
+ response.setContentLength(body.length);
|
|
|
+ response.setContentType("text/plain; charset=UTF-8");
|
|
|
+ response.write(body);
|
|
|
+ } else if ("/json".equals(request.getRequestURI())) {
|
|
|
+ response.setContentType("application/json");
|
|
|
+ JsonUtil.writeJsonBytes(response, new Message("Hello, World!"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).setPort(8080).start();
|
|
|
}
|
|
|
|
|
|
private static void initDB(HttpRouteHandler routeHandle) {
|