Skip to content

Commit 8563ef0

Browse files
committed
Revert "feat: support local host"
This reverts commit cb61722.
1 parent 6b45dee commit 8563ef0

File tree

20 files changed

+245
-1748
lines changed

20 files changed

+245
-1748
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,4 @@ note.md
4141
note_zh.md
4242

4343
docs/.vitepress/dist
44-
docs/.vitepress/cache
45-
46-
.env
44+
docs/.vitepress/cache

packages/server/.env.example

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/server/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99
"init:local": "./scripts/init_local.sh"
1010
},
1111
"dependencies": {
12-
"@aws-sdk/client-s3": "^3.821.0",
13-
"better-sqlite3": "^11.10.0",
14-
"dotenv": "^16.5.0",
1512
"hono": "^4.4.8"
1613
},
1714
"devDependencies": {
1815
"@cloudflare/workers-types": "^4.20240529.0",
1916
"@hono/vite-cloudflare-pages": "^0.4.1",
2017
"@hono/vite-dev-server": "^0.12.1",
2118
"@types/bcryptjs": "^2.4.6",
22-
"@types/better-sqlite3": "^7.6.13",
2319
"@web-archive/shared": "workspace:*",
2420
"wrangler": "^3.57.2"
2521
}

packages/server/src/api/data.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Hono } from 'hono'
2-
import { ListObjectsV2Command, type _Object } from '@aws-sdk/client-s3'
32
import type { HonoTypeUserInformation } from '~/constants/binding'
43
import { getHomeChartData } from '~/model/data'
54
import result from '~/utils/result'
6-
import { S3_BUCKET_NAME } from '~/constants/config'
75

86
const app = new Hono<HonoTypeUserInformation>()
97

@@ -14,13 +12,10 @@ app.get('/page_chart_data', async (c) => {
1412
})
1513

1614
app.get('/r2_usage', async (c) => {
17-
const command = new ListObjectsV2Command({
18-
Bucket: S3_BUCKET_NAME,
19-
})
20-
const objects = await c.env.BUCKET.send(command)
15+
const res = await c.env.BUCKET.list()
2116
return c.json(result.success({
22-
size: objects.Contents?.reduce((acc: number, obj: _Object) => acc + (obj.Size ?? 0), 0) ?? 0,
23-
count: objects.Contents?.length ?? 0,
17+
size: res.objects.reduce((acc, obj) => acc + obj.size, 0),
18+
count: res.objects.length,
2419
}))
2520
})
2621

