Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh9 committed May 18, 2020
2 parents 875852a + c08f162 commit b297c16
Show file tree
Hide file tree
Showing 17 changed files with 581 additions and 53 deletions.
47 changes: 35 additions & 12 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,53 @@

image:https://img.shields.io/badge/vert.x-3.9.0-purple.svg[link="https://vertx.io"]

This application was generated using http://start.vertx.io
A minimal implementation of Nexus repository to practice Vert.x

== Building
== Features
.Mirrors
* [x] Mirror Maven 2.
* [x] Mirror JCenter.
* [x] Mirror Jitpack.

To launch your tests:
.Snapshots
* [ ] Enable snapshots.

.Publishing artifacts
* [ ] Publish artifacts to Kumoru. (_Contemplating_)

.HTTP methods supported
* [x] GET

== Pre-Requisites

1. JDK 11
2. Docker

== Building and Running Locally

To build your application:
```
./gradlew clean test
./gradlew clean build
```

To package your application:
To package your application into Docker container:
```
./gradlew clean assemble
./gradlew jibDockerBuild
```

To run your application:
```
./gradlew clean run
docker run -p 8888:8888 -v /tmp/repo:/srv/repo rishabh9/kumoru
```

== Help
> `/tmp/repo` can be replaced with any path of your choice.
> This is the location where the artifacts will be downloaded on the host machine.

To enable access logs, set the environment variable `KUMORU_ACCESS_LOG=true` as:
```
docker run -e KUMORU_ACCESS_LOG=true -p 8888:8888 -v /tmp/repo:/srv/repo rishabh9/kumoru
```

* https://vertx.io/docs/[Vert.x Documentation]
* https://stackoverflow.com/questions/tagged/vert.x?sort=newest&pageSize=15[Vert.x Stack Overflow]
* https://groups.google.com/forum/?fromgroups#!forum/vertx[Vert.x User Group]
* https://gitter.im/eclipse-vertx/vertx-users[Vert.x Gitter]
> All logs are written to the `STDOUT`.


