-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
76 lines (62 loc) · 2.65 KB
/
main.ts
File metadata and controls
76 lines (62 loc) · 2.65 KB
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
console.log("🚀 Starting Buildkite Overview Application...")
import { App, staticFiles } from "fresh"
import { getBackgroundPoller } from "./server/background-poller.ts"
import { getCacheManager } from "./server/cache/cache-manager.ts"
import { parseCliArgs } from "./server/cli.ts"
import { getConfig, shouldBypassOrgCheck, shouldRequireAuth } from "./server/config.ts"
import { csrfContext, csrfProtection } from "./server/csrf.ts"
import { type AppState, localizationMiddleware, requireGlobalAuth, sessionMiddleware } from "./server/middleware.ts"
import { securityHeaders } from "./server/security-headers.ts"
import { startSessionCleanup } from "./server/session-store.ts"
import { startTokenCleanup } from "./server/token-store.ts"
import { define } from "./utils.ts"
// Parse CLI arguments
const cliOptions = parseCliArgs()
// Initialize configuration (validates environment variables/config file)
getConfig(cliOptions.config)
console.log("✅ Configuration initialized and validated")
// Log configuration status
if (shouldBypassOrgCheck()) {
console.log("⚠️ Development mode: GitHub organization check is bypassed")
}
if (shouldRequireAuth()) {
console.log("🔒 Global authentication is enabled - all routes require login")
}
// Initialize cache manager and database
console.log("🗄️ Initializing cache database...")
getCacheManager() // Initialize the singleton
// Cache cleanup is handled internally by the cache manager
console.log("✅ Cache system initialized")
// Start security-related cleanup services
console.log("🛡️ Starting security services...")
startSessionCleanup()
startTokenCleanup()
console.log("✅ Security services started")
// Initialize and start background polling service
const backgroundPoller = getBackgroundPoller()
backgroundPoller.start()
export const app = new App<AppState>()
// Apply middleware in order - security headers first
app.use(staticFiles())
app.use(securityHeaders)
app.use(sessionMiddleware)
app.use(csrfContext) // Add CSRF token to context
app.use(csrfProtection) // Protect state-changing requests
app.use(localizationMiddleware)
// Apply global auth middleware only if REQUIRE_AUTH is enabled
if (shouldRequireAuth()) {
app.use(requireGlobalAuth)
}
// this can also be defined via a file. feel free to delete this!
const loggerMiddleware = define.middleware(async (ctx) => {
if (ctx.req.url.endsWith("/health")) {
return ctx.next()
}
const response = await ctx.next()
const status = response instanceof Response ? response.status : "unknown"
console.log(`${ctx.req.method} ${status} ${ctx.req.url}`)
return response
})
app.use(loggerMiddleware)
// Include file-system based routes here
app.fsRoutes()