mirror of
https://github.com/fmhy/edit.git
synced 2026-03-11 08:55:38 +00:00
feat(api): ratelimiting
This commit is contained in:
parent
26213d9f91
commit
1148023b1a
10 changed files with 104 additions and 92 deletions
17
api/middleware/ratelimit.ts
Normal file
17
api/middleware/ratelimit.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export default defineEventHandler(async (event) => {
|
||||
const { cloudflare } = event.context
|
||||
|
||||
// FIXME: THIS IS NOT RECOMMENDED. BUT I WILL USE IT FOR NOW
|
||||
// Not recommended: many users may share a single IP, especially on mobile networks
|
||||
// or when using privacy-enabling proxies
|
||||
const ipAddress = getHeader(event, 'CF-Connecting-IP') ?? ''
|
||||
|
||||
const { success } = await // KILL YOURSELF
|
||||
(cloudflare.env as unknown as Env).RATE_LIMITER.limit({
|
||||
key: ipAddress
|
||||
})
|
||||
|
||||
if (!success) {
|
||||
throw createError('Failure – global rate limit exceeded')
|
||||
}
|
||||
})
|
||||
|
|
@ -13,99 +13,61 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
const files = (
|
||||
[
|
||||
'adblockvpnguide.md',
|
||||
'ai.md',
|
||||
'android-iosguide.md',
|
||||
'audiopiracyguide.md',
|
||||
'beginners-guide.md',
|
||||
'devtools.md',
|
||||
'downloadpiracyguide.md',
|
||||
'edupiracyguide.md',
|
||||
'file-tools.md',
|
||||
'gaming-tools.md',
|
||||
'gamingpiracyguide.md',
|
||||
'img-tools.md',
|
||||
'internet-tools.md',
|
||||
'linuxguide.md',
|
||||
'miscguide.md',
|
||||
'non-english.md',
|
||||
'readingpiracyguide.md',
|
||||
'social-media-tools.md',
|
||||
'storage.md',
|
||||
'system-tools.md',
|
||||
'text-tools.md',
|
||||
'torrentpiracyguide.md',
|
||||
'unsafesites.md',
|
||||
'video-tools.md',
|
||||
'videopiracyguide.md'
|
||||
] as const
|
||||
).map((file) => ({
|
||||
name: file,
|
||||
url: `https://raw.githubusercontent.com/fmhy/edit/main/docs/${file}`
|
||||
}))
|
||||
|
||||
import { fetcher } from 'itty-fetcher'
|
||||
import { createStorage } from 'unstorage'
|
||||
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
|
||||
export default defineCachedEventHandler(
|
||||
async (event) => {
|
||||
let body = '<!-- This is autogenerated content, do not edit manually. -->\n'
|
||||
|
||||
// Look inside the docs directory
|
||||
const GITHUB_REPO = 'https://api.github.com/repos/fmhy/edit/contents/docs/'
|
||||
const EXCLUDE_FILES = [
|
||||
'README.md',
|
||||
'index.md',
|
||||
'feedback.md',
|
||||
'posts.md',
|
||||
'sandbox.md'
|
||||
]
|
||||
const EXCLUDE_DIRECTORIES = ['posts/']
|
||||
|
||||
interface File {
|
||||
name: string
|
||||
path: string
|
||||
sha: string
|
||||
size: number
|
||||
url: string
|
||||
html_url: string
|
||||
git_url: string
|
||||
download_url: string | null
|
||||
type: string
|
||||
_links: {
|
||||
self: string
|
||||
git: string
|
||||
html: string
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const markdownStorage = createStorage({
|
||||
driver: cloudflareKVBindingDriver({ binding: 'STORAGE' })
|
||||
})
|
||||
|
||||
let body = '<!-- This is autogenerated content, do not edit manually. -->\n'
|
||||
const f = fetcher({
|
||||
headers: {
|
||||
'User-Agent': 'taskylizard'
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
// Fetch the list of files in the repository
|
||||
const indexCacheKey = "INDEX"
|
||||
let files = await markdownStorage.getItem<File[]>(indexCacheKey)
|
||||
|
||||
if (!files) {
|
||||
files = await f.get(GITHUB_REPO)
|
||||
await markdownStorage.setItem(indexCacheKey, files, { ttl: 60 * 60 * 24 * 7 })
|
||||
}
|
||||
|
||||
// Filter out the excluded files and non-markdown files
|
||||
const markdownFiles = files.filter((file: File) => {
|
||||
const isExcludedFile = EXCLUDE_FILES.includes(file.name)
|
||||
const isInExcludedDirectory = EXCLUDE_DIRECTORIES.some((dir) =>
|
||||
file.path.startsWith(dir)
|
||||
)
|
||||
const isMarkdownFile = file.name.endsWith('.md')
|
||||
|
||||
return isMarkdownFile && !isExcludedFile && !isInExcludedDirectory
|
||||
})
|
||||
|
||||
// Fetch and concatenate the contents of the markdown files with caching
|
||||
const contents = await Promise.all(
|
||||
markdownFiles.map(async (file: File) => {
|
||||
const cached = await markdownStorage.getItem(file.name)
|
||||
if (cached) return cached
|
||||
|
||||
const content = await f.get<string>(file.download_url)
|
||||
if (content) {
|
||||
await markdownStorage.setItem(file.name, content, { ttl: 60 * 60 })
|
||||
}
|
||||
files.map(async (file) => {
|
||||
const content = await $fetch<string>(file.url)
|
||||
|
||||
return content
|
||||
})
|
||||
)
|
||||
|
||||
body += contents.join('\n\n')
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 500,
|
||||
body: `Error fetching markdown files: ${error.message}`
|
||||
}
|
||||
}
|
||||
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
|
||||
appendResponseHeaders(event, {
|
||||
'content-type': 'text/markdown;charset=utf-8',
|
||||
'cache-control': 'public, max-age=3600'
|
||||
})
|
||||
return body
|
||||
})
|
||||
appendResponseHeaders(event, {
|
||||
'content-type': 'text/markdown;charset=utf-8',
|
||||
'cache-control': 'public, max-age=7200'
|
||||
})
|
||||
return body
|
||||
},
|
||||
{
|
||||
maxAge: 60 * 60,
|
||||
name: 'single-page',
|
||||
getKey: () => 'default' /* Can be extended in the future */
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
{
|
||||
"extends": "../.nitro/types/tsconfig.json"
|
||||
"extends": "../.nitro/types/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": [
|
||||
"@cloudflare/workers-types"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
6
api/worker-configuration.d.ts
vendored
Normal file
6
api/worker-configuration.d.ts
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Generated by Wrangler by running `wrangler types api/worker-configuration.d.ts`
|
||||
|
||||
interface Env {
|
||||
STORAGE: KVNamespace;
|
||||
RATE_LIMITER: RateLimit;
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
{
|
||||
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
|
||||
"extends": ["@taskylizard/biome-config", "./.cache/imports.json"],
|
||||
"extends": [
|
||||
"@taskylizard/biome-config",
|
||||
"./.cache/imports.json"
|
||||
],
|
||||
"files": {
|
||||
"ignore": [
|
||||
"docs/.vitepress/**/*.vue",
|
||||
|
|
@ -42,6 +45,9 @@
|
|||
},
|
||||
"linter": {
|
||||
"rules": {
|
||||
"correctness": {
|
||||
"noUndeclaredVariables": "off"
|
||||
},
|
||||
"style": {
|
||||
"useFilenamingConvention": "off",
|
||||
"noDefaultExport": "off"
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
> [!NOTE]
|
||||
> The website is no longer getting new features. It is now in maintenance mode. Please do not open issues or PRs.
|
||||
|
||||
This is the website source code to be used with [VitePress](https://vitepress.dev/).
|
||||
|
||||
Licensed under the Apache License v2.0, see [LICENSE](./LICENSE) for more information.
|
||||
|
|
|
|||
|
|
@ -13,8 +13,12 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
//https://nitro.unjs.io/config
|
||||
|
||||
import nitroCloudflareBindings from 'nitro-cloudflare-dev'
|
||||
import { defineNitroConfig } from 'nitropack/config'
|
||||
|
||||
export default defineNitroConfig({
|
||||
modules: [nitroCloudflareBindings],
|
||||
preset: 'cloudflare_module',
|
||||
compatibilityDate: '2024-11-01',
|
||||
runtimeConfig: {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
"api:dev": "nitropack dev",
|
||||
"api:prepare": "nitropack prepare",
|
||||
"api:preview": "node .output/server/index.mjs",
|
||||
"api:typegen": "wrangler types api/worker-configuration.d.ts",
|
||||
"docs:build": "vitepress build docs/",
|
||||
"docs:dev": "vitepress dev docs/",
|
||||
"docs:preview": "vitepress preview docs/",
|
||||
|
|
@ -41,6 +42,7 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^1.9.3",
|
||||
"@cloudflare/workers-types": "^4.20241230.0",
|
||||
"@ianvs/prettier-plugin-sort-imports": "^4.3.1",
|
||||
"@iconify-json/carbon": "^1.2.3",
|
||||
"@iconify-json/heroicons-solid": "^1.2.0",
|
||||
|
|
@ -51,6 +53,7 @@
|
|||
"@taskylizard/biome-config": "^1.0.5",
|
||||
"@types/node": "^20.16.12",
|
||||
"@types/nprogress": "^0.2.3",
|
||||
"nitro-cloudflare-dev": "^0.2.1",
|
||||
"prettier": "^3.3.3",
|
||||
"prettier-plugin-pkgsort": "^0.2.1",
|
||||
"prettier-plugin-tailwindcss": "^0.6.8",
|
||||
|
|
|
|||
BIN
pnpm-lock.yaml
BIN
pnpm-lock.yaml
Binary file not shown.
|
|
@ -2,7 +2,7 @@ name = "api"
|
|||
main = ".output/server/index.mjs"
|
||||
workers_dev = false
|
||||
account_id = "02f3b11d8d1017a20f95de4ba88fb5d6"
|
||||
compatibility_flags = [ "nodejs_compat" ]
|
||||
compatibility_flags = ["nodejs_compat"]
|
||||
compatibility_date = "2024-11-01"
|
||||
|
||||
routes = [
|
||||
|
|
@ -16,5 +16,11 @@ id = "6f18adea26a64d6b8858ffbdfd3f4cf2"
|
|||
[[unsafe.bindings]]
|
||||
name = "RATE_LIMITER"
|
||||
type = "ratelimit"
|
||||
namespace_id = "69420"
|
||||
# An identifier you define, that is unique to your Cloudflare account.
|
||||
# Must be an integer.
|
||||
namespace_id = "1001"
|
||||
|
||||
# Limit: the number of tokens allowed within a given period in a single
|
||||
# Cloudflare location
|
||||
# Period: the duration of the period, in seconds. Must be either 10 or 60
|
||||
simple = { limit = 100, period = 60 }
|
||||
|
|
|
|||
Loading…
Reference in a new issue