diff --git a/pom.xml b/pom.xml index cab479685..986d50cb2 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,11 @@ nanohttpd 2.3.1 + + io.projectreactor.netty + reactor-netty + 0.9.7.RELEASE + org.panda-lang panda-utilities diff --git a/src/main/java/org/panda_lang/reposilite/Reposilite.java b/src/main/java/org/panda_lang/reposilite/Reposilite.java index df973867d..d49c10eac 100644 --- a/src/main/java/org/panda_lang/reposilite/Reposilite.java +++ b/src/main/java/org/panda_lang/reposilite/Reposilite.java @@ -18,18 +18,17 @@ import org.panda_lang.reposilite.auth.Authenticator; import org.panda_lang.reposilite.auth.TokenService; +import org.panda_lang.reposilite.config.Configuration; +import org.panda_lang.reposilite.config.ConfigurationLoader; import org.panda_lang.reposilite.console.Console; import org.panda_lang.reposilite.frontend.Frontend; import org.panda_lang.reposilite.frontend.FrontendLoader; import org.panda_lang.reposilite.metadata.MetadataService; import org.panda_lang.reposilite.repository.RepositoryService; import org.panda_lang.reposilite.utils.TimeUtils; -import org.panda_lang.reposilite.utils.YamlUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; - public final class Reposilite { private static final Logger LOGGER = LoggerFactory.getLogger("Reposilite"); @@ -56,11 +55,11 @@ public void launch() throws Exception { getLogger().info(""); Reposilite.getLogger().info("--- Preparing workspace"); - ReposiliteWorkspace workspace = new ReposiliteWorkspace(); - workspace.prepare(); + ConfigurationLoader configurationLoader = new ConfigurationLoader(); + this.configuration = configurationLoader.load(); - File configurationFile = new File(ReposiliteConstants.CONFIGURATION_FILE_NAME); - this.configuration = YamlUtils.load(configurationFile, Configuration.class); + // ReposiliteReactiveHttpServer reactiveHttpServer = new ReposiliteReactiveHttpServer(this); + // reactiveHttpServer.start(configuration); this.console = new Console(this); console.hook(); @@ -80,6 +79,7 @@ public void launch() throws Exception { this.metadataService = new MetadataService(); this.repositoryService = new RepositoryService(); + repositoryService.load(configuration); repositoryService.scan(configuration); getLogger().info(""); diff --git a/src/main/java/org/panda_lang/reposilite/ReposiliteReactiveHttpServer.java b/src/main/java/org/panda_lang/reposilite/ReposiliteReactiveHttpServer.java new file mode 100644 index 000000000..d3b9ad6df --- /dev/null +++ b/src/main/java/org/panda_lang/reposilite/ReposiliteReactiveHttpServer.java @@ -0,0 +1,31 @@ +package org.panda_lang.reposilite; + +import org.panda_lang.reposilite.config.Configuration; +import reactor.core.publisher.Mono; +import reactor.netty.DisposableServer; +import reactor.netty.http.server.HttpServer; + +import java.net.InetSocketAddress; + +final class ReposiliteReactiveHttpServer { + + private final Reposilite reposilite; + + ReposiliteReactiveHttpServer(Reposilite reposilite) { + this.reposilite = reposilite; + } + + DisposableServer start(Configuration configuration) { + return HttpServer.create() + .bindAddress(() -> InetSocketAddress.createUnresolved(configuration.getHostname(), configuration.getPort() + 1)) + .route(routes -> routes + .get("/", (request, response) -> response.sendString(Mono.just(reposilite.getFrontend().forMessage("#onlypanda")))) + .get("/*", (request, response) -> response) + .head("/*", (request, response) -> response) + .post("/*", (request, response) -> response) + ) + .bindNow(); + //.bindUntilJavaShutdown(Duration.ZERO, onStart); + } + +} diff --git a/src/main/java/org/panda_lang/reposilite/ReposiliteWorkspace.java b/src/main/java/org/panda_lang/reposilite/ReposiliteWorkspace.java deleted file mode 100644 index 1fa3ad417..000000000 --- a/src/main/java/org/panda_lang/reposilite/ReposiliteWorkspace.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2020 Dzikoysk - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.panda_lang.reposilite; - -import org.panda_lang.reposilite.utils.FilesUtils; - -import java.io.File; - -final class ReposiliteWorkspace { - - void prepare() { - // Configuration - if (!FilesUtils.exists(ReposiliteConstants.CONFIGURATION_FILE_NAME)) { - Reposilite.getLogger().info("Generating default configuration file."); - FilesUtils.copyResource("/reposilite.yml", ReposiliteConstants.CONFIGURATION_FILE_NAME); - } - else { - Reposilite.getLogger().info("Using an existing configuration file"); - } - - // Repositories - if (!FilesUtils.exists("repositories")) { - createDirectories("repositories/releases", "repositories/snapshots"); - Reposilite.getLogger().info("Default repositories have been created"); - } - else { - Reposilite.getLogger().info("Using an existing repositories directory"); - } - - // Tokens data - if (!FilesUtils.exists(ReposiliteConstants.TOKENS_FILE_NAME)) { - Reposilite.getLogger().info("Generating tokens data file..."); - FilesUtils.copyResource("/tokens.yml", ReposiliteConstants.TOKENS_FILE_NAME); - Reposilite.getLogger().info("Empty tokens file has been generated"); - } - else { - Reposilite.getLogger().info("Using an existing tokens data file"); - } - - Reposilite.getLogger().info(""); - } - - private boolean createDirectories(String... dirs) { - for (String dir : dirs) { - File directory = new File(dir); - - if (directory.exists()) { - continue; - } - - if (!directory.mkdirs()) { - return false; - } - } - - return true; - } - -} diff --git a/src/main/java/org/panda_lang/reposilite/auth/TokensStorage.java b/src/main/java/org/panda_lang/reposilite/auth/TokensStorage.java index d11746b37..f424a3d72 100644 --- a/src/main/java/org/panda_lang/reposilite/auth/TokensStorage.java +++ b/src/main/java/org/panda_lang/reposilite/auth/TokensStorage.java @@ -16,8 +16,9 @@ package org.panda_lang.reposilite.auth; -import org.panda_lang.reposilite.ReposiliteConstants; import org.panda_lang.reposilite.Reposilite; +import org.panda_lang.reposilite.ReposiliteConstants; +import org.panda_lang.reposilite.utils.FilesUtils; import org.panda_lang.reposilite.utils.YamlUtils; import java.io.File; @@ -35,6 +36,15 @@ public TokensStorage(TokenService tokenService) { } public void loadTokens() throws IOException { + if (!TOKENS_FILE.exists()) { + Reposilite.getLogger().info("Generating tokens data file..."); + FilesUtils.copyResource("/tokens.yml", ReposiliteConstants.TOKENS_FILE_NAME); + Reposilite.getLogger().info("Empty tokens file has been generated"); + } + else { + Reposilite.getLogger().info("Using an existing tokens data file"); + } + TokensCollection tokensCollection = YamlUtils.load(TOKENS_FILE, TokensCollection.class); for (Token token : tokensCollection.getTokens()) { diff --git a/src/main/java/org/panda_lang/reposilite/Configuration.java b/src/main/java/org/panda_lang/reposilite/config/Configuration.java similarity index 98% rename from src/main/java/org/panda_lang/reposilite/Configuration.java rename to src/main/java/org/panda_lang/reposilite/config/Configuration.java index 6bd7c06ea..d49a4ba6f 100644 --- a/src/main/java/org/panda_lang/reposilite/Configuration.java +++ b/src/main/java/org/panda_lang/reposilite/config/Configuration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.panda_lang.reposilite; +package org.panda_lang.reposilite.config; import java.io.Serializable; import java.util.List; diff --git a/src/main/java/org/panda_lang/reposilite/config/ConfigurationLoader.java b/src/main/java/org/panda_lang/reposilite/config/ConfigurationLoader.java new file mode 100644 index 000000000..dba4067d4 --- /dev/null +++ b/src/main/java/org/panda_lang/reposilite/config/ConfigurationLoader.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Dzikoysk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.panda_lang.reposilite.config; + +import org.panda_lang.reposilite.Reposilite; +import org.panda_lang.reposilite.ReposiliteConstants; +import org.panda_lang.reposilite.utils.FilesUtils; +import org.panda_lang.reposilite.utils.YamlUtils; + +import java.io.File; +import java.io.IOException; + +public final class ConfigurationLoader { + + public Configuration load() throws IOException { + File configurationFile = new File(ReposiliteConstants.CONFIGURATION_FILE_NAME); + + if (!configurationFile.exists()) { + Reposilite.getLogger().info("Generating default configuration file."); + FilesUtils.copyResource("/reposilite.yml", ReposiliteConstants.CONFIGURATION_FILE_NAME); + } + else { + Reposilite.getLogger().info("Using an existing configuration file"); + } + + Reposilite.getLogger().info(""); + return YamlUtils.load(configurationFile, Configuration.class); + } + + +} diff --git a/src/main/java/org/panda_lang/reposilite/frontend/FrontendLoader.java b/src/main/java/org/panda_lang/reposilite/frontend/FrontendLoader.java index 02892bead..761d5e3d5 100644 --- a/src/main/java/org/panda_lang/reposilite/frontend/FrontendLoader.java +++ b/src/main/java/org/panda_lang/reposilite/frontend/FrontendLoader.java @@ -25,11 +25,13 @@ public final class FrontendLoader { public Frontend loadFrontend(String frontendFile) throws IOException { - if (!FilesUtils.exists(frontendFile)) { + File frontend = new File(frontendFile); + + if (!frontend.exists()) { FilesUtils.copyResource("/index.html", frontendFile); } - return new Frontend(FileUtils.getContentOfFile(new File(frontendFile))); + return new Frontend(FileUtils.getContentOfFile(frontend)); } } diff --git a/src/main/java/org/panda_lang/reposilite/repository/DownloadController.java b/src/main/java/org/panda_lang/reposilite/repository/DownloadController.java index b6ce95924..8a5175eca 100644 --- a/src/main/java/org/panda_lang/reposilite/repository/DownloadController.java +++ b/src/main/java/org/panda_lang/reposilite/repository/DownloadController.java @@ -25,7 +25,7 @@ import fi.iki.elonen.NanoHTTPD.IHTTPSession; import fi.iki.elonen.NanoHTTPD.Response; import fi.iki.elonen.NanoHTTPD.Response.Status; -import org.panda_lang.reposilite.Configuration; +import org.panda_lang.reposilite.config.Configuration; import org.panda_lang.reposilite.Reposilite; import org.panda_lang.reposilite.ReposiliteController; import org.panda_lang.reposilite.ReposiliteHttpServer; diff --git a/src/main/java/org/panda_lang/reposilite/repository/RepositoryService.java b/src/main/java/org/panda_lang/reposilite/repository/RepositoryService.java index bb11f1c55..2faa101b9 100644 --- a/src/main/java/org/panda_lang/reposilite/repository/RepositoryService.java +++ b/src/main/java/org/panda_lang/reposilite/repository/RepositoryService.java @@ -16,7 +16,7 @@ package org.panda_lang.reposilite.repository; -import org.panda_lang.reposilite.Configuration; +import org.panda_lang.reposilite.config.Configuration; import org.panda_lang.reposilite.Reposilite; import org.panda_lang.reposilite.metadata.MetadataUtils; import org.panda_lang.utilities.commons.StringUtils; @@ -28,16 +28,35 @@ public final class RepositoryService { + private final File rootDirectory = new File("repositories"); private final Map repositories; public RepositoryService() { this.repositories = new LinkedHashMap<>(2); } - public void scan(Configuration configuration) { - File rootDirectory = new File("repositories"); - repositories.clear(); + public void load(Configuration configuration) { + Reposilite.getLogger().info("--- Loading repository"); + + if (rootDirectory.mkdirs()) { + Reposilite.getLogger().info("Default repository directory has been created"); + } + else { + Reposilite.getLogger().info("Using an existing repository directory"); + } + + for (String repository : configuration.getRepositories()) { + File repositoryDirectory = new File(rootDirectory, repository); + if (repositoryDirectory.mkdirs()) { + Reposilite.getLogger().info("+ Repository '" + repository + "' has been created"); + } + } + + Reposilite.getLogger().info(""); + } + + public void scan(Configuration configuration) { Reposilite.getLogger().info("--- Scanning to find repositories"); for (String repositoryName : configuration.getRepositories()) { diff --git a/src/main/java/org/panda_lang/reposilite/repository/RepositoryUtils.java b/src/main/java/org/panda_lang/reposilite/repository/RepositoryUtils.java index 5a52f29e9..048ce7279 100644 --- a/src/main/java/org/panda_lang/reposilite/repository/RepositoryUtils.java +++ b/src/main/java/org/panda_lang/reposilite/repository/RepositoryUtils.java @@ -16,7 +16,7 @@ package org.panda_lang.reposilite.repository; -import org.panda_lang.reposilite.Configuration; +import org.panda_lang.reposilite.config.Configuration; import org.panda_lang.utilities.commons.StringUtils; import org.panda_lang.utilities.commons.text.ContentJoiner; diff --git a/src/main/java/org/panda_lang/reposilite/repository/UploadController.java b/src/main/java/org/panda_lang/reposilite/repository/UploadController.java index d3b1b35bf..92d3139f4 100644 --- a/src/main/java/org/panda_lang/reposilite/repository/UploadController.java +++ b/src/main/java/org/panda_lang/reposilite/repository/UploadController.java @@ -21,7 +21,7 @@ import fi.iki.elonen.NanoHTTPD.Response.Status; import fi.iki.elonen.NanoHTTPD.ResponseException; import org.apache.commons.io.FileUtils; -import org.panda_lang.reposilite.Configuration; +import org.panda_lang.reposilite.config.Configuration; import org.panda_lang.reposilite.ReposiliteController; import org.panda_lang.reposilite.ReposiliteHttpServer; import org.panda_lang.reposilite.Reposilite; diff --git a/src/main/java/org/panda_lang/reposilite/utils/FilesUtils.java b/src/main/java/org/panda_lang/reposilite/utils/FilesUtils.java index 233198e04..40cb6fa94 100644 --- a/src/main/java/org/panda_lang/reposilite/utils/FilesUtils.java +++ b/src/main/java/org/panda_lang/reposilite/utils/FilesUtils.java @@ -82,8 +82,4 @@ public static String getExtension(String name) { return occurrence == -1 ? StringUtils.EMPTY : name.substring(occurrence + 1); } - public static boolean exists(String file) { - return new File(file).exists(); - } - }