Download api-gateway/src/app.controller.ts from mishrabp/meridian: direct link, hf CLI and curl.
- Browser
- Download file 16.9 kB
-
https://huggingface.co/spaces/mishrabp/meridian/resolve/main/api-gateway/src/app.controller.ts
- Command line
-
hf download hf://spaces/mishrabp/meridian/api-gateway/src/app.controller.ts
-
curl -L -o app.controller.ts https://huggingface.co/spaces/mishrabp/meridian/resolve/main/api-gateway/src/app.controller.ts
16.9 kB
| import { | |
| Controller, All, Req, Res, Next, Get, Post, Put, Patch, Delete, Param, Body, Query, | |
| UseGuards, Request, UseInterceptors, UploadedFile, Headers, HttpCode | |
| } from '@nestjs/common'; | |
| import { AuthGuard } from '@nestjs/passport'; | |
| import { FileInterceptor } from '@nestjs/platform-express'; | |
| import { memoryStorage } from 'multer'; | |
| import { ProxyService } from './proxy.service'; | |
| import { Request as ExpressRequest, Response } from 'express'; | |
| ('api') | |
| export class AppController { | |
| constructor(private proxy: ProxyService) {} | |
| private getAuthHeader(req: any) { | |
| return req.headers?.authorization || ''; | |
| } | |
| // AUTH ROUTES | |
| ('auth/login') | |
| login(() body: any) { | |
| return this.proxy.forward('auth', '/auth/login', 'POST', body); | |
| } | |
| ('auth/verify') | |
| (AuthGuard('jwt')) | |
| verify(() req: any) { | |
| return req.user; | |
| } | |
| // USER ROUTES | |
| ('users') | |
| (AuthGuard('jwt')) | |
| getUsers(() req: any) { | |
| return this.proxy.forward('auth', '/users', 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('users') | |
| (AuthGuard('jwt')) | |
| createUser(() body: any, () req: any) { | |
| return this.proxy.forward('auth', '/users', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('users/:id') | |
| (AuthGuard('jwt')) | |
| updateUser(('id') id: string, () body: any, () req: any) { | |
| return this.proxy.forward('auth', `/users/${id}`, 'PUT', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('users/:id') | |
| (AuthGuard('jwt')) | |
| deleteUser(('id') id: string, () req: any) { | |
| return this.proxy.forward('auth', `/users/${id}`, 'DELETE', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // BLOG ROUTES - Public | |
| ('posts') | |
| getPosts(() query: any) { | |
| const params = new URLSearchParams(query).toString(); | |
| return this.proxy.forward('blog', `/posts?${params}`, 'GET'); | |
| } | |
| ('posts/featured') | |
| getFeatured() { return this.proxy.forward('blog', '/posts/featured', 'GET'); } | |
| ('posts/recent') | |
| getRecent() { return this.proxy.forward('blog', '/posts/recent', 'GET'); } | |
| ('posts/stats') | |
| (AuthGuard('jwt')) | |
| getStats(() req: any) { | |
| return this.proxy.forward('blog', '/posts/stats', 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('posts/admin') | |
| (AuthGuard('jwt')) | |
| getAdminPosts(() query: any, () req: any) { | |
| const params = new URLSearchParams({ ...query, status: query.status || '' }).toString(); | |
| return this.proxy.forward('blog', `/posts/admin?${params}`, 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('posts/:slug') | |
| getPost(('slug') slug: string) { | |
| return this.proxy.forward('blog', `/posts/${slug}`, 'GET'); | |
| } | |
| ('posts') | |
| (AuthGuard('jwt')) | |
| createPost(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/posts', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('posts/:id/approve') | |
| (AuthGuard('jwt')) | |
| approvePost(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/posts/${id}/approve`, 'PATCH', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('posts/:id/reject') | |
| (AuthGuard('jwt')) | |
| rejectPost(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/posts/${id}/reject`, 'PATCH', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('posts/:id') | |
| (AuthGuard('jwt')) | |
| updatePost(('id') id: string, () body: any, () req: any) { | |
| return this.proxy.forward('blog', `/posts/${id}`, 'PUT', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('posts/:id') | |
| (AuthGuard('jwt')) | |
| deletePost(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/posts/${id}`, 'DELETE', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // CATEGORIES | |
| ('categories') | |
| getCategories() { return this.proxy.forward('blog', '/categories', 'GET'); } | |
| ('categories') | |
| (AuthGuard('jwt')) | |
| createCategory(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/categories', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('categories/:id') | |
| (AuthGuard('jwt')) | |
| updateCategory(('id') id: string, () body: any, () req: any) { | |
| return this.proxy.forward('blog', `/categories/${id}`, 'PUT', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('categories/:id') | |
| (AuthGuard('jwt')) | |
| deleteCategory(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/categories/${id}`, 'DELETE', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // TAGS | |
| ('tags') | |
| getTags() { return this.proxy.forward('blog', '/tags', 'GET'); } | |
| ('tags') | |
| (AuthGuard('jwt')) | |
| createTag(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/tags', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('tags/many') | |
| (AuthGuard('jwt')) | |
| createManyTags(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/tags/many', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // COMMENTS | |
| ('comments') | |
| (AuthGuard('jwt')) | |
| getComments(() req: any) { | |
| return this.proxy.forward('blog', '/comments', 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('comments/post/:postId') | |
| getPostComments(('postId') postId: string) { | |
| return this.proxy.forward('blog', `/comments/post/${postId}`, 'GET'); | |
| } | |
| ('comments/post/:postId') | |
| createComment(('postId') postId: string, () body: any) { | |
| return this.proxy.forward('blog', `/comments/post/${postId}`, 'POST', body); | |
| } | |
| ('comments/:id/approve') | |
| (AuthGuard('jwt')) | |
| approveComment(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/comments/${id}/approve`, 'PUT', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('comments/:id') | |
| (AuthGuard('jwt')) | |
| deleteComment(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/comments/${id}`, 'DELETE', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // MEDIA | |
| ('media') | |
| (AuthGuard('jwt')) | |
| getMedia(() req: any) { | |
| return this.proxy.forward('media', '/media', 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('media/upload') | |
| (AuthGuard('jwt')) | |
| (FileInterceptor('file', { storage: memoryStorage() })) | |
| uploadMedia( | |
| () file: Express.Multer.File, | |
| () body: any, | |
| ('filename') filename: string, | |
| () req: any, | |
| ) { | |
| return this.proxy.forwardWithFile( | |
| '/media/upload', | |
| file, | |
| body, | |
| this.getAuthHeader(req), | |
| filename ? { filename } : undefined, | |
| ); | |
| } | |
| ('media/:id') | |
| (AuthGuard('jwt')) | |
| deleteMedia(('id') id: string, () req: any) { | |
| return this.proxy.forward('media', `/media/${id}`, 'DELETE', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // STORY ROUTES - Public | |
| ('stories') | |
| getStories(() query: any) { | |
| const params = new URLSearchParams(query).toString(); | |
| return this.proxy.forward('blog', `/stories?${params}`, 'GET'); | |
| } | |
| ('stories/recent') | |
| getRecentStories() { return this.proxy.forward('blog', '/stories/recent', 'GET'); } | |
| ('stories/stats') | |
| (AuthGuard('jwt')) | |
| getStoryStats(() req: any) { | |
| return this.proxy.forward('blog', '/stories/stats', 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('stories/admin') | |
| (AuthGuard('jwt')) | |
| getAdminStories(() query: any, () req: any) { | |
| const params = new URLSearchParams({ ...query, status: query.status || '' }).toString(); | |
| return this.proxy.forward('blog', `/stories/admin?${params}`, 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('stories/:slug') | |
| getStory(('slug') slug: string) { | |
| return this.proxy.forward('blog', `/stories/${slug}`, 'GET'); | |
| } | |
| ('stories') | |
| (AuthGuard('jwt')) | |
| createStory(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/stories', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('stories/:id/approve') | |
| (AuthGuard('jwt')) | |
| approveStory(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/stories/${id}/approve`, 'PATCH', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('stories/:id/reject') | |
| (AuthGuard('jwt')) | |
| rejectStory(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/stories/${id}/reject`, 'PATCH', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('stories/:id') | |
| (AuthGuard('jwt')) | |
| updateStory(('id') id: string, () body: any, () req: any) { | |
| return this.proxy.forward('blog', `/stories/${id}`, 'PUT', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('stories/:id') | |
| (AuthGuard('jwt')) | |
| deleteStory(('id') id: string, () req: any) { | |
| return this.proxy.forward('blog', `/stories/${id}`, 'DELETE', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // NEWS ROUTES | |
| ('news') | |
| getNews(() query: any) { | |
| const params = new URLSearchParams(query).toString(); | |
| return this.proxy.forward('blog', `/news?${params}`, 'GET'); | |
| } | |
| ('news/refresh') | |
| (AuthGuard('jwt')) | |
| refreshNews(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/news/refresh', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('news/:id') | |
| (AuthGuard('jwt')) | |
| updateNewsItem(('id') id: string, () body: any, () req: any) { | |
| return this.proxy.forward('blog', `/news/${id}`, 'PATCH', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // PUSH NOTIFICATION ROUTES | |
| ('push/vapid-key') | |
| getPushVapidKey() { | |
| return this.proxy.forward('blog', '/push/vapid-key', 'GET'); | |
| } | |
| ('push/subscribe') | |
| subscribePush(() body: any) { | |
| return this.proxy.forward('blog', '/push/subscribe', 'POST', body); | |
| } | |
| ('push/unsubscribe') | |
| unsubscribePush(() body: any) { | |
| return this.proxy.forward('blog', '/push/unsubscribe', 'DELETE', body); | |
| } | |
| // AGENT RUNS | |
| ('agent-runs/dispatch') | |
| (AuthGuard('jwt')) | |
| dispatchAgent(() body: any, () req: any) { | |
| return this.proxy.forward('blog', '/agent-runs/dispatch', 'POST', body, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('agent-runs') | |
| createAgentRun(() body: any) { | |
| return this.proxy.forward('blog', '/agent-runs', 'POST', body); | |
| } | |
| ('agent-runs/:runId') | |
| updateAgentRun(('runId') runId: string, () body: any) { | |
| return this.proxy.forward('blog', `/agent-runs/${runId}`, 'PUT', body); | |
| } | |
| ('agent-runs') | |
| (AuthGuard('jwt')) | |
| getAgentRuns(() req: any) { | |
| return this.proxy.forward('blog', '/agent-runs', 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| ('agent-runs/:runId') | |
| (AuthGuard('jwt')) | |
| getAgentRun(('runId') runId: string, () req: any) { | |
| return this.proxy.forward('blog', `/agent-runs/${runId}`, 'GET', null, { Authorization: this.getAuthHeader(req) }); | |
| } | |
| // TEXT-TO-SPEECH β one chunk per request (live playback); style selects the voice/pace. | |
| // format: 'wav' (default, live per-chunk playback) or 'mp3' (whole-post pre-rendered | |
| // narration for the media library β mp3 encoding only exists on the local Kokoro path, | |
| // so an mp3 request skips the HF fallbacks rather than silently returning the wrong format. | |
| // News: Primary: maya-research/maya1 (HF) β Secondary: Kokoro-82M β Tertiary: facebook/mms-tts-eng (HF) | |
| // Other: Primary: Kokoro-82M local β Fallback: facebook/mms-tts-eng (HF) | |
| ('tts') | |
| async textToSpeech( | |
| () body: { text: string; style?: string; format?: string }, | |
| () res: Response, | |
| ) { | |
| const text = (body.text || '').trim(); | |
| const style = (body.style || '').trim(); | |
| const format = (body.format || 'wav').trim().toLowerCase() === 'mp3' ? 'mp3' : 'wav'; | |
| if (!text) { res.status(400).json({ message: 'text is required' }); return; } | |
| const localTts = process.env.TTS_SERVICE_URL; | |
| const hfToken = process.env.HF_TOKEN; | |
| if (!localTts && !hfToken) { | |
| res.status(503).json({ message: 'TTS not configured: set TTS_SERVICE_URL or HF_TOKEN' }); | |
| return; | |
| } | |
| const errors: string[] = []; | |
| // Retries once on HF 503 "model loading" response. | |
| const hfInfer = async (model: string): Promise<{ buf: Buffer; ct: string }> => { | |
| const doFetch = async () => { | |
| const r = await fetch( | |
| `https://api-inference.huggingface.co/models/${model}`, | |
| { | |
| method: 'POST', | |
| headers: { Authorization: `Bearer ${hfToken}`, 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ inputs: text }), | |
| signal: AbortSignal.timeout(90_000), | |
| }, | |
| ); | |
| if (r.status === 503) { | |
| const b = await r.json().catch(() => ({})) as { estimated_time?: number }; | |
| const wait = Math.min((b.estimated_time ?? 20) * 1000, 30_000); | |
| await new Promise(resolve => setTimeout(resolve, wait)); | |
| const r2 = await fetch( | |
| `https://api-inference.huggingface.co/models/${model}`, | |
| { | |
| method: 'POST', | |
| headers: { Authorization: `Bearer ${hfToken}`, 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ inputs: text }), | |
| signal: AbortSignal.timeout(90_000), | |
| }, | |
| ); | |
| if (!r2.ok) throw new Error(`HF ${model}: ${r2.status}`); | |
| return r2; | |
| } | |
| if (!r.ok) throw new Error(`HF ${model}: ${r.status}`); | |
| return r; | |
| }; | |
| const r = await doFetch(); | |
| return { buf: Buffer.from(await r.arrayBuffer()), ct: r.headers.get('content-type') || 'audio/flac' }; | |
| }; | |
| // ββ News primary: maya-research/maya1 (HF) ββββββββββββββββββββββββββββββ | |
| // (mp3 output only exists on the local Kokoro path β skip HF branches entirely.) | |
| if (format === 'mp3') { | |
| if (!localTts) { | |
| res.status(503).json({ message: 'mp3 TTS requires TTS_SERVICE_URL (local Kokoro)' }); | |
| return; | |
| } | |
| } else if (style === 'news' && hfToken) { | |
| try { | |
| const { buf, ct } = await hfInfer('maya-research/maya1'); | |
| res.set({ 'Content-Type': ct, 'Content-Length': String(buf.length) }); | |
| return res.send(buf); | |
| } catch (e) { | |
| errors.push((e as Error).message); | |
| } | |
| } | |
| // ββ Primary (all styles): Kokoro-82M local service βββββββββββββββββββββββ | |
| // mp3 requests (whole-post pre-rendered narration) take much longer than a short live | |
| // chunk, so they get a longer timeout matching tts-service's own gunicorn timeout. | |
| if (localTts) { | |
| try { | |
| const r = await fetch(`${localTts}/tts`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ text, style, format }), | |
| signal: AbortSignal.timeout(format === 'mp3' ? 900_000 : 120_000), | |
| }); | |
| if (!r.ok) throw new Error(`Kokoro: ${r.status}`); | |
| const buf = Buffer.from(await r.arrayBuffer()); | |
| res.set({ | |
| 'Content-Type': format === 'mp3' ? 'audio/mpeg' : 'audio/wav', | |
| 'Content-Length': String(buf.length), | |
| }); | |
| return res.send(buf); | |
| } catch (e) { | |
| errors.push((e as Error).message); | |
| if (format === 'mp3') { | |
| res.status(503).json({ message: `mp3 TTS unavailable: ${errors.join(' | ')}` }); | |
| return; | |
| } | |
| } | |
| } | |
| // ββ Tertiary / final fallback: HF facebook/mms-tts-eng ββββββββββββββββββ | |
| if (hfToken) { | |
| try { | |
| const { buf, ct } = await hfInfer('facebook/mms-tts-eng'); | |
| res.set({ 'Content-Type': ct, 'Content-Length': String(buf.length) }); | |
| return res.send(buf); | |
| } catch (e) { | |
| errors.push((e as Error).message); | |
| } | |
| } | |
| res.status(503).json({ message: `TTS unavailable: ${errors.join(' | ')}` }); | |
| } | |
| } | |