feat: init

This commit is contained in:
ccbikai
2024-05-25 08:09:30 +08:00
commit bd47e755d5
298 changed files with 21915 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
import type { z } from 'zod'
import { parsePath } from 'ufo'
import type { LinkSchema } from '@/schemas/link'
export default eventHandler(async (event) => {
const { pathname: slug } = parsePath(event.path.slice(1)) // remove leading slash
const { slugRegex, reserveSlug } = useAppConfig(event)
const { homeURL } = useRuntimeConfig(event)
const { cloudflare } = event.context
if (event.path === '/' && homeURL)
return sendRedirect(event, homeURL)
if (slug && !reserveSlug.includes(slug) && slugRegex.test(slug) && cloudflare) {
const { KV } = cloudflare.env
const link: z.infer<typeof LinkSchema> | null = await KV.get(`link:${slug}`, { type: 'json' })
if (link) {
event.context.link = link
try {
await useAccessLog(event)
}
catch (error) {
console.error('Failed write access log:', error)
}
return sendRedirect(event, link.url, +useRuntimeConfig(event).redirectStatusCode)
}
}
})
+15
View File
@@ -0,0 +1,15 @@
export default eventHandler((event) => {
const token = getHeader(event, 'Authorization')?.replace('Bearer ', '')
if (event.path.startsWith('/api/') && !event.path.startsWith('/api/_') && token !== useRuntimeConfig(event).siteToken) {
throw createError({
status: 401,
statusText: 'Unauthorized',
})
}
if (token && token.length < 8) {
throw createError({
status: 401,
statusText: 'Token is too short',
})
}
})