-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
67 lines (53 loc) · 1.19 KB
/
main.ts
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
import { MongoClient } from "npm:mongodb"
const MONGO_URI = Deno.env.get("MONGO_URI") as string
const client = new MongoClient(MONGO_URI)
const db = client.db("bsky")
const collection = db.collection("hashtags")
const hashtags = async ({ limit }: { limit: number } = { limit: 25 }) => {
const pipeline = [
{
$group: {
_id: "$hashtag",
count: { $sum: 1 },
},
},
{
$project: {
_id: 0,
hashtag: "$_id",
count: 1,
},
},
{
$sort: { count: -1 },
},
{
$limit: limit,
},
]
return await collection
.aggregate(pipeline)
.toArray()
}
const handler = async (req: Request): Promise<Response> => {
const url = new URL(req.url)
const limit = Number(url.searchParams.get("limit") || 25)
const headers = new Headers({
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers":
"Origin, X-Requested-With, Content-Type, Accept",
})
const data = await hashtags({ limit })
if (!data) {
return new Response(null, {
status: 400,
})
}
return new Response(JSON.stringify(data), {
status: 200,
headers,
})
}
Deno.serve({ port: 80 }, handler)