-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from ajcwebdev/next
`setup.sh` script and Fastify server integration
- Loading branch information
Showing
40 changed files
with
774 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,49 @@ | ||
// server/index.js | ||
|
||
import http from 'node:http' | ||
import Fastify from 'fastify' | ||
import cors from '@fastify/cors' | ||
import { handleVideoRequest } from './routes/video.js' | ||
import { handlePlaylistRequest } from './routes/playlist.js' | ||
import { handleURLsRequest } from './routes/urls.js' | ||
import { handleFileRequest } from './routes/file.js' | ||
import { handleRSSRequest } from './routes/rss.js' | ||
import { env } from 'node:process' | ||
|
||
// Set the port from environment variable or default to 3000 | ||
const port = env.PORT || 3000 | ||
|
||
const server = http.createServer(async (req, res) => { | ||
console.log(`[${new Date().toISOString()}] Received ${req.method} request for ${req.url}`) | ||
res.setHeader('Access-Control-Allow-Origin', '*') | ||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS') | ||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type') | ||
console.log('CORS headers set') | ||
async function start() { | ||
// Create a Fastify instance with logging enabled | ||
const fastify = Fastify({ logger: true }) | ||
|
||
if (req.method === 'OPTIONS') { | ||
console.log('Handling OPTIONS preflight request') | ||
res.writeHead(204) | ||
res.end() | ||
return | ||
} | ||
// Register CORS plugin to handle CORS headers and preflight requests | ||
await fastify.register(cors, { | ||
origin: '*', | ||
methods: ['GET', 'POST', 'OPTIONS'], | ||
allowedHeaders: ['Content-Type'], | ||
}) | ||
|
||
// Log each incoming request | ||
fastify.addHook('onRequest', async (request, reply) => { | ||
console.log( | ||
`[${new Date().toISOString()}] Received ${request.method} request for ${request.url}` | ||
) | ||
}) | ||
|
||
// Define route handlers | ||
fastify.post('/video', handleVideoRequest) | ||
fastify.post('/playlist', handlePlaylistRequest) | ||
fastify.post('/urls', handleURLsRequest) | ||
fastify.post('/file', handleFileRequest) | ||
fastify.post('/rss', handleRSSRequest) | ||
|
||
if (req.method === 'POST') { | ||
switch (req.url) { | ||
case '/video': | ||
console.log('Routing to handleVideoRequest') | ||
await handleVideoRequest(req, res) | ||
break | ||
case '/playlist': | ||
console.log('Routing to handlePlaylistRequest') | ||
await handlePlaylistRequest(req, res) | ||
break | ||
case '/urls': | ||
console.log('Routing to handleURLsRequest') | ||
await handleURLsRequest(req, res) | ||
break | ||
case '/file': | ||
console.log('Routing to handleFileRequest') | ||
await handleFileRequest(req, res) | ||
break | ||
case '/rss': | ||
console.log('Routing to handleRSSRequest') | ||
await handleRSSRequest(req, res) | ||
break | ||
default: | ||
console.log('Unknown route, sending 404') | ||
res.statusCode = 404 | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify({ error: 'Not Found' })) | ||
} | ||
} else { | ||
console.log(`Method ${req.method} not allowed, sending 405`) | ||
res.statusCode = 405 | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify({ error: 'Method Not Allowed' })) | ||
try { | ||
await fastify.listen({ port }) | ||
console.log(`Server running at http://localhost:${port}`) | ||
} catch (err) { | ||
fastify.log.error(err) | ||
process.exit(1) | ||
} | ||
}) | ||
} | ||
|
||
server.listen(port, () => { | ||
console.log(`Server running at http://localhost:${port}`) | ||
}) | ||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,38 @@ | ||
// server/routes/file.js | ||
|
||
import { processFile } from '../../src/commands/processFile.js' | ||
import { reqToOpts } from '../utils/reqToOpts.js' | ||
|
||
const handleFileRequest = async (req, res) => { | ||
// Handler for /file route | ||
const handleFileRequest = async (request, reply) => { | ||
console.log('Entered handleFileRequest') | ||
let body = '' | ||
req.on('data', chunk => { | ||
body += chunk.toString() | ||
console.log('Received chunk:', chunk.toString()) | ||
}) | ||
req.on('end', async () => { | ||
console.log('Request body complete:', body) | ||
try { | ||
const { filePath, model = 'base', llm, options = {} } = JSON.parse(body) | ||
console.log('Parsed request body:', { filePath, model, llm, options }) | ||
if (!filePath) { | ||
console.log('File path not provided, sending 400') | ||
res.statusCode = 400 | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify({ error: 'File path is required' })) | ||
return | ||
} | ||
const llmOpt = llm || null | ||
await processFile(filePath, llmOpt, model, options) | ||
console.log('processFile completed successfully') | ||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify({ | ||
message: 'File processed successfully.' | ||
})) | ||
} catch (error) { | ||
console.error('Error processing file:', error) | ||
res.statusCode = 500 | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify({ error: 'An error occurred while processing the file' })) | ||
|
||
try { | ||
// Access parsed request body | ||
const requestData = request.body | ||
console.log('Parsed request body:', requestData) | ||
|
||
// Extract file path | ||
const { filePath } = requestData | ||
|
||
if (!filePath) { | ||
console.log('File path not provided, sending 400') | ||
reply.status(400).send({ error: 'File path is required' }) | ||
return | ||
} | ||
}) | ||
|
||
// Map request data to processing options | ||
const { options, llmOpt, transcriptOpt } = reqToOpts(requestData) | ||
console.log('Calling processFile with params:', { filePath, llmOpt, transcriptOpt, options }) | ||
|
||
await processFile(filePath, llmOpt, transcriptOpt, options) | ||
|
||
console.log('processFile completed successfully') | ||
reply.send({ message: 'File processed successfully.' }) | ||
} catch (error) { | ||
console.error('Error processing file:', error) | ||
reply.status(500).send({ error: 'An error occurred while processing the file' }) | ||
} | ||
} | ||
|
||
export { handleFileRequest } |
Oops, something went wrong.