Skip to content

Commit afbe161

Browse files
committed
feat: added git enabled data backup and usage as template instructions
1 parent 4f36a53 commit afbe161

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
[![Commitizen Friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
44
[![License](https://img.shields.io/github/license/0-vortex/workers-url-shortener)](./LICENSE)
5+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2F0-vortex%2Fworkers-url-shortener.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2F0-vortex%2Fworkers-url-shortener?ref=badge_shield)
6+
7+
[![Maintainability](https://api.codeclimate.com/v1/badges/26ea74df7c6fe2f18438/maintainability)](https://codeclimate.com/github/0-vortex/workers-url-shortener/maintainability)
8+
[![Known Vulnerabilities](https://snyk.io/test/github/0-vortex/workers-url-shortener/badge.svg)](https://snyk.io/test/github/0-vortex/workers-url-shortener)
59

610
## Overview
711

8-
TBD
12+
A Cloudflare Workers script to use as a simple redirect system.
13+
14+
The [data.json](./src/data.json) file can be used as dummy input or ``git`` enabled backup for your production environment.
15+
16+
The motivation for doing such a thing is GUI or IAM enabled acces to these variables from other CloudFlare tools, essentially promoting observability.
917

1018
## Folder structure
1119

@@ -88,6 +96,46 @@ gh repo clone 0-vortex/workers-url-shortener
8896

8997
## Usage
9098

99+
### Configure KV
100+
101+
To successfully run the redirect worker we need to set up some KV namespaces.
102+
103+
Generate new `namespace_id`s for the KV keys it [wrangler.toml](./wrangler.toml) and follow the instructions:
104+
105+
```shell
106+
# dev environment
107+
wrangler kv:namespace create "REDIRECTS"
108+
wrangler kv:namespace create "REDIRECTS" --preview
109+
```
110+
111+
```shell
112+
# production environment
113+
wrangler kv:namespace create "REDIRECTS" --env production
114+
wrangler kv:namespace create "REDIRECTS" --env production --preview
115+
```
116+
117+
After you are done editing check if the changes are correct:
118+
119+
```shell
120+
wrangler kv:namespace list
121+
```
122+
123+
### Test data
124+
125+
Upload some data to the ``REDIRECTS`` namespace:
126+
127+
```shell
128+
# dev environment
129+
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json
130+
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json --preview
131+
```
132+
133+
```shell
134+
# production environment
135+
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json --env production
136+
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json --env production --preview
137+
```
138+
91139
### Local development
92140

93141
To develop locally just run:
@@ -113,3 +161,5 @@ wrangler tail
113161
## License
114162

115163
This library is released under BSD-3 license clause.
164+
165+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2F0-vortex%2Fworkers-url-shortener.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2F0-vortex%2Fworkers-url-shortener?ref=badge_large)

src/data.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"key": "lastfm",
4+
"value": "https://www.last.fm/user/zero-vortex"
5+
},
6+
{
7+
"key": "soundcloud",
8+
"value": "https://soundcloud.com/vorteks"
9+
},
10+
{
11+
"key": "twitter",
12+
"value": "https://twitter.com/0_vortex"
13+
},
14+
{
15+
"key": "angel",
16+
"value": "https://angel.co/u/vortex-1"
17+
},
18+
{
19+
"key": "bitbucket",
20+
"value": "https://bitbucket.org/tedvortex"
21+
},
22+
{
23+
"key": "crunchbase",
24+
"value": "https://www.crunchbase.com/person/ted-vortex"
25+
},
26+
{
27+
"key": "github",
28+
"value": "https://github.com/0-vortex"
29+
},
30+
{
31+
"key": "linkedin",
32+
"value": "https://uk.linkedin.com/in/tedvortex"
33+
},
34+
{
35+
"key": "stackoverflow",
36+
"value": "http://stackoverflow.com/users/757375/vortex"
37+
},
38+
{
39+
"key": "stackshare",
40+
"value": "https://stackshare.io/0_vortex"
41+
}
42+
]

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const handleRequest = async request => {
88

99
if (key !== '') {
1010
// eslint-disable-next-line
11-
const redirectTo = new URL(await VORTEX_DOT_NAME.get(key));
11+
const redirectTo = new URL(await REDIRECTS.get(key));
1212

1313
if (redirectTo) {
1414
return Response.redirect(redirectTo, 301);

wrangler.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ account_id = "4ae2b0231430be3b4de63e32381d6513"
44
zone_id = "af76b9090f6e819df8e36ba0d850b2fd"
55
workers_dev = true
66
kv_namespaces = [
7-
{ binding = "VORTEX_DOT_NAME", id = "0b8f93760c244cad97f10a6ab2cad9bb", preview_id = "8f35599f4d424d78b68117e8ed82dbed" }
7+
{ binding = "REDIRECTS", id = "a440b997322e4f74b64de848dee1d149", preview_id = "22e9d18085d44c049a481035df41c5b1" }
88
]
99

1010
[dev]
@@ -18,5 +18,5 @@ name = "url-shortener"
1818
workers_dev = false
1919
route = "get-social-with.vortex.name"
2020
kv_namespaces = [
21-
{ binding = "VORTEX_DOT_NAME", id = "bee089bc49d342bfb9b019dbffc3e415", preview_id = "6d3f646d985d469685dbc306051951ab" }
21+
{ binding = "REDIRECTS", id = "d6444b81e4be4991af05e9cbbe0b48f1", preview_id = "6a76c5899d914da5851e6f31a074692a" }
2222
]

0 commit comments

Comments
 (0)