Add Cloudflare KV Storage

This commit is contained in:
René
2024-04-09 09:19:47 +02:00
parent 1527f88e51
commit 64b08d48dc
4 changed files with 22 additions and 19 deletions

View File

@@ -1,14 +1,14 @@
#!/bin/bash #!/bin/bash
# generate index.ts # generate cloudflarekv/index.ts
cat <<EOF > index.ts cat <<EOF > cloudflarekv/index.ts
export default { async fetch(Request, env) { export default { async fetch(Request, env) {
const namespace = env.TEST_NAMESPACE1; const namespace = env.TEST_NAMESPACE1;
if (Request.url === "http://localhost:8787/health") { if (Request.url === "http://localhost:8787/health") {
return new Response("Success"); return new Response("Success");
} }
if (Request.url === "http://localhost:8787/writeworkerskvkeyvaluepair") { if (Request.url === "http://localhost:8787/writeworkerskvkeyvaluepair") {
const res = await Request.json(); const res = await Request.json();
@@ -27,14 +27,14 @@ export default { async fetch(Request, env) {
else if (Request.url === "http://localhost:8787/deleteworkerskvpairbykey") { else if (Request.url === "http://localhost:8787/deleteworkerskvpairbykey") {
const res = await Request.json(); const res = await Request.json();
const { key } = res; const { key } = res;
await DeleteWorkersKVPairByKey(namespace, key); await DeleteWorkersKVPairByKey(namespace, key);
return new Response(key) return new Response(key)
} }
else if (Request.url === "http://localhost:8787/getworkerskvvaluebykey") { else if (Request.url === "http://localhost:8787/getworkerskvvaluebykey") {
const key = (await Request.json()).key; const key = (await Request.json()).key;
const res = await GetWorkersKVValueByKey(namespace, key); const res = await GetWorkersKVValueByKey(namespace, key);
return new Response(res); return new Response(res);
} }
@@ -44,10 +44,10 @@ export default { async fetch(Request, env) {
const { keys } = res; const { keys } = res;
const newKeys = keys.filter(x => x.length > 0); const newKeys = keys.filter(x => x.length > 0);
await DeleteWorkersKVEntries(namespace, newKeys); await DeleteWorkersKVEntries(namespace, newKeys);
return new Response("Success") return new Response("Success")
} }
} }
} }
const GetWorkersKVValueByKey = async (NAMESPACE, key) => { const GetWorkersKVValueByKey = async (NAMESPACE, key) => {
@@ -64,7 +64,7 @@ const WriteWorkersKVKeyValuePair = async (NAMESPACE, key, val) => {
const DeleteWorkersKVPairByKey = async (NAMESPACE, key) => { const DeleteWorkersKVPairByKey = async (NAMESPACE, key) => {
await NAMESPACE.delete(key); await NAMESPACE.delete(key);
return "Delete Successfully" return "Delete Successfully"
} }
@@ -77,7 +77,7 @@ const ListWorkersKVKeys = async (NAMESPACE, limit, prefix, cursor) => {
const DeleteWorkersKVEntries = async (NAMESPACE, keys) => { const DeleteWorkersKVEntries = async (NAMESPACE, keys) => {
for (let key of keys) { for (let key of keys) {
await NAMESPACE.delete(key); await NAMESPACE.delete(key);
} }
return "Delete Successfully" return "Delete Successfully"
} }
@@ -85,10 +85,10 @@ const DeleteWorkersKVEntries = async (NAMESPACE, keys) => {
EOF EOF
echo "wrangler.toml generated" echo "index.ts generated"
# generate wrangler.toml # generate cloudflarekv/wrangler.toml
cat <<EOF > wrangler.toml cat <<EOF > cloudflarekv/wrangler.toml
main = "index.ts" main = "index.ts"
kv_namespaces = [ kv_namespaces = [

View File

@@ -100,7 +100,8 @@ jobs:
- name: Install Cloudflare Worker - name: Install Cloudflare Worker
run : | run : |
.github/scripts/initialize-wrangler.sh .github/scripts/initialize-wrangler.sh
npx wrangler dev & cd cloudflarekv && npx wrangler dev &
npx wait-on tcp:8787
- name: Install Coherence - name: Install Coherence
run: | run: |

View File

@@ -24,18 +24,18 @@ jobs:
- name: Setup Go - name: Setup Go
uses: actions/setup-go@v5 uses: actions/setup-go@v5
with: with:
go-version: ${{ matrix.go-version }} go-version: ${{ matrix.go-version }}
- name: Setup Node.js - name: Setup Node.js
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: '18' node-version: '18'
- name: Write code - name: Start Wrangler Dev
run: | run: |
chmod +x .github/scripts/initialize-wrangler.sh
.github/scripts/initialize-wrangler.sh .github/scripts/initialize-wrangler.sh
npx wrangler dev & cd cloudflarekv && npx wrangler dev &
npx wait-on tcp:8787
- name: Run Go Tests - name: Run Go Tests
run: cd cloudflarekv && go test ./... -race run: cd cloudflarekv && go test ./... -v -race

2
cloudflarekv/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
index.ts
wrangler.toml