use of io.javalin.Javalin in project javalin by tipsy.
the class TestUtil method test.
public static void test(Javalin javalin, ThrowingBiConsumer<Javalin, HttpUtil> test) {
JavalinLogger.enabled = false;
try (final Javalin closingJavalin = javalin.start(0)) {
HttpUtil http = new HttpUtil(closingJavalin.port());
test.accept(closingJavalin, http);
closingJavalin.delete("/x-test-cookie-cleaner", ctx -> ctx.cookieMap().keySet().forEach(ctx::removeCookie));
http.call(HttpMethod.DELETE, "/x-test-cookie-cleaner");
}
JavalinLogger.enabled = true;
}
use of io.javalin.Javalin in project javalin by tipsy.
the class TestOpenApiAnnotations_Java method testWithCrudHandler.
@Test
public void testWithCrudHandler() {
OpenApiOptions openApiOptions = new OpenApiOptions(new Info().title("Example").version("1.0.0"));
Javalin app = Javalin.create(config -> config.registerPlugin(new OpenApiPlugin(openApiOptions)));
app.routes(() -> ApiBuilder.crud("users/{user-id}", new JavaCrudHandler()));
OpenAPI actual = JavalinOpenApi.createSchema(app);
assertThat(JsonKt.asJsonString(actual)).isEqualTo(JsonKt.getCrudExampleJson());
}
use of io.javalin.Javalin in project javalin by tipsy.
the class HelloWorldWebSockets method main.
public static void main(String[] args) {
Javalin app = Javalin.create(config -> config.enableDevLogging());
app.ws("/websocket", ws -> {
ws.onConnect(ctx -> {
System.out.println("Connected");
ctx.send("[MESSAGE FROM SERVER] Connection established");
});
ws.onMessage(ctx -> {
System.out.println("Received: " + ctx.message());
ctx.send("[MESSAGE FROM SERVER] Echo: " + ctx.message());
});
ws.onClose(ctx -> {
System.out.println("Closed");
});
ws.onError(ctx -> {
System.out.println("Errored");
});
});
app.get("/", ctx -> {
ctx.html("<h1>WebSocket example</h1>\n" + "<script>\n" + " let ws = new WebSocket(\"ws://localhost:7070/websocket\");\n" + " ws.onmessage = e => document.body.insertAdjacentHTML(\"beforeEnd\", \"<pre>\" + e.data + \"</pre>\");\n" + " ws.onclose = () => alert(\"WebSocket connection closed\");\n" + " setInterval(() => ws.send(\"Repeating request every 2 seconds\"), 2000);\n" + "</script>");
});
app.start(7070);
}
use of io.javalin.Javalin in project javalin by tipsy.
the class SimpleAsyncTest method test_async.
@Test
@Disabled("For running manually")
public void test_async() throws Exception {
QueuedThreadPool threadPool = new QueuedThreadPool(10, 2, 60_000);
Javalin app = Javalin.create(c -> c.server(() -> new Server(threadPool))).start(0);
HttpUtil http = new HttpUtil(app.port());
app.get("/test-async", ctx -> ctx.future(getFuture()));
app.get("/test-sync", ctx -> ctx.result(getBlockingResult()));
timeCallable("Async result", () -> {
return new ForkJoinPool(100).submit(() -> range(0, 50).parallel().forEach(i -> {
assertThat(http.getBody("/test-async")).isEqualTo("success");
})).get();
});
timeCallable("Blocking result", () -> {
return new ForkJoinPool(100).submit(() -> range(0, 50).parallel().forEach(i -> {
assertThat(http.getBody("/test-sync")).isEqualTo("success");
})).get();
});
app.stop();
}
use of io.javalin.Javalin in project javalin by tipsy.
the class FileUploadExample method main.
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.get("/", ctx -> ctx.html("" + "<form method='post' enctype='multipart/form-data'>" + " <input type='file' name='files' multiple>" + " <button>Upload</button>" + "</form>"));
app.post("/", ctx -> {
ctx.uploadedFiles("files").forEach(file -> {
FileUtil.streamToFile(file.getContent(), "upload/" + file.getFilename());
});
});
}
Aggregations