Skip to content

Commit

Permalink
cloudflare d1 for blog friends
Browse files Browse the repository at this point in the history
  • Loading branch information
fwqaaq committed Jun 8, 2024
1 parent b9ea0b6 commit d562379
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?
*.sql

src/util/resume/index.html
public/resume
6 changes: 4 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"src/util/**/*.js",
"main.js",
"public/JavaScript/*.js",
"scripts/*.ts"
"scripts/*.ts",
"**/*.json"
]
},
"imports": {
Expand All @@ -34,6 +35,7 @@
"yaml": "https://deno.land/std@0.194.0/yaml/mod.ts",
"Command": "https://deno.land/x/cliffy@v1.0.0-rc.3/command/mod.ts",
"datetime": "https://deno.land/std@0.200.0/datetime/format.ts",
"lightningcss": "https://cdn.jsdelivr.net/npm/lightningcss-wasm/+esm"
"lightningcss": "https://cdn.jsdelivr.net/npm/lightningcss-wasm/+esm",
"@cloudflare/workers-types": "https://cdn.jsdelivr.net/npm/@cloudflare/workers-types@latest/index.ts"
}
}
7 changes: 7 additions & 0 deletions src/util/blog_friends/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
/node_modules
*-lock.*
*.lock
*.log
.wrangler
/dist
25 changes: 25 additions & 0 deletions src/util/blog_friends/README.md
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
```
8 changes: 8 additions & 0 deletions src/util/blog_friends/create.sql
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
);
111 changes: 111 additions & 0 deletions src/util/blog_friends/index.js
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,
},
})
},
}
8 changes: 8 additions & 0 deletions src/util/blog_friends/wrangler.toml
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"

0 comments on commit d562379

Please sign in to comment.