Compare commits
5 Commits
probablyco
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| af4ecf3b69 | |||
| 5e29aa5212 | |||
| 7814a6cde8 | |||
| db16756354 | |||
| 2ed70c6a0a |
79
code.ts
Normal file
79
code.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import { serve } from "bun"
|
||||||
|
import { handleGiteaWebhook } from "./gitea/webhook-handler"
|
||||||
|
import "./discord/index" // Make suer the discord client is initialized
|
||||||
|
import { getConfig } from "./config"
|
||||||
|
|
||||||
|
interface ErrorLog {
|
||||||
|
timestamp: string
|
||||||
|
message: string
|
||||||
|
stack?: string
|
||||||
|
payload: any
|
||||||
|
}
|
||||||
|
|
||||||
|
const errors: ErrorLog[] = []
|
||||||
|
|
||||||
|
const server = serve({
|
||||||
|
port: parseInt(process.env.PORT || "3000"),
|
||||||
|
routes: {
|
||||||
|
"/": {
|
||||||
|
GET: () => new Response("🌵"),
|
||||||
|
POST: () => new Response("Use /gitea/webhook for POST requests", { status: 400 }),
|
||||||
|
},
|
||||||
|
"/wtf/delete-the-db": {
|
||||||
|
GET: () => {
|
||||||
|
const dbPath = getConfig("dbPath")
|
||||||
|
Bun.file(dbPath).delete()
|
||||||
|
|
||||||
|
return new Response(`DB is no more`)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"/gitea/webhook": {
|
||||||
|
POST: async (req) => {
|
||||||
|
const payload = await req.json()
|
||||||
|
const eventType = req.headers.get("X-Gitea-Event") || "unknown"
|
||||||
|
console.log(`🌵 Received Gitea webhook ${eventType}`)
|
||||||
|
|
||||||
|
try {
|
||||||
|
await handleGiteaWebhook(payload, eventType as any)
|
||||||
|
return new Response("OK", { status: 200 })
|
||||||
|
} catch (error) {
|
||||||
|
const errorLog: ErrorLog = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
message: error instanceof Error ? error.message : String(error),
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
payload,
|
||||||
|
}
|
||||||
|
errors.push(errorLog)
|
||||||
|
console.error("💥 Webhook error 💥")
|
||||||
|
console.error(error)
|
||||||
|
return new Response("Error", { status: 500 })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"/errors": {
|
||||||
|
GET: () => {
|
||||||
|
return new Response(JSON.stringify(errors, null, 2), {
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"/discord/auth": async () => {
|
||||||
|
const permissions = 536870912 // from https://discord.com/developers/applications
|
||||||
|
const authorizeUrl = `https://discord.com/oauth2/authorize?client_id=${process.env.DISCORD_CLIENT_ID}&scope=bot&permissions=${permissions}`
|
||||||
|
|
||||||
|
const html = (
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Authenticate spike</h1>
|
||||||
|
<a href={authorizeUrl}>Authorize</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)
|
||||||
|
return new Response(html.toString(), { headers: { "Content-Type": "text/html" } })
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
development: process.env.NODE_ENV !== "production" && { hmr: true, console: true },
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(`Spike running at ${server.url}:${server.port}`)
|
||||||
Loading…
Reference in New Issue
Block a user