-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
*.sql | ||
|
||
src/util/resume/index.html | ||
public/resume |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
/node_modules | ||
*-lock.* | ||
*.lock | ||
*.log | ||
.wrangler | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Template: worker | ||
|
||
[![Deploy with Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/workers-sdk/tree/main/templates/worker) | ||
|
||
A simple template for kick starting a Cloudflare worker project. | ||
|
||
## Setup | ||
|
||
启动本地 sqlite3 服务: | ||
|
||
```sh | ||
wrangler d1 execute blog_friends --local --file=./create.sql | ||
``` | ||
|
||
部署: | ||
|
||
```sh | ||
wrangler deploy index.js | ||
``` | ||
|
||
开发: | ||
|
||
```sh | ||
wrangler dev index.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
DROP TABLE IF EXISTS friends; | ||
CREATE TABLE IF NOT EXISTS friends ( | ||
id INTEGER PRIMARY KEY, | ||
name VARCHAR(100) NOT NULL, | ||
url VARCHAR(255) NOT NULL, | ||
avatar VARCHAR(255), | ||
description TEXT | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const headers = { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', | ||
'Access-Control-Max-Age': '86400', | ||
'Access-Control-Allow-Headers': 'Authorization, Content-Type, Range', | ||
} | ||
|
||
export default { | ||
/** | ||
* @param {import("@cloudflare/workers-types").Request} request | ||
* @param {{DB: import("@cloudflare/workers-types").D1Database}} env | ||
* @returns | ||
*/ | ||
async fetch(request, env) { | ||
const db = env.DB | ||
const { method, url } = request | ||
const { pathname } = new URL(url) | ||
|
||
if (method === 'GET' && pathname === '/api/list') { | ||
const friends = await db.prepare('SELECT * FROM friends').all() | ||
|
||
if (friends.success) { | ||
return new Response(JSON.stringify(friends.results), { | ||
headers: { | ||
'content-type': 'application/json', | ||
...headers, | ||
}, | ||
}) | ||
} | ||
|
||
return new Response('Failed to get friends list', { | ||
status: 500, | ||
statusText: 'Failed to get friends list', | ||
headers: { | ||
'content-type': 'text/plain', | ||
...headers, | ||
}, | ||
}) | ||
} | ||
|
||
if (method === 'POST' && pathname === '/api/add') { | ||
let name, link, avatar, description | ||
try { | ||
;({ | ||
name, | ||
link, | ||
avatar = 'https://avatars.githubusercontent.com/u/286696?v=4', | ||
description = '并没有什么可说的', | ||
} = await request.json()) | ||
} catch { | ||
return new Response('Please input name and link', { | ||
status: 400, | ||
statusText: 'Please input name and link', | ||
headers: { | ||
'content-type': 'text/plain', | ||
...headers, | ||
}, | ||
}) | ||
} | ||
const RE = /^https:\/\//i | ||
if (!RE.test(link)) { | ||
return new Response('Please input a valid link', { | ||
status: 400, | ||
statusText: 'Please input a valid link', | ||
headers: { | ||
'content-type': 'text/plain', | ||
...headers, | ||
}, | ||
}) | ||
} | ||
const exists = await db | ||
.prepare('SELECT * FROM friends WHERE url = ?') | ||
.bind(link) | ||
.run() | ||
|
||
if (exists.results.length > 0) { | ||
return new Response('Friend already exists', { | ||
status: 400, | ||
statusText: 'Friend already exists', | ||
headers: { | ||
'content-type': 'text/plain', | ||
...headers, | ||
}, | ||
}) | ||
} | ||
|
||
await db | ||
.prepare( | ||
'INSERT INTO friends (name, url, avatar, description) VALUES (?, ?, ?, ?)' | ||
) | ||
.bind(name, link, avatar, description) | ||
.run() | ||
return new Response('Add friend successfully!', { | ||
headers: { | ||
'content-type': 'text/plain', | ||
statusText: 'Add friend successfully!', | ||
...headers, | ||
}, | ||
}) | ||
} | ||
|
||
return new Response('Not Found', { | ||
headers: { | ||
status: 404, | ||
statusText: 'Not Found', | ||
'content-type': 'text/plain', | ||
...headers, | ||
}, | ||
}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name = "for_blog_friends" | ||
main = "./index.js" | ||
compatibility_date = "2022-10-10" | ||
|
||
[[d1_databases]] | ||
binding = "DB" | ||
database_name = "blog_friends" | ||
database_id = "33287eae-11dc-4b86-a3b9-46af2105c7fd" |