jetty.project icon indicating copy to clipboard operation
jetty.project copied to clipboard

Jetty 12 - Server/Handler DSL

Open sbordet opened this issue 2 years ago • 4 comments

Jetty version(s) 12+

Enhancement Description The idea is to provide an easy to use, almost single-line, builder-like, DSL to build a Server instance and map HTTP methods and paths to Java method references.

Just syntactic sugar, but sometimes easier than figuring out what to write the next line, because an IDE will prompt what is available, and it's easier to pick .listen(), or .route().

A mocked API could be:

Server
    .listen(8080, new HttpConnectionFactory())
    .listen(8443, <a builder for ConnectionFactory>)
    .routes()
        .route(HttpMethod.GET, "/welcome", this::welcome)
        .route(HttpMethod.POST, "/form", this::handleFormUpload)
        .build()
    .build()
    .start();

sbordet avatar Feb 15 '23 19:02 sbordet

A halfway step could be to make all our existing setters return this, so that they can be used in fluent style:

    Server server = new Server();
    server.
        .addConnector(new ServerConnector(server, new HttpConnectionFactory()).setPort(8080));
        .addConnector(new ServerConnector(server, new SslContextFacotory(), new SslConnectionFactory(), new HttpConnectionFactory()).setPort(8080));
        .addHandler(new ContextHandlerCollection()
            .addHandler(new ContextHandler("/foo").setHandler(...))
            .addHandler(new ContextHandler("/bar").setHandler(...)))
       .start();
        

gregw avatar Feb 16 '23 09:02 gregw

A halfway step could be to make all our existing setters return this

I think it may mess up reflection so I would prefer to have that separated.

Also, I would like to avoid having to allocate ServerConnector (replaced by just listen()), ContextHandlerCollection (replaced by just route()) to ease up new users that don't have those concepts already.

sbordet avatar Feb 16 '23 10:02 sbordet

Ok for something separate

I'm not so ok on calling connectors listeners, adding different names for the same thing will be confusing.

We could hide the CHC, but the DSLs of other servers don't do that.

I think we need to try a few experiments

gregw avatar Feb 16 '23 11:02 gregw

This issue has been automatically marked as stale because it has been a full year without activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Feb 17 '24 00:02 github-actions[bot]