|
8 | 8 |
|
9 | 9 | package org.smartboot.http; |
10 | 10 |
|
11 | | -import com.zaxxer.hikari.HikariConfig; |
12 | | -import com.zaxxer.hikari.HikariDataSource; |
13 | 11 | import org.smartboot.Message; |
| 12 | +import tech.smartboot.feat.core.Feat; |
| 13 | +import tech.smartboot.feat.core.common.enums.HeaderValueEnum; |
| 14 | +import tech.smartboot.feat.core.server.HttpHandler; |
14 | 15 | import tech.smartboot.feat.core.server.HttpRequest; |
15 | 16 | import tech.smartboot.feat.core.server.HttpResponse; |
16 | | -import tech.smartboot.feat.core.server.HttpServer; |
17 | | -import tech.smartboot.feat.core.server.HttpServerHandler; |
18 | | -import tech.smartboot.feat.core.server.handler.HttpRouteHandler; |
19 | | - |
20 | | -import javax.sql.DataSource; |
21 | 17 |
|
22 | 18 | public class Bootstrap { |
23 | 19 | static byte[] body = "Hello, World!".getBytes(); |
24 | 20 |
|
25 | 21 | public static void main(String[] args) { |
26 | 22 | int cpuNum = Runtime.getRuntime().availableProcessors(); |
27 | 23 | // 定义服务器接受的消息类型以及各类消息对应的处理器 |
28 | | - HttpServer bootstrap = new HttpServer(); |
29 | | - bootstrap.configuration() |
30 | | - .threadNum(cpuNum + 1) |
31 | | - .headerLimiter(0) |
32 | | - .readBufferSize(1024 * 4) |
33 | | - .writeBufferSize(1024 * 4); |
34 | | - bootstrap.httpHandler(new HttpServerHandler() { |
35 | | - @Override |
36 | | - public void handle(HttpRequest request, HttpResponse response) throws Throwable { |
37 | | - if ("/plaintext".equals(request.getRequestURI())) { |
38 | | - response.setContentLength(body.length); |
39 | | - response.setContentType("text/plain; charset=UTF-8"); |
40 | | - response.write(body); |
41 | | - } else if ("/json".equals(request.getRequestURI())) { |
42 | | - response.setContentType("application/json"); |
43 | | - JsonUtil.writeJsonBytes(response, new Message("Hello, World!")); |
44 | | - } |
| 24 | + Feat.createHttpServer(options -> { |
| 25 | + options.threadNum(cpuNum + 1) |
| 26 | + .headerLimiter(0) |
| 27 | + .readBufferSize(1024 * 4) |
| 28 | + .writeBufferSize(1024 * 4); |
| 29 | + }).httpHandler(request -> { |
| 30 | + HttpResponse response = request.getResponse(); |
| 31 | + if ("/plaintext".equals(request.getRequestURI())) { |
| 32 | + response.setContentLength(body.length); |
| 33 | + response.setContentType(HeaderValueEnum.ContentType.TEXT_PLAIN_UTF8); |
| 34 | + response.write(body); |
| 35 | + } else if ("/json".equals(request.getRequestURI())) { |
| 36 | + response.setContentType("application/json"); |
| 37 | + JsonUtil.writeJsonBytes(response, new Message("Hello, World!")); |
45 | 38 | } |
46 | | - }).setPort(8080).start(); |
| 39 | + }).listen(8080); |
47 | 40 | } |
48 | 41 |
|
49 | | - private static void initDB(HttpRouteHandler routeHandle) { |
50 | | - try { |
51 | | - Class.forName("org.postgresql.Driver"); |
52 | | - } catch (ClassNotFoundException e) { |
53 | | - e.printStackTrace(); |
54 | | - } |
55 | | - HikariConfig config = new HikariConfig(); |
56 | | - config.setJdbcUrl("jdbc:postgresql://tfb-database:5432/hello_world"); |
57 | | - config.setUsername("benchmarkdbuser"); |
58 | | - config.setPassword("benchmarkdbpass"); |
59 | | - config.setMaximumPoolSize(64); |
60 | | - config.addDataSourceProperty("cachePrepStmts", "true"); |
61 | | - config.addDataSourceProperty("prepStmtCacheSize", "250"); |
62 | | - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); |
63 | | - DataSource dataSource = new HikariDataSource(config); |
64 | | - routeHandle.route("/db", new SingleQueryHandler(dataSource)) |
65 | | - .route("/queries", new MultipleQueriesHandler(dataSource)) |
66 | | - .route("/updates", new UpdateHandler(dataSource)); |
67 | | -// .route("/fortunes", new FortunesHandler(dataSource)); |
68 | | - } |
| 42 | +// private static void initDB(HttpRouteHandler routeHandle) { |
| 43 | +// try { |
| 44 | +// Class.forName("org.postgresql.Driver"); |
| 45 | +// } catch (ClassNotFoundException e) { |
| 46 | +// e.printStackTrace(); |
| 47 | +// } |
| 48 | +// HikariConfig config = new HikariConfig(); |
| 49 | +// config.setJdbcUrl("jdbc:postgresql://tfb-database:5432/hello_world"); |
| 50 | +// config.setUsername("benchmarkdbuser"); |
| 51 | +// config.setPassword("benchmarkdbpass"); |
| 52 | +// config.setMaximumPoolSize(64); |
| 53 | +// config.addDataSourceProperty("cachePrepStmts", "true"); |
| 54 | +// config.addDataSourceProperty("prepStmtCacheSize", "250"); |
| 55 | +// config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); |
| 56 | +// DataSource dataSource = new HikariDataSource(config); |
| 57 | +// routeHandle.route("/db", new SingleQueryHandler(dataSource)) |
| 58 | +// .route("/queries", new MultipleQueriesHandler(dataSource)) |
| 59 | +// .route("/updates", new UpdateHandler(dataSource)); |
| 60 | +//// .route("/fortunes", new FortunesHandler(dataSource)); |
| 61 | +// } |
69 | 62 | } |
0 commit comments