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

View File

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

View File

@@ -24,18 +24,18 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
go-version: ${{ matrix.go-version }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Write code
- name: Start Wrangler Dev
run: |
chmod +x .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
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