diff --git a/build.gradle b/build.gradle index ac50540..d54ff04 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,7 @@ dependencies { implementation 'io.activej:activej-http:4.1' implementation 'io.activej:activej-boot:4.1' implementation 'io.activej:activej-specializer:4.1' + implementation 'io.activej:activej-launchers-http:4.1' implementation 'net.java.dev.jna:jna-platform:5.8.0' } diff --git a/config.properties b/config.properties index 0109681..7f296a3 100644 --- a/config.properties +++ b/config.properties @@ -2,6 +2,9 @@ # The port to Listen on. PORT: 8080 +# The number of workers to use for the server +HTTP_WORKERS: 128 + # Proxy PROXY_PART: https://pipedproxy-ams.kavin.rocks diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index dd8de5a..a0a1460 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -1,10 +1,11 @@ package me.kavin.piped; +import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.http.HttpHeaders.CACHE_CONTROL; import static io.activej.http.HttpHeaders.CONTENT_TYPE; -import static io.activej.inject.module.Modules.combine; import java.io.ByteArrayInputStream; +import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -15,40 +16,25 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.SyndFeedInput; -import io.activej.eventloop.Eventloop; -import io.activej.http.AsyncHttpServer; +import io.activej.config.Config; import io.activej.http.AsyncServlet; import io.activej.http.HttpMethod; import io.activej.http.HttpResponse; import io.activej.http.RoutingServlet; -import io.activej.inject.annotation.Eager; import io.activej.inject.annotation.Provides; +import io.activej.inject.module.AbstractModule; import io.activej.inject.module.Module; -import io.activej.launcher.Launcher; -import io.activej.service.ServiceGraphModule; -import io.activej.worker.WorkerPool; -import io.activej.worker.WorkerPoolModule; -import io.activej.worker.WorkerPools; +import io.activej.launchers.http.MultithreadedHttpServerLauncher; import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.CustomServletDecorator; import me.kavin.piped.utils.ResponseHelper; import me.kavin.piped.utils.SponsorBlockUtils; import me.kavin.piped.utils.resp.ErrorResponse; -public class ServerLauncher extends Launcher { +public class ServerLauncher extends MultithreadedHttpServerLauncher { @Provides - Eventloop eventloop() { - return Eventloop.create(); - } - - @Provides - WorkerPool workerPool(WorkerPools workerPools) { - return workerPools.createPool(128); - } - - @Provides - AsyncServlet servlet() { + AsyncServlet mainServlet() { RoutingServlet router = RoutingServlet.create().map(HttpMethod.GET, "/webhooks/pubsub", request -> { return HttpResponse.ok200().withPlainText(request.getQueryParameter("hub.challenge")); @@ -147,20 +133,17 @@ public class ServerLauncher extends Launcher { return new CustomServletDecorator(router); } - @Provides - @Eager - AsyncHttpServer server(Eventloop eventloop, AsyncServlet servlet) { - return AsyncHttpServer.create(eventloop, servlet).withListenPort(Constants.PORT); - } - @Override - protected Module getModule() { - return combine(ServiceGraphModule.create(), WorkerPoolModule.create()); - } - - @Override - protected void run() throws Exception { - awaitShutdown(); + protected Module getOverrideModule() { + return new AbstractModule() { + @Provides + Config config() { + return Config.create() + .with("http.listenAddresses", + Config.ofValue(ofInetSocketAddress(), new InetSocketAddress(PORT))) + .with("workers", Constants.HTTP_WORKERS); + } + }; } private @NotNull HttpResponse getJsonResponse(byte[] body, String cache) { diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index e422369..3caeb34 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -19,6 +19,7 @@ public class Constants { public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0"; public static final int PORT; + public static final String HTTP_WORKERS; public static final String PROXY_PART; @@ -42,7 +43,8 @@ public class Constants { YOUTUBE_SERVICE = NewPipe.getService(0); prop.load(new FileReader("config.properties")); - PORT = Integer.parseInt(prop.getProperty("PORT")); + PORT = Integer.parseInt(prop.getProperty("PORT", "8080")); + HTTP_WORKERS = prop.getProperty("HTTP_WORKERS", "128"); PROXY_PART = prop.getProperty("PROXY_PART"); CAPTCHA_BASE_URL = prop.getProperty("CAPTCHA_BASE_URL"); CAPTCHA_API_KEY = prop.getProperty("CAPTCHA_API_KEY");