-
-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use regexp for harbor endpoints registration #2599
Conversation
This is very nice, but I am not sur that we want to use regexps by default. The thing is that a mountpoint of |
I have a bigger change pending that will make the whole thing much closer to how express works in node. Default operator will use absolute strings e.g. |
I'd like to see
|
63b9adf
to
7ae4444
Compare
@smimram @vitoyucepi I believe that this one is ready. Description and documentation are fully updated. |
484c3c1
to
c5a9911
Compare
I have not read the PR in details (please leave me some time to do so) but I have two comments from the description. Taking one record parameter ( However, I am not fan of taking the pre-filled response as an answer. What was wrong with the previous |
The main reasons for me are:
I think that, from a user perspective, this is much more user-friendly than using a one-size-fits-all wrapper over the low-level HTTP response string.. |
Code for the http response wrapper is here: https://github.com/savonet/liquidsoap/pull/2599/files#diff-4106ab6d9bc3a4ec0b7b38bb791ee42d1f950031f2e208c0aae454484da075fdR272-R322 |
abfa42c
to
7734750
Compare
Ok, I've reviewed everything and here's what I'm thinking here:
Thus, I have:
I think that this is a good compromise and that we should merge this now! |
1f4eeb5
to
97d523e
Compare
2e8941b
to
a502de9
Compare
This PR completely revamps the harbor HTTP endpoint implementation:
Here's the new doc:
Simple API
The
harbor.http.register.simple
function provides a simple, easy to use registration API for quickHTTP response implementation. This function receives a record describing the request and returns
the HTTP response.
For convenience, a HTTP response builder is provided via
harbor.http.response
. Here's an example:where:
port
is the port where to receive incoming connectionsmethod
is for the http method (or verb), one of:"GET"
,"PUT"
,"POST"
,"DELETE"
,"OPTIONS"
and"HEAD"
path
is the matched path. It can include named fragments, e.g."/users/:id/collabs/:cid"
. Named named framents are passed viarequest.query
, for instance:req.query["cid"]
.Node/express API
The
harbor.http.register
function offers a higher-level API for advanced HTTP response implementation.Its API is very similar to the node/express API. Here's an example:
where:
port
is the port where to receive incoming connectionsmethod
is for the http method (or verb), one of:"GET"
,"PUT"
,"POST"
,"DELETE"
,"OPTIONS"
and"HEAD"
path
is the matched path. It can include named fragments, e.g."/users/:id/collabs/:cid"
. Matched named framents are passed viarequest.query
, for instance:req.query["cid"]
.The handler function receives a record containing all the information about the request and fills
up the details about the response, which is then used to write a proper HTTP response to the client.
Named fragments from the request path are passed to the response
query
list.Advanced usage
All registration functions have a
.regexp
counter part, e.g.harbor.http.register.simple.regexp
. These function accepta full regular expression for their
path
argument. Named matches on the regular expression are also passed via the request'squery
parameter.It is also possible to directly interact with the underlying socket using the
simple
API:Examples
These functions can be used to create your own HTTP interface. Some examples
are:
Redirect Icecast's pages
Some source clients using the harbor may also request pages that
are served by an icecast server, for instance listeners statistics.
In this case, you can register the following handler:
Get metadata
You can use harbor to register HTTP services to
fecth/set the metadata of a source. For instance,
using the JSON export function
json.stringify
:Once the script is running,
a GET request for
/getmeta
at port7000
returns the following:
Set metadata
Using
insert_metadata
, you can register a GET handler thatupdates the metadata of a given source. For instance:
Now, a request of the form
http://server:7000/setmeta?title=foo
will update the metadata of source
s
with[("title","foo")]
. Youcan use this handler, for instance, in a custom HTML form.
Also worth noting, I don't think that you can provide backward compatible APIs here or, perhaps more accurately, I think we should avoid it. Adapting existing use of the harbor HTTP endpoints shouldn't be too difficult and I'd rather encourage that.
This PR also unifies the terminology used for HTTP queries between harbor and libcurl (
http.{get,post,put,...}
), in particular it switches to:status_code
/status_message
http_version
Lastly, the PR introduces support for named match for regexp in the language by adding a
groups
method to the list of matches. This is pretty much on-par with the javascript implementation.TODO