|
| 1 | +package com.unfamoussoul.test.web; |
| 2 | + |
| 3 | +import com.unfamoussoul.sapi.api.web.WebListener; |
| 4 | +import com.unfamoussoul.test.Test; |
| 5 | +import com.unfamoussoul.test.web.route.RemindersRoute; |
| 6 | +import com.unfamoussoul.test.web.route.ServerRoute; |
| 7 | +import org.eclipse.jetty.server.Server; |
| 8 | +import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 9 | + |
| 10 | +import java.util.logging.Level; |
| 11 | + |
| 12 | +import static io.javalin.apibuilder.ApiBuilder.get; |
| 13 | +import static io.javalin.apibuilder.ApiBuilder.path; |
| 14 | + |
| 15 | +public class Web extends WebListener { |
| 16 | + |
| 17 | + private final Test module; |
| 18 | + private final ServerRoute serverRoute; |
| 19 | + private final RemindersRoute remindersRoute; |
| 20 | + |
| 21 | + public Web(Test module) { |
| 22 | + this.module = module; |
| 23 | + this.serverRoute = new ServerRoute(module); |
| 24 | + this.remindersRoute = new RemindersRoute(module); |
| 25 | + createInstance(); |
| 26 | + } |
| 27 | + |
| 28 | + private void createInstance() { |
| 29 | + QueuedThreadPool threadPool = getThreadPool(); |
| 30 | + |
| 31 | + threadPool.setMinThreads(module.getConfig().webThreadsMin); |
| 32 | + threadPool.setMaxThreads(module.getConfig().webThreadsMax); |
| 33 | + threadPool.setReservedThreads(module.getConfig().webThreadsQueued); |
| 34 | + threadPool.setIdleTimeout(module.getConfig().webThreadsIdle); |
| 35 | + threadPool.setMaxEvictCount(module.getConfig().webThreadsEvict); |
| 36 | + threadPool.setDetailedDump(true); |
| 37 | + |
| 38 | + create(config -> { |
| 39 | + config.jetty.modifyServer(server -> server.setHandler(new Server(threadPool))); |
| 40 | + config.requestLogger.http((ctx, ms) -> module.getLogger().log( |
| 41 | + Level.CONFIG, "[TYPE: {}], [CONTEXT: {}], [IP: {}], took {}ms", new Object[]{ctx.method(), ctx.path(), ctx.ip(), String.format("%.2f", ms / 1000)} |
| 42 | + )); |
| 43 | + }); |
| 44 | + |
| 45 | + getApp().unsafe.routes.apiBuilder(() -> { |
| 46 | + path("/server", () -> get("online", serverRoute::getOnline)); |
| 47 | + path("/reminders", () -> { |
| 48 | + get("", remindersRoute::getAll); |
| 49 | + get("{nickname}", remindersRoute::getByNickname); |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
0 commit comments