Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 43bf227

Browse files
committed
Cloudflare Workers examples added
1 parent 3129f8e commit 43bf227

File tree

2 files changed

+252
-2
lines changed

2 files changed

+252
-2
lines changed

README.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ $
705705

706706
$ cli4 --post file=@zone.txt /zones/:example.com/dns_records/import
707707
{
708-
"recs_added": 4,
708+
"recs_added": 4,
709709
"total_records_parsed": 4
710710
}
711711
$
@@ -756,6 +756,126 @@ if __name__ == '__main__':
756756
main()
757757
```
758758

759+
### Cloudflare Workers
760+
761+
Cloudflare Workers are described on the Cloudflare blog at
762+
[here](https://blog.cloudflare.com/introducing-cloudflare-workers/) and
763+
[here](https://blog.cloudflare.com/code-everywhere-cloudflare-workers/), with the beta release announced
764+
[here](https://blog.cloudflare.com/cloudflare-workers-is-now-on-open-beta/).
765+
766+
The Python libraries now support the Cloudflare Workers API calls. The following javascript is lifted from [https://cloudflareworkers.com/](https://cloudflareworkers.com/) and slightly modified.
767+
768+
```
769+
$ cat modify-body.js
770+
addEventListener("fetch", event => {
771+
event.respondWith(fetchAndModify(event.request));
772+
});
773+
774+
async function fetchAndModify(request) {
775+
console.log("got a request:", request);
776+
777+
// Send the request on to the origin server.
778+
const response = await fetch(request);
779+
780+
// Read response body.
781+
const text = await response.text();
782+
783+
// Modify it.
784+
const modified = text.replace(
785+
"<body>",
786+
"<body style=\"background: #ff0;\">");
787+
788+
// Return modified response.
789+
return new Response(modified, {
790+
status: response.status,
791+
statusText: response.statusText,
792+
headers: response.headers
793+
});
794+
}
795+
$
796+
```
797+
798+
Here's the website with it's simple ```<body>``` statement
799+
800+
```
801+
$ curl -sS https://example.com/ | fgrep '<body'
802+
<body>
803+
$
804+
```
805+
806+
Now lets add the script. Looking above, you will see that it's simple action is to modify the ```<body>``` statement and make the background yellow.
807+
808+
```
809+
$ cli4 --put =@- /zones/:example.com/workers/script < modify-body.js
810+
{
811+
"etag": "1234567890123456789012345678901234567890123456789012345678901234",
812+
"id": "example-com",
813+
"modified_on": "2018-02-15T00:00:00.000000Z",
814+
"script": "addEventListener(\"fetch\", event => {\n event.respondWith(fetchAndModify(event.request));\n});\n\nasync function fetchAndModify(request) {\n console.log(\"got a request:\", request);\n\n // Send the request on to the origin server.\n const response = await fetch(request);\n\n // Read response body.\n const text = await response.text();\n\n // Modify it.\n const modified = text.replace(\n \"<body>\",\n \"<body style=\\\"background: #ff0;\\\">\");\n\n // Return modified response.\n return new Response(modified, {\n status: response.status,\n statusText: response.statusText,\n headers: response.headers\n });\n}\n",
815+
"size": 603
816+
}
817+
$
818+
```
819+
820+
The following call checks that the script is associated with the zone. In this case, it's the only script added by this user.
821+
822+
```
823+
$ python3 -m cli4 /user/workers/scripts
824+
[
825+
{
826+
"created_on": "2018-02-15T00:00:00.000000Z",
827+
"etag": "1234567890123456789012345678901234567890123456789012345678901234",
828+
"id": "example-com",
829+
"modified_on": "2018-02-15T00:00:00.000000Z"
830+
}
831+
]
832+
$
833+
```
834+
835+
Next step is to make sure a route is added for that script on that zone.
836+
837+
```
838+
$ cli4 --post pattern="example.com/*" script="example-com" /zones/:example.com/workers/routes
839+
{
840+
"id": "12345678901234567890123456789012"
841+
}
842+
$
843+
844+
$ cli4 /zones/:example.com/workers/routes
845+
[
846+
{
847+
"id": "12345678901234567890123456789012",
848+
"pattern": "example.com/*",
849+
"script": "example-com"
850+
}
851+
]
852+
$
853+
```
854+
855+
With that script added to the zone and the route added, we can now see the the website has been modified because of the Cloudflare Worker.
856+
857+
```
858+
$ curl -sS https://example.com/ | fgrep '<body'
859+
<body style="background: #ff0;">
860+
$
861+
```
862+
863+
All this can be removed; hence bringing the website back to its initial state.
864+
865+
```
866+
$ cli4 --delete /zones/:example.com/workers/script
867+
12345678901234567890123456789012
868+
$ cli4 --delete /zones/:example.com/workers/routes/:12345678901234567890123456789012
869+
true
870+
$
871+
872+
$ curl -sS https://example.com/ | fgrep '<body'
873+
<body>
874+
$
875+
```
876+
877+
Refer to the Cloudflare Workers API documentation for more information.
878+
759879
## Implemented API calls
760880

761881
The **--dump** argument to cli4 will produce a list of all the call implemented within the library.

README.rst

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ on API documentation for this feature.
779779
780780
$ cli4 --post file=@zone.txt /zones/:example.com/dns_records/import
781781
{
782-
"recs_added": 4,
782+
"recs_added": 4,
783783
"total_records_parsed": 4
784784
}
785785
$
@@ -831,6 +831,136 @@ This can also be done via Python code with the following example.
831831
if __name__ == '__main__':
832832
main()
833833

834+
Cloudflare Workers
835+
~~~~~~~~~~~~~~~~~~
836+
837+
Cloudflare Workers are described on the Cloudflare blog at
838+
`here <https://blog.cloudflare.com/introducing-cloudflare-workers/>`__
839+
and
840+
`here <https://blog.cloudflare.com/code-everywhere-cloudflare-workers/>`__,
841+
with the beta release announced
842+
`here <https://blog.cloudflare.com/cloudflare-workers-is-now-on-open-beta/>`__.
843+
844+
The Python libraries now support the Cloudflare Workers API calls. The
845+
following javascript is lifted from https://cloudflareworkers.com/ and
846+
slightly modified.
847+
848+
::
849+
850+
$ cat modify-body.js
851+
addEventListener("fetch", event => {
852+
event.respondWith(fetchAndModify(event.request));
853+
});
854+
855+
async function fetchAndModify(request) {
856+
console.log("got a request:", request);
857+
858+
// Send the request on to the origin server.
859+
const response = await fetch(request);
860+
861+
// Read response body.
862+
const text = await response.text();
863+
864+
// Modify it.
865+
const modified = text.replace(
866+
"<body>",
867+
"<body style=\"background: #ff0;\">");
868+
869+
// Return modified response.
870+
return new Response(modified, {
871+
status: response.status,
872+
statusText: response.statusText,
873+
headers: response.headers
874+
});
875+
}
876+
$
877+
878+
Here's the website with it's simple ``<body>`` statement
879+
880+
::
881+
882+
$ curl -sS https://example.com/ | fgrep '<body'
883+
<body>
884+
$
885+
886+
Now lets add the script. Looking above, you will see that it's simple
887+
action is to modify the ``<body>`` statement and make the background
888+
yellow.
889+
890+
::
891+
892+
$ cli4 --put =@- /zones/:example.com/workers/script < modify-body.js
893+
{
894+
"etag": "1234567890123456789012345678901234567890123456789012345678901234",
895+
"id": "example-com",
896+
"modified_on": "2018-02-15T00:00:00.000000Z",
897+
"script": "addEventListener(\"fetch\", event => {\n event.respondWith(fetchAndModify(event.request));\n});\n\nasync function fetchAndModify(request) {\n console.log(\"got a request:\", request);\n\n // Send the request on to the origin server.\n const response = await fetch(request);\n\n // Read response body.\n const text = await response.text();\n\n // Modify it.\n const modified = text.replace(\n \"<body>\",\n \"<body style=\\\"background: #ff0;\\\">\");\n\n // Return modified response.\n return new Response(modified, {\n status: response.status,\n statusText: response.statusText,\n headers: response.headers\n });\n}\n",
898+
"size": 603
899+
}
900+
$
901+
902+
The following call checks that the script is associated with the zone.
903+
In this case, it's the only script added by this user.
904+
905+
::
906+
907+
$ python3 -m cli4 /user/workers/scripts
908+
[
909+
{
910+
"created_on": "2018-02-15T00:00:00.000000Z",
911+
"etag": "1234567890123456789012345678901234567890123456789012345678901234",
912+
"id": "example-com",
913+
"modified_on": "2018-02-15T00:00:00.000000Z"
914+
}
915+
]
916+
$
917+
918+
Next step is to make sure a route is added for that script on that zone.
919+
920+
::
921+
922+
$ cli4 --post pattern="example.com/*" script="example-com" /zones/:example.com/workers/routes
923+
{
924+
"id": "12345678901234567890123456789012"
925+
}
926+
$
927+
928+
$ cli4 /zones/:example.com/workers/routes
929+
[
930+
{
931+
"id": "12345678901234567890123456789012",
932+
"pattern": "example.com/*",
933+
"script": "example-com"
934+
}
935+
]
936+
$
937+
938+
With that script added to the zone and the route added, we can now see
939+
the the website has been modified because of the Cloudflare Worker.
940+
941+
::
942+
943+
$ curl -sS https://example.com/ | fgrep '<body'
944+
<body style="background: #ff0;">
945+
$
946+
947+
All this can be removed; hence bringing the website back to its initial
948+
state.
949+
950+
::
951+
952+
$ cli4 --delete /zones/:example.com/workers/script
953+
12345678901234567890123456789012
954+
$ cli4 --delete /zones/:example.com/workers/routes/:12345678901234567890123456789012
955+
true
956+
$
957+
958+
$ curl -sS https://example.com/ | fgrep '<body'
959+
<body>
960+
$
961+
962+
Refer to the Cloudflare Workers API documentation for more information.
963+
834964
Implemented API calls
835965
---------------------
836966

0 commit comments

Comments
 (0)