brillout / wildcard-api

Functions as API.
MIT License
368 stars 14 forks source link

Mounting wildcard at a different url #47

Closed lostpebble closed 4 years ago

lostpebble commented 4 years ago

Hey Romuald :)

I've been really struggling with getting Wildcard to work at a different URL than the one that is default /wildcard/*

For one of my next apps, I need to mount the api at a sub-path on the server - so /reports/_api/wildcard*.

I've tried messing around with serverUrl on the client, and while I can tell it to use http://localhost:8080/reports/_api/wildcard - when that endpoint is actually hit, wildcard responds that no endpoint was hit (or just ignores the request).

I've mounted wildcard at that path on my server using koa-router, and I've confirmed that it is definitely hitting wildcard, but the requests are falling through.

Looking into wildcard's code, I assume this is the problematic section:

if(
    !['GET', 'POST'].includes(method) ||
    !isPathanameBase({pathname}) && !pathname.startsWith(API_URL_BASE)
  ){
    return {isNotWildcardRequest: true, isHumanMode};
  }

It appears that wildcard will always fail unless the path starts with /wildcard ? In my case, the path is /reports/_api/wildcard

Am I maybe missing something?

Is there no way to tell the server which wildcard base path is correct? A way to set API_URL_BASE manually perhaps.

brillout commented 4 years ago

Salut Paul,

Yes, Wildcard doesn't currently support changing the base path.

I need to mount the api at a sub-path on the server

Why is that?

I'd like to understand your use case before implementing a basePath option.

Happy Easter by the way :-).

I had to think about you and Vibescout lately, it must be a rough time for you guys :(

lostpebble commented 4 years ago

Happy Easter :)

Well, in my case, I have two servers. One is my regular Vibescout app server, at www.vibescout.com/*

And I'm mounting a completely different Node.js server at www.vibescout.com/reports/* - which allows me to serve pages and develop a new "reporting" section which is completely different to the regular site. The way I've achieved this is with my GCP load balancer sending all traffic with those url patterns to the correct backend. So, my API needs to be available at the correct backend too.

My wildcard API for the reports site needs to either serve at /reports/wildcard, or preferably at the _api namespaced sub-url, reports/_api/wildcard - to keep the URL structure more nicely modularised.

On that note, even for my regular sites, I would prefer to be able to host wildcard at a different URL as well - for instance, if you look at instagram, their URL structures are instagram.com/lostpebble or whatnot - if they were using wildcard they'd probably want to namespace it to something more preferable, such as instagram.com/_api/wildcard - and have rules that names can't start with a _, as that encompasses backend paths.

I had to think about you and Vibescout lately, it must be a rough time for you guys :(

Thanks for the thought 😅 times have been tough, yea. That's why we're pivoting for the time being to releasing Covid reports for South Africa (just to keep the traffic flowing), and then perhaps online events for now. Definitely having to rethink a lot of things during these times.

Hope you're holding out alright that side!

brillout commented 4 years ago

Implemented and published at @wildcard-api/client@0.3.0 and @wildcard-api/server@0.3.0.

Docs: https://github.com/reframejs/wildcard-api#baseUrl.

If you are on an older version of Wildcard, you can see all breaking changes at https://github.com/reframejs/wildcard-api/releases.

I took the opportunity to change the default base URL from /wildcard/ to/_wildcard_api/.

Since you'll have two Wildcard APIs, you may need to have two different Wildcard client instances that have distinct serverUrl. You can already do that today but it's undocumented- let me know if you stumble upon this situation.

I usually travel to Paris every summer, if the curfew situation improves, then let's grab a drink :-).

lostpebble commented 4 years ago

Thanks @brillout ! Seems to be working perfect.

And especially thanks for making it as easy as setting baseUrl directly instead of serverUrl - really makes the configuration a lot easier because you don't need to account for which host to point it to localhost:8080 or the live version https://....

Since you'll have two Wildcard APIs, you may need to have two different Wildcard client instances that have distinct serverUrl. You can already do that today but it's undocumented- let me know if you stumble upon this situation.

Cool, yea shouldn't be a problem, as the regular Vibescout app will point to /_wildcard_api/ now, and this one is pointing to /reports/_api/wildcard. But definitely something to keep in mind in the future.

I usually travel to Paris every summer, if the curfew situation improves, then let's grab a drink :-).

For sure! Hopefully we all get a bit of free movement soon enough. The weather has been spectacular lately - its so hard having to be stuck indoors instead of enjoying a few cold ones next to the Seine!

brillout commented 4 years ago

Yes serverUrl and baseUrl have different uses case, it's prob better to separate their configuration.

The weather has been spectacular lately [...] few cold ones next to the Seine!

Yes! And with some sweet potatoes :-).