index.ts

1import express, { Request, Response } from 'express'
2import cors from 'cors'
3import { supabase } from '@/configs/supabase'
4
5const app = express()
6const port = process.env.HTTP_PORT || 3000
7
8app.use(cors())
9app.use(express.json())
10
11// Route to suggest blacklist tags
12app.post(
13	'/booru/suggest-blacklist',
14	async (req: Request, res: Response): Promise<any> => {
15		try {
16			const { tags, reason, suggestedBy } = req.body
17
18			if (!tags || !Array.isArray(tags) || tags.length === 0) {
19				return res.status(400).json({
20					error: 'Invalid tags. Must provide an array of tags.',
21				})
22			}
23
24			const { error } = await supabase.from('blacklist_suggestions').insert(
25				tags.map((tag) => ({
26					tag,
27					reason: reason || null,
28					suggested_by: suggestedBy || null,
29					status: 'pending',
30				})),
31			)
32
33			if (error) {
34				console.error('Error storing blacklist suggestion:', error)
35				return res.status(500).json({
36					error: 'Failed to store suggestion',
37				})
38			}
39
40			return res.status(201).json({
41				message: 'Suggestion received',
42				tags,
43			})
44		} catch (error) {
45			console.error('Suggestion endpoint error:', error)
46			return res.status(500).json({
47				error: 'Internal server error',
48			})
49		}
50	},
51)
52
53app.get(
54	'/booru/suggestions',
55	async (req: Request, res: Response): Promise<any> => {
56		try {
57			const { status } = req.query
58
59			const { data, error } = await supabase
60				.from('blacklist_suggestions')
61				.select('*')
62				.eq('status', status || 'pending')
63
64			if (error) {
65				return res.status(500).json({
66					error: 'Internal server error',
67				})
68			}
69
70			return res.status(200).json(data)
71		} catch (error) {
72			console.error('Suggestions endpoint error:', error)
73			return res.status(500).json({
74				error: 'Internal server error',
75			})
76		}
77	},
78)
79
80// Health check endpoint
81app.get('/health', (_, res: Response): any => {
82	res.status(200).json({ status: 'ok' })
83})
84
85export function startServer() {
86	app.listen(port, () => {
87		console.log(`HTTP server listening on port ${port}`)
88	})
89}
90