Java HTTP-server
server based on my epoll
HttpServer server = new HttpServer<>(8080);
server.getUrlMapping()
.append("/", (request, response) -> response.setBody("It's alive!"));
server.start();
Framework
Gradle
build.gradle
apply plugin: 'java'
apply plugin: 'application'
repositories {
jcenter()
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '0.1'
mainClassName = "com.example.MyWebApp"
dependencies {
compile 'com.wizzardo:http:0.2+'
}
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes(
"Main-Class": mainClassName
)
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
import com.wizzardo.http.framework.Controller;
import com.wizzardo.http.framework.Environment;
import com.wizzardo.http.framework.WebApplication;
import com.wizzardo.http.framework.template.Renderer;
public class MyWebApp {
static class AppController extends Controller {
public Renderer index() {
return renderString("It's alive!");
}
}
public static void main(String[] args) {
WebApplication application = new WebApplication(args);
application.onSetup(app -> {
app.getUrlMapping()
.append("/", AppController.class, "index");
});
application.start();
}
}
./gradlew fatJar
java -jar build/libs/MyWebApp-all.jar env=prod profiles.active=profile_A,profile_B
Controllers and actions could be mapped to static paths or to something dynamic with variables and wildcards
urlMapping
.append("/index", AppController.class, "index")
.append("/books/$id?", AppController.class, "books") // 'id' - is optional
.append("/optionals/$foo?/$bar?", AppController.class, "optionals") // 'foo' and 'bar' - are optional
.append("/${foo}-${bar}", AppController.class, "fooBar")
.append("/any/*", AppController.class, "any")
.append("*.html", AppController.class, "html")
.append("