Skip to content

Commit

Permalink
GH-22 Initialize reactive-netty implementation of http server
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed May 22, 2020
1 parent e7ab231 commit d45eae1
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 95 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>nanohttpd</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.panda-lang</groupId>
<artifactId>panda-utilities</artifactId>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/panda_lang/reposilite/Reposilite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
Expand All @@ -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("");

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
73 changes: 0 additions & 73 deletions src/main/java/org/panda_lang/reposilite/ReposiliteWorkspace.java

This file was deleted.

12 changes: 11 additions & 1 deletion src/main/java/org/panda_lang/reposilite/auth/TokensStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,16 +28,35 @@

public final class RepositoryService {

private final File rootDirectory = new File("repositories");
private final Map<String, Repository> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/panda_lang/reposilite/utils/FilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit d45eae1

Please sign in to comment.