jsdw / weave

A simple CLI router for wiring together several sources behind a single HTTP endpoint
MIT License
144 stars 4 forks source link

Leading or trailing `and`? #10

Closed vi closed 4 years ago

vi commented 4 years ago

Shall Weave support leading or trailing and in command line?:

OPTS=
OPTS="$OPTS =8080/q to http://127.0.0.1:1234/ and"
OPTS="$OPTS =8080/w to http://127.0.0.1:1235/ and"
OPTS="$OPTS =8080/e to http://127.0.0.1:1236/ and"
weave $OPTS

Or can and be made completely optional?

jsdw commented 4 years ago

I'm not super keen on allowing a trailing and because in many cases that is more likely to signal a usage error than anything else.

Can you give an example of a way you'd like to use weave that would benefit from a trailing and? In the above example it would be easy to avoid it; do you plan on doing some more complex programmatic usage?

vi commented 4 years ago

In the above example it would be easy to avoid it

How would it be avoided without making any line (first or last) special and without additional code?


Another idea is a dummy forward, so that the code can be like this:

OPTS=dummy
OPTS="and $OPTS =8080/q to http://127.0.0.1:1234/"
OPTS="and $OPTS =8080/w to http://127.0.0.1:1235/"
OPTS="and $OPTS =8080/e to http://127.0.0.1:1236/"
weave $OPTS

This would also allow easy popularion of routes from scripts.

One more idea: OPTS="=8080 to dummy" to allow starting Weave with effectively zero routes - it should still listen to the port, but simply return 404 for all requests.

jsdw commented 4 years ago

How would it be avoided without making any line (first or last) special and without additional code?

I'm not super keen on allowing a trailing or leading 'and' as I think it would often be a sign of user error in typical weave usage.

The idea of a dummy/noop/sink route is interesting though and, as you say, would provide a workaround.

Do you have any concrete reasons for binding to a port and returning a status code? I would be tempted to allow you to forward to something like status://404 so that you can return whatever status code you like. This aligns with how I would handle other types of resources like WebSockets (I should probably allow file:// as well)

vi commented 4 years ago

Yes, returning arbitrary status codes also fits the bill.

The main idea is forming list of routes programmatically, from multiple optional sources. Example:

ROUTES="8080 to status://404"
if [ "$FOO" = "yes" ]; then
    ROUTES="$ROUTES and =8080/foo to http://foo/"
fi
if [ "$BAR" = "yes" ]; then
    ROUTES="$ROUTES and =8080/bar to http://bar/"
fi
weave $ROUTES

Startup error should be avoided even in no/no case.

jsdw commented 4 years ago

I've implemented a version of this (support for "nothing" in place of a route, and support for "statuscode://204" style syntax as the destination route (or "nothing" there as an alias for "statuscode://404"). in the latest release (v0.5.1), so I'll close this. Let me know what you think! :)

vi commented 4 years ago

only valid HTTP status codes are allowed

Why so? I'd expect it to support any 2xx, 4xx and 5xx status codes, including custom non-standard. Weave looks like a proper tool for prototyping that.

By the way, does it support 3xx status codes with a Location: header? It can be done low-levelish with an pseudo-URL like statuscode://302?location=http%3a%2d%2fexample.com%2f, maybe with a user-friendly alias like redirect:http://example.com/

jsdw commented 4 years ago

Valid as in, a status code that exists rather than one that signals "ok" (2xx) :)

Weave doesn't support headers; redirects are currently passed back to the browser but I have pondered allowing weave to handle them!

vi commented 4 years ago

Redirects are currently passed back to the browser but I have pondered allowing weave to handle them!

I mean emitting new redirects to location specified on command line.

Maybe a separate Github issues shall be for redirect handling.

jsdw commented 4 years ago

Yep, feel free to create a new issue :)