helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.52k stars 565 forks source link

Routing V2 API design #3359

Open Verdent opened 3 years ago

Verdent commented 3 years ago

Create new or update new Routing API design of the one used in PoC of Tomas Langer.

lambdaupb commented 2 years ago

May I suggest supporting some sort of RoutePath interface that can be put into an annotation when implemented by an enum constant and also used to reverse-route links.

This makes refactoring so much easier.

This is only a concept, I have only tried this in smaller projects.

    interface RoutePath<X extends Enum> {
        String pattern();

        default String link(Map<X, String> params) {
            this.pattern ... replace params
            ... check all params defined
        }

        default String link(X param, String value) {
            return link(Map.of(param, value));
        }
        default String link(X param1, String value1, X param2, String value2) {
            return link(Map.of(param1, value1, param2, value2));
        }
    }

    enum MyParams {
        BOOK,
        CUSTOMER
    }

    class Controller {

        @POST(MyPaths.HOME)
        void home(ServerRequest req, ServerResponse resp) {
            String link = MyPaths.CUSTOMER.link(MyParams.CUSTOMER, "123");
        }
    }

    enum MyPaths implements RoutePath<MyParams> {
        HOME("/home"),
        CUSTOMER("/customer/{customer}");

        private final String pattern;

        MyPaths(String pattern) {
            this.pattern = pattern;
        }

        @Override
        public String pattern() {
            return this.pattern;
        }
    }
romain-grecourt commented 2 years ago

@lambdaupb in order words typed route paths. This would mean overload Routing.Rules @tomas-langer thoughts ?

lambdaupb commented 2 years ago

The one problem I have is nested paths that appear in controllers with a path and controller methods with their own.

I ended up registering all controllers (Helidon Services, very unfortunate naming) at "/" and use the full path on the method.

I hope the path matching is smart and uses some sort of trie.

This splits up the path and makes generating reverse routes/links hard since I would have to again puzzle together the nested path elements manually.

romain-grecourt commented 2 years ago

Can you describe your use-case with example routes ?

lilac commented 12 months ago

To be consistent with http2 and WebSocket, it's better to add another task.