69 changes: 50 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '5.2.0'
id 'checkstyle'
id 'maven-publish'
id 'signing'
Expand All @@ -20,10 +19,17 @@ repositories {
ext {
vertxVersion = '3.9.0'
junitJupiterEngineVersion = '5.4.0'
log4j2Version = '2.13.2'
disruptorVersion = '3.4.2'
tikaVersion = '1.24.1'
}

application {
mainClassName = 'io.vertx.core.Launcher'
applicationDefaultJvmArgs = [
'-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector',
'-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory'
]
}

java {
Expand All @@ -39,26 +45,20 @@ def watchForChange = 'src/**/*'
def doOnChange = './gradlew classes'

dependencies {
implementation "io.vertx:vertx-web-client:$vertxVersion"
implementation "io.vertx:vertx-web:$vertxVersion"
implementation "io.vertx:vertx-auth-htdigest:$vertxVersion"
implementation "io.vertx:vertx-web-client:$vertxVersion"

implementation "org.apache.tika:tika-core:$tikaVersion"

implementation "org.apache.logging.log4j:log4j-api:$log4j2Version"
implementation "org.apache.logging.log4j:log4j-core:$log4j2Version"
implementation "com.lmax:disruptor:$disruptorVersion"

testImplementation "io.vertx:vertx-junit5:$vertxVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitJupiterEngineVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitJupiterEngineVersion"
}


shadowJar {
classifier = 'fat'
manifest {
attributes 'Main-Verticle': mainVerticleName
}
mergeServiceFiles {
include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
}
}

test {
useJUnitPlatform()
testLogging {
Expand All @@ -67,7 +67,16 @@ test {
}

run {
args = ['run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$mainClassName", "--on-redeploy=$doOnChange"]
args = [
'run', mainVerticleName,
"--redeploy=$watchForChange",
"--launcher-class=$mainClassName",
"--on-redeploy=$doOnChange"
]
jvmArgs = [
'-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector',
'-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory'
]
}

jib {
Expand All @@ -76,22 +85,28 @@ jib {
}
to {
image = 'rishabh9/kumoru'
credHelper = 'docker-credential-osxkeychain'
project.afterEvaluate {
tags = [version]
tags = [version] as Set<String>
}
}
container {
jvmFlags = ['-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector']
jvmFlags = [
'-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector',
'-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory'
]
format = 'OCI'
mainClass = mainClassName
args = ['run', mainVerticleName]
}
}

publishing {

repositories {
maven {
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots"
def releasesRepoUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
def snapshotsRepoUrl = 'https://oss.sonatype.org/content/repositories/snapshots'
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username ossNexusUsername
Expand Down Expand Up @@ -153,3 +168,19 @@ spotless {
googleJavaFormat('1.7')
}
}

task createVersionFile {
// destination directory for the version file
def versionFileDir = "$buildDir/resources/main"
// version filename
def versionFileName = "version.properties"
doLast {
file(versionFileDir).mkdirs()
file("$versionFileDir/$versionFileName").delete()
// Groovy concise syntax for writing into file; maybe you want to delete this file first.
file("$versionFileDir/$versionFileName") << "version=$version"
}
}

// make processResources depends on createVersionFile task
processResources.dependsOn createVersionFile
110 changes: 90 additions & 20 deletions src/main/java/com/github/rishabh9/kumoru/MainVerticle.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,99 @@
package com.github.rishabh9.kumoru;

import com.github.rishabh9.kumoru.handlers.FinalHandler;
import com.github.rishabh9.kumoru.handlers.IndexSearchHandler;
import com.github.rishabh9.kumoru.handlers.JCenterMirrorHandler;
import com.github.rishabh9.kumoru.handlers.JitPackMirrorHandler;
import com.github.rishabh9.kumoru.handlers.LocalResourceHandler;
import com.github.rishabh9.kumoru.handlers.MavenMirrorHandler;
import com.github.rishabh9.kumoru.handlers.SendFileHandler;
import com.github.rishabh9.kumoru.handlers.ValidRequestHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.LoggerFormat;
import io.vertx.ext.web.handler.LoggerHandler;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class MainVerticle extends AbstractVerticle {

private static final int PORT = 8888;

@Override
public void start(final Future<Void> startFuture) throws Exception {
vertx
.createHttpServer()
.requestHandler(
req -> {
req.response().putHeader("content-type", "text/plain").end("Hello from Vert.x!");
})
.listen(
PORT,
http -> {
if (http.succeeded()) {
startFuture.complete();
System.out.println("HTTP server started on port 8888");
} else {
startFuture.fail(http.cause());
}
});
public void start(final Promise<Void> startFuture) {

// Get all environment variables
final int port = getKumoruPort();
final boolean enableAccessLog = enableAccessLog();

// All handlers
final ValidRequestHandler validRequestHandler = new ValidRequestHandler();
final LocalResourceHandler localResourceHandler = new LocalResourceHandler(vertx);
final MavenMirrorHandler mavenMirror = new MavenMirrorHandler(vertx);
final JCenterMirrorHandler jcenterMirror = new JCenterMirrorHandler(vertx);
final JitPackMirrorHandler jitPackMirror = new JitPackMirrorHandler(vertx);
final FinalHandler finalHandler = new FinalHandler();
final IndexSearchHandler searchHandler = new IndexSearchHandler();
final SendFileHandler sendFileHandler = new SendFileHandler();

final Router router = Router.router(vertx);
// for all routes do
final Route route = router.route();
if (enableAccessLog) {
route.handler(LoggerHandler.create(LoggerFormat.DEFAULT));
}
route.handler(validRequestHandler);
// for HEAD method do
router.head().handler(searchHandler).handler(finalHandler);
// for GET method do
router
.get()
.handler(localResourceHandler)
.handler(mavenMirror)
.handler(jcenterMirror)
.handler(jitPackMirror)
.handler(sendFileHandler)
.handler(finalHandler);

// Logging network server activity
final HttpServerOptions options = new HttpServerOptions().setLogActivity(true);
final HttpServer httpServer = vertx.createHttpServer(options);
httpServer.requestHandler(router);
httpServer.listen(
port,
asyncResult -> {
if (asyncResult.succeeded()) {
startFuture.complete();
log.info("Kumoru server started on port {}", port);
} else {
startFuture.fail(asyncResult.cause());
}
});
}

private boolean enableAccessLog() {
final String flag = System.getenv("KUMORU_ACCESS_LOG");
if (null != flag && !flag.isEmpty() && flag.matches("^(true|false)$")) {
return Boolean.parseBoolean(flag);
}
return false;
}

// I see no utility of this facility if deployed as a Docker container.
private int getKumoruPort() {
final int defaultPort = 8888;
final String port = System.getenv("KUMORU_PORT");
log.debug("Port configured as {}", port);
if (null != port
&& !port.isEmpty()
&& port.matches(
"^()([1-9]|[1-5]?[0-9]{2,4}|6[1-4][0-9]{3}|65[1-4][0-9]{2}|655[1-2][0-9]|6553[1-5])$")) {
return Integer.parseInt(port);
} else {
log.debug("Setting default port {}", defaultPort);
return defaultPort;
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/github/rishabh9/kumoru/VersionProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.rishabh9.kumoru;

import java.io.IOException;
import java.util.Properties;

public enum VersionProperties {
INSTANCE;

private final org.apache.logging.log4j.Logger log =
org.apache.logging.log4j.LogManager.getLogger(VersionProperties.class);

private final Properties properties;

VersionProperties() {
properties = new Properties();
try {
properties.load(getClass().getClassLoader().getResourceAsStream("version.properties"));
} catch (IOException e) {
log.warn(e.getMessage(), e);
}
}

public String getVersion() {
return properties.getProperty("version");
}
}
Loading

0 comments on commit b297c16

Please sign in to comment.