Spaces:
Running

File size: 4,460 Bytes
c015809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as cm from 'cache-manager';
import fastq from 'fastq';

import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { marked } from 'marked';
import { poweredBy } from 'hono/powered-by';
import { prettyJSON } from 'hono/pretty-json';
import { serveStatic } from 'hono/serve-static';

import Quote from './core/index.js';
import getClientIP from './utils/getClientIp.js';

const app = new Hono();

const defaultTtl = 60 * 60 * 1000;

const inflight = new Map();
const cache = cm.createCache({ ttl: defaultTtl });

const queue = fastq.promise(async ({ key, c, body }) => {
	try {
		const data = await Quote(body);

		const result = c.json(data);

		await cache.set(key, result);

		return result;
	} finally {
		inflight.delete(key);
	}
}, 1);

app.use('*', cors({
	origin: '*',
	allowMethods: ['GET', 'POST', 'OPTIONS'],
	allowHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
}));
app.use(poweredBy({ serverName: 'Hisoka Labs' }));
app.use(prettyJSON({ space: 3 }));

app.use(
	serveStatic({
		root: './public',
		getContent: async (file, c) => {
			const res = Bun.file(file);
			if (await res.exists()) {
				const data = await res.arrayBuffer();
				const mime = res.type;

				return c.body(data, 200, { 'Content-Type': mime });
			}
		},
	}),
);

app.use(async (c, next) => {
	var colorStatus = status => {
		switch ((status / 100) | 0) {
			case 5:
				return `\x1B[31m${status}\x1B[0m`;
			case 4:
				return `\x1B[33m${status}\x1B[0m`;
			case 3:
				return `\x1B[36m${status}\x1B[0m`;
			case 2:
				return `\x1B[32m${status}\x1B[0m`;
		}
		return `${status}`;
	};

	const start = Date.now();
	const ipClient = getClientIP(c);

	await next();

	console.log(
		`${ipClient} ${c.req.method} ${c.req.path + new URL(c.req.url).search} ${colorStatus(c.res.status)} ${Date.now() - start}ms`,
	);
});

app.get('/', async c => {
	const res = Bun.file('README.md');
	const text = await res.text();
	const html = marked.parse(text);

	return c.html(`<html>
	  <head>
			<title>Quote API Documentation</title>
			<meta name="viewport" content="width=device-width, initial-scale=1">
			<meta charset="utf-8">
			<meta name="description" content="An Quote API Telegram fast, simple, and free.">
			<meta name="keywords" content="quote api, telegram quote api, quote maker, quote generator, hisoka labs, kualat">
			<meta name="author" content="Hisoka Labs">
			<link rel="stylesheet" href="/styles.css">
			<link rel="icon" type="image/png" href="/favicon.png">
			<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/github-dark.min.css">
			<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script>
		</head>
	  <body>
			${html}
			<script>hljs.highlightAll();</script>
	  </body>
   </html>`);
});

app.post('/', async c => {
	try {
		if (c.req.header('content-type') !== 'application/json') {
			return c.json({ status: false, message: 'Content-Type must be application/json' }, 400);
		}

		const body = await c.req.json();

		// body validation
		if (!body.messages) return c.json({ status: false, message: 'messages is required' }, 400);
		if (!Array.isArray(body.messages)) return c.json({ status: false, message: 'messages must be an array' }, 400);
		if (typeof body.messages[0] !== 'object') {
			return c.json({ status: false, message: 'messages must be an array of objects' }, 400);
		}

		const key = Bun.CryptoHasher.hash("sha256", JSON.stringify(body)).toString('hex');

		const cached = await cache.get(key);
		if (cached !== undefined) {
			cached.headers.set('X-Cache', 'HIT');
			return cached instanceof Response ? cached.clone() : cached;
		}

		if (inflight.has(key)) {
			const result = await inflight.get(key);
			result.headers.set('X-Cache', 'HIT');
			return result instanceof Response ? result.clone() : result;
		}

		const promise = queue.push({ key, c, body });
		inflight.set(key, promise);

		const result = await promise;

		result.headers.set('X-Cache', 'MISS');
		return result instanceof Response ? result.clone() : result;
	} catch (e) {
		console.error(e);
		return c.json({ status: false, message: 'Invalid JSON' }, 400);
	}
});

app.get('/status', c => {
	const uptime = process.uptime();
	return c.json({
		status: true,
		message: 'Quote API is running',
		uptime: new Date(uptime * 1000).toISOString().slice(11, 19),
	});
});

export default {
	port: process.env.PORT || 7860,
	fetch: app.fetch,
};