| 17 | const PORT = 3060; |
| 18 | |
| 19 | export class Api { |
| 20 | private server: http.Server; |
| 21 | |
| 22 | constructor( |
| 23 | promClient: PromClient, |
| 24 | private prisma: PrismaClient, |
| 25 | private connectionManager: ConnectionManager, |
| 26 | private repoIndexManager: RepoIndexManager, |
| 27 | private accountPermissionSyncer: AccountPermissionSyncer, |
| 28 | ) { |
| 29 | const app = express(); |
| 30 | app.use(express.json()); |
| 31 | app.use(express.urlencoded({ extended: true })); |
| 32 | |
| 33 | // Prometheus metrics endpoint |
| 34 | app.use('/metrics', async (_req: Request, res: Response) => { |
| 35 | res.set('Content-Type', promClient.registry.contentType); |
| 36 | const metrics = await promClient.registry.metrics(); |
| 37 | res.end(metrics); |
| 38 | }); |
| 39 | |
| 40 | app.post('/api/sync-connection', this.syncConnection.bind(this)); |
| 41 | app.post('/api/index-repo', this.indexRepo.bind(this)); |
| 42 | app.post('/api/trigger-account-permission-sync', this.triggerAccountPermissionSync.bind(this)); |
| 43 | app.post(`/api/experimental/add-github-repo`, this.experimental_addGithubRepo.bind(this)); |
| 44 | |
| 45 | this.server = app.listen(PORT, () => { |
| 46 | logger.debug(`API server is running on port ${PORT}`); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | private async syncConnection(req: Request, res: Response) { |
| 51 | const schema = z.object({ |
| 52 | connectionId: z.number(), |
| 53 | }).strict(); |
| 54 | |
| 55 | const parsed = schema.safeParse(req.body); |
| 56 | if (!parsed.success) { |
| 57 | res.status(400).json({ error: parsed.error.message }); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | const { connectionId } = parsed.data; |
| 62 | const connection = await this.prisma.connection.findUnique({ |
| 63 | where: { |
| 64 | id: connectionId, |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | if (!connection) { |
| 69 | res.status(404).json({ error: 'Connection not found' }); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | const [jobId] = await this.connectionManager.createJobs([connection]); |
| 74 | |
| 75 | res.status(200).json({ jobId }); |
| 76 | } |
nothing calls this directly
no outgoing calls
no test coverage detected