packages/server/src/api/folders.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ app.delete(
6060

6161
const { folderResult, pageResult } = await deleteFolderById(c.env.DB, id)
6262

63-
if (folderResult.changes === 0 && pageResult.changes === 0) {
63+
if (folderResult.error || pageResult.error) {
64+
throw folderResult.error || pageResult.error
65+
}
66+
67+
if (folderResult.meta.changes === 0 && pageResult.meta.changes === 0) {
6468
return c.json(result.error(400, 'No changes made'))
6569
}
6670

67-
if (folderResult.changes !== 1 || pageResult.changes !== allPages.length) {
71+
if (folderResult.meta.changes !== 1 || pageResult.meta.changes !== allPages.length) {
6872
return c.json(result.error(400, 'Some folders or pages are not deleted'))
6973
}
7074

@@ -91,8 +95,8 @@ app.put(
9195
async (c) => {
9296
const { id, name } = c.req.valid('json')
9397

94-
const success = await updateFolder(c.env.DB, { id, name })
95-
if (!success) {
98+
const sqlResult = await updateFolder(c.env.DB, { id, name })
99+
if (sqlResult.meta.changes === 0) {
96100
if (!(await checkFolderExists(c.env.DB, name))) {
97101
return c.json(result.error(400, 'Folder does not exists'))
98102
}

packages/server/src/api/pages.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ import { Hono } from 'hono'
22
import { validator } from 'hono/validator'
33
import { isNil, isNotNil, isNumberString } from '@web-archive/shared/utils'
44
import { z } from 'zod'
5-
import { GetObjectCommand } from '@aws-sdk/client-s3'
65
import type { HonoTypeUserInformation } from '~/constants/binding'
76
import result from '~/utils/result'
87
import { clearDeletedPage, deletePageById, getPageById, insertPage, queryAllPageIds, queryDeletedPage, queryPage, queryPageByUrl, queryRecentSavePage, restorePage, selectPageTotalCount, updatePage } from '~/model/page'
98
import { getFolderById, restoreFolder } from '~/model/folder'
109
import { getFileFromBucket, saveFileToBucket } from '~/utils/file'
1110
import { updateShowcase } from '~/model/showcase'
1211
import { updateBindPageByTagName } from '~/model/tag'
13-
import type { TagBindRecord } from '~/model/tag'
14-
import { S3_BUCKET_NAME } from '~/constants/config'
1512

1613
const app = new Hono<HonoTypeUserInformation>()
1714

@@ -85,11 +82,7 @@ app.post(
8582
isShowcased,
8683
})
8784
if (isNotNil(insertId)) {
88-
const updateTagResult = await updateBindPageByTagName(
89-
c.env.DB,
90-
bindTags.map(tagName => ({ tagName, pageIds: [Number(insertId)] })),
91-
[],
92-
)
85+
const updateTagResult = await updateBindPageByTagName(c.env.DB, bindTags.map(tagName => ({ tagName, pageIds: [insertId] })), [])
9386
if (updateTagResult)
9487
return c.json(result.success(null))
9588
}
@@ -375,18 +368,14 @@ app.get(
375368
return c.json(result.error(500, 'Page not found'))
376369
}
377370

378-
const command = new GetObjectCommand({
379-
Bucket: S3_BUCKET_NAME,
380-
Key: page.contentUrl,
381-
})
382-
const content = await c.env.BUCKET.send(command)
383-
if (!content.Body) {
371+
const content = await c.env.BUCKET.get(page.contentUrl)
372+
if (!content) {
384373
return c.json(result.error(500, 'Page data not found'))
385374
}
386375

387376
c.res.headers.set('cache-control', 'private, max-age=604800')
388377
return c.html(
389-
await content.Body.transformToString(),
378+
await content.text(),
390379
)
391380
},
392381
)

packages/server/src/api/showcase.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { isNotNil } from '@web-archive/shared/utils'
22
import { Hono } from 'hono'
33
import { validator } from 'hono/validator'
4-
import { GetObjectCommand } from '@aws-sdk/client-s3'
54
import type { HonoTypeUserInformation } from '~/constants/binding'
65
import { getNextShowcasePageId, getShowcaseDetailById, queryShowcase } from '~/model/showcase'
76
import type { Page } from '~/sql/types'
87
import { getBase64FileFromBucket } from '~/utils/file'
98
import result from '~/utils/result'
10-
import { S3_BUCKET_NAME } from '~/constants/config'
11-
import { getPageById } from '~/model/page'
129

1310
const app = new Hono<HonoTypeUserInformation>()
1411

@@ -50,22 +47,25 @@ app.get('/content', async (c) => {
5047
}
5148

5249
// todo refactor
53-
const page = await getPageById(c.env.DB, { id: Number(pageId), isDeleted: false })
54-
if (!page || !page.isShowcased) {
55-
return c.json(result.error(500, 'Page not found'))
50+
const pageListResult = await c.env.DB.prepare('SELECT * FROM pages WHERE isShowcased = 1 AND isDeleted = 0 AND id = ?')
51+
.bind(pageId)
52+
.all()
53+
if (!pageListResult.success) {
54+
return c.redirect('/error')
55+
}
56+
57+
const page = pageListResult.results?.[0] as Page
58+
if (!page) {
59+
return c.redirect('/error')
5660
}
5761

58-
const command = new GetObjectCommand({
59-
Bucket: S3_BUCKET_NAME,
60-
Key: page.contentUrl,
61-
})
62-
const content = await c.env.BUCKET.send(command)
63-
if (!content.Body) {
62+
const content = await c.env.BUCKET.get(page.contentUrl)
63+
if (!content) {
6464
return c.redirect('/error')
6565
}
6666

6767
return c.html(
68-
await content.Body.transformToString(),
68+
await content?.text(),
6969
)
7070
})
7171

packages/server/src/constants/binding.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import type { Database } from 'better-sqlite3'
2-
import type { S3Client } from '@aws-sdk/client-s3'
3-
41
export type Bindings = {
52
JWT_SECRET: string
6-
DB: Database
7-
BUCKET: S3Client
3+
DB: D1Database
4+
KV: KVNamespace
5+
BUCKET: R2Bucket
86
AI: Ai
97
}
108

packages/server/src/constants/config.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/server/src/model/data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Database } from 'better-sqlite3'
1+
import type { D1Database } from '@cloudflare/workers-types/experimental'
22
import { selectAllPageCount, selectPageTotalCount } from './page'
33
import { selectAllFolders } from './folder'
44

5-
async function getHomeChartData(DB: Database) {
5+
async function getHomeChartData(DB: D1Database) {
66
const folderList = await selectAllFolders(DB)
77
const folderPageCountList = await Promise.all(folderList.map(async (folder) => {
88
const pageCount = await selectPageTotalCount(DB, { folderId: folder.id })

0 commit comments

Comments
 (0)