Search in sources :

Example 16 with Javalin

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!");
    });
}
Also used : Javalin(io.javalin.Javalin) Test(org.junit.jupiter.api.Test)

Example 17 with Javalin

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);
    });
}
Also used : Javalin(io.javalin.Javalin) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 18 with Javalin

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"));
}
Also used : Javalin(io.javalin.Javalin) ApiBuilder.post(io.javalin.apibuilder.ApiBuilder.post) ApiBuilder.get(io.javalin.apibuilder.ApiBuilder.get) Javalin(io.javalin.Javalin) ApiBuilder.patch(io.javalin.apibuilder.ApiBuilder.patch)

Example 19 with Javalin

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);
}
Also used : Javalin(io.javalin.Javalin) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 20 with Javalin

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ProcessorMetrics(io.micrometer.core.instrument.binder.system.ProcessorMetrics) HandlerEntry(io.javalin.http.HandlerEntry) TimedHandler(io.micrometer.core.instrument.binder.jetty.TimedHandler) Javalin(io.javalin.Javalin) JettyConnectionMetrics(io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics) JvmGcMetrics(io.micrometer.core.instrument.binder.jvm.JvmGcMetrics) ExceptionHandler(io.javalin.http.ExceptionHandler) JvmMemoryMetrics(io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics) NonNull(io.micrometer.common.lang.NonNull) StringUtils(io.micrometer.common.util.StringUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) TextFormat(io.prometheus.client.exporter.common.TextFormat) Server(org.eclipse.jetty.server.Server) FileDescriptorMetrics(io.micrometer.core.instrument.binder.system.FileDescriptorMetrics) Tag(io.micrometer.core.instrument.Tag) Tags(io.micrometer.core.instrument.Tags) HttpServletResponse(javax.servlet.http.HttpServletResponse) JvmHeapPressureMetrics(io.micrometer.core.instrument.binder.jvm.JvmHeapPressureMetrics) HandlerType(io.javalin.http.HandlerType) DefaultHttpServletRequestTagsProvider(io.micrometer.core.instrument.binder.http.DefaultHttpServletRequestTagsProvider) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) PrometheusMeterRegistry(io.micrometer.prometheus.PrometheusMeterRegistry) JettyServerThreadPoolMetrics(io.micrometer.core.instrument.binder.jetty.JettyServerThreadPoolMetrics) ApiBuilder.get(io.javalin.apibuilder.ApiBuilder.get) ApiBuilder.path(io.javalin.apibuilder.ApiBuilder.path) Plugin(io.javalin.core.plugin.Plugin) PrometheusConfig(io.micrometer.prometheus.PrometheusConfig) Server(org.eclipse.jetty.server.Server) JettyServerThreadPoolMetrics(io.micrometer.core.instrument.binder.jetty.JettyServerThreadPoolMetrics) TimedHandler(io.micrometer.core.instrument.binder.jetty.TimedHandler) DefaultHttpServletRequestTagsProvider(io.micrometer.core.instrument.binder.http.DefaultHttpServletRequestTagsProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) Tag(io.micrometer.core.instrument.Tag) HandlerEntry(io.javalin.http.HandlerEntry) JettyConnectionMetrics(io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics)

Aggregations

Javalin (io.javalin.Javalin)47 Test (org.junit.jupiter.api.Test)9 ApiBuilder.get (io.javalin.apibuilder.ApiBuilder.get)4 Context (io.javalin.http.Context)4 Config (org.devocative.artemis.cfg.Config)4 Server (org.eclipse.jetty.server.Server)4 OpenApiPlugin (io.javalin.plugin.openapi.OpenApiPlugin)3 Info (io.swagger.v3.oas.models.info.Info)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Context (io.javalin.Context)2 Handler (io.javalin.Handler)2 ApiBuilder.path (io.javalin.apibuilder.ApiBuilder.path)2 Plugin (io.javalin.core.plugin.Plugin)2 ExceptionHandler (io.javalin.http.ExceptionHandler)2 Handler (io.javalin.http.Handler)2 HandlerEntry (io.javalin.http.HandlerEntry)2 HandlerType (io.javalin.http.HandlerType)2