Skip to content

Commit

Permalink
Merge pull request #17 from 7Sageer/test
Browse files Browse the repository at this point in the history
fix: using kv instead of R2
  • Loading branch information
7Sageer authored Sep 2, 2024
2 parents d061406 + 7141f95 commit 9eb39cd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
35 changes: 20 additions & 15 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Deploy Worker

on:
workflow_dispatch:
repository_dispatch:
Expand All @@ -17,27 +18,31 @@ jobs:
with:
node-version: '21'

- name: Install dependencies
run: npm install

- name: Install wrangler
run: npm install -g wrangler
- name: Install dependencies and wrangler
run: |
npm install
npm install -g wrangler
- name: Check and Create R2 Bucket
- name: Check and Create KV Namespace
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
run: |
BUCKET_NAME="sublink-worker"
if ! wrangler r2 bucket list | grep -q "$BUCKET_NAME"; then
echo "Bucket $BUCKET_NAME does not exist. Creating..."
wrangler r2 bucket create "$BUCKET_NAME"
echo "Bucket $BUCKET_NAME created successfully."
else
echo "Bucket $BUCKET_NAME already exists. Skipping creation."
KV_NAMESPACE="SUBLINK_KV"
KV_ID=$(wrangler kv:namespace list | jq -r '.[] | select(.title == "sublink-worker-'$KV_NAMESPACE'") | .id')
if [ -z "$KV_ID" ]; then
KV_ID=$(wrangler kv:namespace create "$KV_NAMESPACE" | jq -r '.id')
fi
echo "KV_ID=$KV_ID" >> $GITHUB_ENV
- name: Update wrangler.toml
run: sed -i 's/id = "[^"]*"/id = "'$KV_ID'"/' wrangler.toml

- name: Deploy to Cloudflare Workers
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
apiToken: ${{ secrets.CF_API_TOKEN }}

- name: Display deployment logs
if: failure()
run: wrangler tail --format pretty
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转
- Sing-Box
- Clash
- Xray/V2Ray
- 支持短链接生成(基于 R2
- 支持短链接生成(基于 KV
- 浅色/深色主题切换
- 灵活的 API,支持脚本化操作
- 用户友好的 Web 界面,灵活的自定义规则
Expand All @@ -25,7 +25,7 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转

[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/7Sageer/sublink-worker)

> 注意:确保你的 Cloudflare 账户已经开通 R2 存储服务
> 注意:9月2日之后,短连接服务由 R2 迁移到 KV,请确保API令牌有对应权限
## API 文档

Expand All @@ -40,6 +40,10 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转

## 最近更新

### 2024-09-02

- 现在使用 KV 存储短链接,不再依赖 R2

### 2024-09-01

- 自定义规则现在支持以下规则:
Expand Down
20 changes: 9 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,28 @@ async function handleRequest(request) {
}
);

} if (url.pathname === '/shorten') {
} else if (url.pathname === '/shorten') {
const originalUrl = url.searchParams.get('url');
if (!originalUrl) {
return new Response('Missing URL parameter', { status: 400 });
}

const shortCode = GenerateWebPath();
await R2_BUCKET.put(`urls/${shortCode}`, originalUrl);

await SUBLINK_KV.put(shortCode, originalUrl);
const shortUrl = `${url.origin}/s/${shortCode}`;
return new Response(JSON.stringify({ shortUrl }), {
headers: { 'Content-Type': 'application/json' }
});
}

if (url.pathname.startsWith('/s/')) {
} else if (url.pathname.startsWith('/s/')) {
const shortCode = url.pathname.split('/')[2];
const originalUrl = await R2_BUCKET.get(`urls/${shortCode}`);

const originalUrl = await SUBLINK_KV.get(shortCode);
if (originalUrl === null) {
return new Response('Short URL not found', { status: 404 });
}

return Response.redirect(await originalUrl.text(), 302);
return Response.redirect(originalUrl, 302);
} else if (url.pathname.startsWith('/xray')) {
// Handle Xray config requests
const inputString = url.searchParams.get('config');
Expand Down
8 changes: 3 additions & 5 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ main = "src/index.js"
compatibility_date = "2024-07-24"
compatibility_flags = ["nodejs_compat"]

# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets
[[r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "sublink-worker"
kv_namespaces = [
{ binding = "SUBLINK_KV", id = "e55b046290924cb087f168a7085a4aee" }
]

0 comments on commit 9eb39cd

Please sign in to comment.