uiharu/uiharu.ts

69 lines
2.3 KiB
TypeScript

import { handleMetadataLookup, handleMetadataBatchLookup } from './src/handlers/lookup.ts';
import { handleThumbnailRetrieve } from './src/handlers/thumb.ts';
import { handlePublicPath } from './src/handlers/local.ts';
import { MemcacheClient } from 'npm:memcache-client@^1.0.5';
// todo: these should not be hardcoded lol
const hostName: String = 'uiharu.edgii.net';
const port: Number = 3009;
const memcacheServer: String = '127.0.0.1:11211';
const allowedOrigins: String[] = [
'edgii.net',
'chat.edgii.net',
'sockchat.edgii.net',
'ajaxchat.edgii.net',
];
const cache: MemcacheClient = new MemcacheClient({
server: memcacheServer,
compressor: {
// fuck it lol
compressSync: buffer => buffer,
decompressSync: buffer => buffer,
},
});
Deno.serve({ port }, async (req: Request): Response => {
const url = new URL(req.url);
const headers = { 'X-Powered-By': 'Uiharu' };
if(req.headers.has('origin')) {
const originRaw = req.headers.get('origin');
const origin = new URL(originRaw);
if(!allowedOrigins.includes(origin.host))
return new Response('403', { status: 403, headers });
headers['Access-Control-Allow-Origin'] = originRaw;
headers['Vary'] = 'Origin';
}
if(req.method === 'OPTIONS') {
headers['Allow'] = 'OPTIONS, GET, HEAD, POST';
headers['Access-Control-Allow-Methods'] = 'OPTIONS, GET, HEAD, POST';
// idk if this is the appropriate status code but: balls
return new Response('', { status: 204, headers });
}
if(url.pathname === '/metadata')
return handleMetadataLookup(url, headers, req, cache, hostName);
if(url.pathname === '/metadata/batch')
return handleMetadataBatchLookup(headers, req);
const isAudio = url.pathname === '/metadata/thumb/audio';
const isVideo = url.pathname === '/metadata/thumb/video';
if(isAudio || isVideo)
return handleThumbnailRetrieve(url, headers, req, isAudio, isVideo);
// serving files from /public dir
if(['HEAD', 'GET'].includes(req.method))
return handlePublicPath(headers, url.pathname);
// 404 fallback
return new Response('', {
status: ['OPTIONS', 'HEAD', 'GET', 'POST'].includes(req.method) ? 404 : 405,
headers,
});
});