use of io.javalin.Javalin in project javalin by tipsy.
the class JavaTest method custom_javalin_works.
@Test
public void custom_javalin_works() {
Javalin app = Javalin.create().get("/hello", ctx -> ctx.result("Hello, World!"));
JavalinTest.test(app, (server, client) -> {
assertThat(client.get("/hello").body().string()).isEqualTo("Hello, World!");
});
}
use of io.javalin.Javalin in project javalin by tipsy.
the class HelloWorldAsync method main.
public static void main(String[] args) {
Javalin app = Javalin.create().start(7070);
app.get("/", ctx -> {
CompletableFuture<String> future = new CompletableFuture<>();
Executors.newSingleThreadScheduledExecutor().schedule(() -> future.complete("Hello World!"), 10, TimeUnit.MILLISECONDS);
ctx.future(future);
});
}
use of io.javalin.Javalin in project javalin by tipsy.
the class HelloWorldCors method main.
public static void main(String[] args) {
Javalin corsApp = Javalin.create(config -> {
config.enableCorsForOrigin("http://localhost:7001/", "http://localhost:7002");
}).start(7070);
corsApp.routes(() -> {
get(ctx -> ctx.json("Hello Get"));
post(ctx -> ctx.json("Hello Post"));
patch(ctx -> ctx.json("Hello Patch"));
});
Javalin.create().start(7001).get("/", ctx -> ctx.html("Try some CORS"));
Javalin.create().start(7002).get("/", ctx -> ctx.html("Try some CORS"));
Javalin.create().start(7003).get("/", ctx -> ctx.html("No CORS here"));
}
use of io.javalin.Javalin in project javalin by tipsy.
the class HelloWorldServlet method main.
public static void main(String[] args) {
Javalin app = Javalin.create(config -> {
config.contextPath = "/api";
config.server(() -> {
Server server = new Server();
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/test-servlet");
// Servlet will respond to all requests beginning with /test-servlet
context.addServlet(TestServlet.class, "/");
ContextHandlerCollection handlers = new ContextHandlerCollection();
handlers.setHandlers(new Handler[] { context });
server.setHandler(handlers);
return server;
});
});
app.get("/", ctx -> ctx.result("Hello Javalin World!"));
app.start(8000);
}
use of io.javalin.Javalin in project micrometer by micrometer-metrics.
the class MicrometerPlugin method apply.
@Override
public void apply(@NonNull Javalin app) {
Server server = app.jettyServer().server();
app.exception(Exception.class, EXCEPTION_HANDLER);
server.insertHandler(new TimedHandler(registry, tags, new DefaultHttpServletRequestTagsProvider() {
@Override
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response) {
String exceptionName = response.getHeader(EXCEPTION_HEADER);
response.setHeader(EXCEPTION_HEADER, null);
String uri = app.javalinServlet().getMatcher().findEntries(HandlerType.valueOf(request.getMethod()), request.getPathInfo()).stream().findAny().map(HandlerEntry::getPath).map(path -> path.equals("/") || StringUtils.isBlank(path) ? "root" : path).map(path -> response.getStatus() >= 300 && response.getStatus() < 400 ? "REDIRECTION" : path).map(path -> response.getStatus() == 404 ? "NOT_FOUND" : path).orElse("unknown");
return Tags.concat(super.getTags(request, response), "uri", uri, "exception", exceptionName == null ? "None" : exceptionName);
}
}));
new JettyServerThreadPoolMetrics(server.getThreadPool(), tags).bindTo(registry);
new JettyConnectionMetrics(registry, tags);
}
Aggregations