Skip to content

Instantly share code, notes, and snippets.

@mp911de
Last active March 13, 2017 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mp911de/e0cf842ca27fb56a4e478d705dcfe34a to your computer and use it in GitHub Desktop.
Save mp911de/e0cf842ca27fb56a4e478d705dcfe34a to your computer and use it in GitHub Desktop.
import io.netty.handler.codec.http.HttpHeaderNames;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import org.springframework.core.io.ClassPathResource;
/**
* @author Mark Paluch
*/
public class Application {
private final HttpServer server = HttpServer.create("0.0.0.0", Integer.getInteger("server.port", 8080));
private final Mono<? extends NettyContext> context;
Application() throws IOException {
Hooks.onOperator(h -> h.operatorStacktrace());
context = server.newRouter(r -> r.get("/**", (res, resp) -> resp.header(HttpHeaderNames.CONTENT_TYPE, "text/html")
.sendFile(resolveContentPath().resolve("index.html")).then().doOnError(Throwable::printStackTrace)));
}
public static void main(String... args) throws Exception {
Application app = new Application();
app.startAndAwait();
}
public void startAndAwait() {
context.block().onClose().block();
}
private Path resolveContentPath() {
try {
ClassPathResource cp = new ClassPathResource("static");
if (cp.isFile()) {
return Paths.get(cp.getURI());
}
FileSystem fs = FileSystems.newFileSystem(cp.getURI(), Collections.emptyMap());
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
fs.close();
} catch (IOException io) {
// ignore
}
}));
return fs.getPath("static");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>reactor-bugs</groupId>
<artifactId>reactor-bug-epoll-sendfile-from-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.6.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<classifier>linux-x86_64</classifier>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment