mirror of
https://github.com/gofiber/storage.git
synced 2025-10-15 05:00:55 +08:00
Add Cloudflare KV Storage
This commit is contained in:
24
.github/scripts/initialize-wrangler.sh
vendored
24
.github/scripts/initialize-wrangler.sh
vendored
@@ -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 = [
|
||||||
|
3
.github/workflows/benchmark.yml
vendored
3
.github/workflows/benchmark.yml
vendored
@@ -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: |
|
||||||
|
12
.github/workflows/test-cloudflarekv.yml
vendored
12
.github/workflows/test-cloudflarekv.yml
vendored
@@ -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
2
cloudflarekv/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
index.ts
|
||||||
|
wrangler.toml
|
Reference in New Issue
Block a user