couchapp: Different rewrites for URLs with and without forward slash - url-rewriting

Basically, I want two different rewrite rules for these URLs: http://127.0.0.1:5984/test/_design/myapp/_rewrite/docs/abc
http://127.0.0.1:5984/test/_design/myapp/_rewrite/docs/abc/
The first one will be rewritten as http://127.0.0.1:5984/test/_design/myapp/_show/single/abc while the second one will be rewritten as http://127.0.0.1:5984/test/_design/myapp/_list/container/all
I tried doing this:
{
"---": "Container Retrieval",
"method": "GET",
"from": "/docs/*/",
"to": "/_list/basic-container/all"
}
Both of the URLs (with and without the forward slash) redirect to http://127.0.0.1:5984/test/_design/myapp/_list/container/all. It seems like couchDB ignores the forward slash at the end of the URL.
What are the possible solutions to this problem?

The asterisk catches the whole following path. Try the following instead:
{
"method": "GET",
"from": "/docs/:id",
"to": "/_show/single/:id"
},
{
"method": "GET",
"from": "/docs/:id/",
"to": "/_list/basic-container/all"
}

Related

How can I setup proxy using Vercel

My API it's running under another domain.. and I'm trying to configure proxy with Vercel ..
The app it's making requests to /api/test.json so I tried to... on vercel configuration
"redirects": [
{
"source": "/api/test.json",
"destination": "https://myapi.com/test.json",
}
],
"rewrites": [
{
"source": "/(.*)",
"destination": "/index.html"
}
]
And I received 404 from /api/test.json
Use the wildcard path matching :path* syntax:
// in vercel.json:
// for example proxying /api/ → https://backend-endpoint/
{
"rewrites": [
{
"source": "/api/:path*",
"destination": "https://backend-endpoint/:path*"
},
{
"source": "/api/:path*/",
"destination": "https://backend-endpoint/:path*/"
}
]
}
NOTE: You need both very identical objects under rewrites array as above in order to make things work properly. The example in the documentation is only the one without the trailing slash and it won't convert (for example) /api/auth/login/ to https://backend-endpoint/auth/login/, that example can only convert /api/auth/login to https://backend-endpoint/auth/login (without trailing slash /)
(it took me a day to realize that trailing slash / is actually very important).
Simply use rewrites
"rewrites": [
{
"source": "/api/test.json",
"destination": "https://myapi.com/test.json",
}
]
Then in your application
httpAgent
.get('/api/test.json)
.then(res => { console.log(res) })

Wiremock Standalone with Dynamic response

I have a standalone instance of Wiremock server. The mappings are stored as json files under the mappings folder. I have a POST request that needs to return a dynamic ID(integer) in the response. Is there a way to configure this in the json file?
To make the above examples work, I had to run the standalone jar with the --global-response-templating. Then I saw, for example, {{now}} working which is what I wanted. Not sure if the documentation specifies this -- I tried the always-useful --help.
In WireMock there are a number of response template helper functions for generating random strings. In the below example I'm using the one for generating a UUID, but several other options exist.
Mapping file: dynamic_id.json
{
"request": {
"method": "POST",
"url": "/dynamic_id"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 200,
"body": "{{randomValue type='UUID'}}",
"transformers": ["response-template"]
}
}
Using an empty POST http://wiremock/dynamic_id will return an id similar to: c2e6bf32-c9a3-45c0-b988-94fad04cc7a2.
Start WireMock:
java -jar wiremock-standalone-2.18.0.jar --port 8181 --verbose --local-response-templating
This seems like a perfect use-case for OpenTable's Wiremock Body Transformer.
It can be easily integrated with the Standalone Server like this:
java -cp "wiremock-body-transformer-1.1.6.jar:wiremock-2.3.1-standalone.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions com.opentable.extension.BodyTransformer
And allows you to easily specify a dynamic variable that you would want to match in the response.
Here is an example to get a random integer without having to specify anything in the request, however if you need to match a specific variable in the request to the response, then that is also very doable with this extension and numerous examples can be found in the readme.
{
"request": {
"method": "POST",
"urlPath": "/transform",
},
"response": {
"status": 200,
"body": "{\"randomInteger\": \"$(!RandomInteger)\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["body-transformer"]
}
}
As #Jeff mentions, If you are running it as a stand-alone process, you need to add this flag --global-response-templating. This will apply templating to each and every reponse. However, few of your responses may be jsut plain json with no templating required.
In that case use --local-response-templating. and add this field inside reponse json:
response:{
"transformers": ["response-template"]
}

elasticsearch regex query to include forward slash

I have the following records in elastic search
{
"label": "/home and garden/home furnishings",
"score": "0.731174"
},
{
"label": "/travel/vacation rentals",
"score": "0.601932"
},
{
"label": "/travel/vacation rentals",
"score": "0.657443"
},
{
"label": "/home and garden/gardening and landscaping/yard and patio",
"score": "0.707792"
}
Now i want to make a query to get all taxonomy labels that start with "/travel" and i want the data only till the third forward slash
example if we take
"label": "/home and garden/gardening and landscaping/yard and patio"
then i want the data only till
/home and garden/gardening and landscaping/
I tried some of the queries for it for partial match like:
{
"_source":["taxonomy"],
"from" : 0,
"size" : 100,
"query": {
"regexp":{
"taxonomy.label":{
"value":"/travel.*",
"boost":1.2
}
}
}
}
But it does not seem to take the forward slashes as soon as i give slashes it stops giving any results, i want to know if this is possible or not and if it is then how do i proceed with this query?
Any help is appreciated.
You have two problems. first is querying documents which starts with travel. For that you can try path_hierarchy from ElasticSearch. You can read more here. This will allow you to store your path '/travel/vacation rentals' in the form of
/travel
/travel/vacation rentals
after than you can do direct match query to that field.
For the second part you can try scripts, but be careful with them as enabling inline scripts can result in some unethical access if exposed to outside world.
You can go safe by using scripts kept in specific scripts folder. You can read more on scripts and how to make it secure here.

CouchDB URL Rewriting for SEO

I'm trying to create an entire site hosted purely on CouchDB (no nginx reverse proxy either) using a lot of client side Jquery/AJAX magic. Now I'm in the process of making it SEO friendly. I'm using vhosts and URL rewrites to route traffic from the root to my index.html file:
vhost:
example.com /dbname/_design/dd/_rewrite/
In my rewrite definition:
rewrites:[
{
"from": "/db/*",
"to": "/../../../*",
"query": {
}
},
{
"from": "/",
"to": "../../static/index.html",
"query": {
}
}
]
When optimizing a site for SEO, Google requires you to do a few things:
Use the hashbang (#!) in your friendly URL to tell the web crawler that you are an AJAX site with web crawlable material: http://example.com/index.html#!home
Use an http query argument to provide an HTML escaped fragment of that AJAX page: http://example.com/index.html?_escaped_fragment=home
I tried the following with no luck:
rewrites:[
{
"from": "/db/*",
"to": "/../../../*",
"query": {
}
},
{
"from": "/",
"to": "../../static/index.html",
"query": {
}
}, /* FIRST ATTEMPT */
{
"from": "/?_escaped_fragment=:_escaped_fragment",
"to": "/_show/escaped_fragment/:_escaped_fragment",
"query": {
}
}, /* SECOND ATTEMPT */
{
"from": "/?_escaped_fragment=*",
"to": "/_show/escaped_fragment/*",
"query": {
}
}, /* THIRD ATTEMPT */
{
"from": "/",
"to": "/_show/escaped_fragment/:_escaped_fragment",
"query": {
}
}
]
From what I've seen, CouchDB's URL rewriter is not capable of distinguishing the difference between a URLs with args and no args. Has anyone had luck creating such a rule with CouchDB URL rewrites?
I don't have a answer to the question, but I've developed a solution for the bigger problem of making crawlable sites hosted on CouchDB. It is a system that makes use of Facebook's React, list and show functions, ajax on the client and window.history to render the same HTML components filled with data at CouchDB and at the browser:
https://github.com/fiatjaf/reactive-couch
This solution doesn't need the hashbang, because for each unique URL the browser navigates to, using ajax and window.history or simple links (be it _list/listName/viewName/_show/displayKind/c305ee4d-8611-4e08-b9d3-3318835632a9 or something rewritten as /name//kind/c305ee4d-8611-4e08-b9d3-3318835632a9), the server can render the pertinent content.

CouchDB Rewriting

I have a URL for my website, which is as follows: http://ashgavs.cloudant.com/site/_design/AshGavsCouch/main/index.html
I added a field to my design document called rewrites and it's as follows:
[
{
"from": "",
"to": "main/index.html"
}
]
However when I go to this URL: http://ashgavs.cloudant.com/site/_design/AshGavsCouch/ . The rewrite isn't happening. Am I doing it wrong? Is there a way to see where its rewriting to so that I can debug this?
As mentioned in the docs the default rewrite is from /site/_design/AshGavsCouch/_rewrite. If you want the rewrite to be from /site/_design/AshGavsCouch/ then you need to specify the URL in your "from" field.

Resources