mirror of
https://github.com/pheralb/svgl.git
synced 2024-09-20 19:26:18 +08:00
⚙️ Protect /api/svgs & /api/categories.
This commit is contained in:
parent
0e7ff01315
commit
d4c9b3a316
@ -1,12 +1,29 @@
|
|||||||
|
import type { RequestEvent } from './$types';
|
||||||
|
|
||||||
import { json } from '@sveltejs/kit';
|
import { json } from '@sveltejs/kit';
|
||||||
|
import { ratelimit } from '@/server/redis';
|
||||||
|
|
||||||
// Data:
|
// Data:
|
||||||
import { svgs } from '@/data/svgs';
|
import { svgs } from '@/data/svgs';
|
||||||
|
|
||||||
export const GET = () => {
|
export const GET = async ({ request }: RequestEvent) => {
|
||||||
const categories = svgs
|
const categories = svgs
|
||||||
.map((svg) => svg.category)
|
.map((svg) => svg.category)
|
||||||
.filter((category, index, array) => array.indexOf(category) === index);
|
.filter((category, index, array) => array.indexOf(category) === index);
|
||||||
|
const ip = request.headers.get('x-forwarded-for') ?? '';
|
||||||
|
const { success, reset } = await ratelimit.limit(ip);
|
||||||
|
|
||||||
|
// Error 429 | If rate limit is exceeded:
|
||||||
|
if (!success) {
|
||||||
|
const now = Date.now();
|
||||||
|
const retryAfter = Math.floor((reset - now) / 1000);
|
||||||
|
return new Response('Too Many Requests', {
|
||||||
|
status: 429,
|
||||||
|
headers: {
|
||||||
|
'Retry-After': retryAfter.toString()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Status 200 | If limit is a number:
|
// Status 200 | If limit is a number:
|
||||||
return json(
|
return json(
|
||||||
|
@ -2,12 +2,27 @@ import type { RequestEvent } from './$types';
|
|||||||
import type { iSVG } from '@/types/svg';
|
import type { iSVG } from '@/types/svg';
|
||||||
|
|
||||||
import { error, json } from '@sveltejs/kit';
|
import { error, json } from '@sveltejs/kit';
|
||||||
|
import { ratelimit } from '@/server/redis';
|
||||||
|
|
||||||
// Data:
|
// Data:
|
||||||
import { svgsData } from '@/data';
|
import { svgsData } from '@/data';
|
||||||
|
|
||||||
export const GET = ({ url }: RequestEvent) => {
|
export const GET = async ({ url, request }: RequestEvent) => {
|
||||||
const fullUrl = url.origin ?? 'svgl.vercel.app';
|
const fullUrl = url.origin ?? 'svgl.vercel.app';
|
||||||
|
const ip = request.headers.get('x-forwarded-for') ?? '';
|
||||||
|
const { success, reset } = await ratelimit.limit(ip);
|
||||||
|
|
||||||
|
// Error 429 | If rate limit is exceeded:
|
||||||
|
if (!success) {
|
||||||
|
const now = Date.now();
|
||||||
|
const retryAfter = Math.floor((reset - now) / 1000);
|
||||||
|
return new Response('Too Many Requests', {
|
||||||
|
status: 429,
|
||||||
|
headers: {
|
||||||
|
'Retry-After': retryAfter.toString()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Params:
|
// Params:
|
||||||
const getLimitParams = url.searchParams.get('limit');
|
const getLimitParams = url.searchParams.get('limit');
|
||||||
|
Loading…
Reference in New Issue
Block a user