feat: adding all the services logic on database

This commit is contained in:
Carlos
2025-04-09 23:00:35 -04:00
commit edbbf7ea9e
4 changed files with 1530 additions and 0 deletions

50
server.js Normal file
View File

@ -0,0 +1,50 @@
require("dotenv").config(); // Load environment variables from .env file
const express = require("express");
const axios = require("axios");
const bodyParser = require("body-parser");
const app = express();
const port = 3000; // Backend server will run on this port
// Middleware to parse JSON request bodies
app.use(bodyParser.json());
// API Key validation middleware
const validateApiKey = (req, res, next) => {
const apiKey = req.headers["api-key"];
if (!apiKey) {
return res.status(400).json({ error: "API key is missing" });
}
if (apiKey !== process.env.API_KEY) {
return res.status(403).json({ error: "Invalid API key" });
}
next(); // Proceed if the API key is valid
};
// Forward request to localhost:11434 (ollama)
app.post("/api/generate", validateApiKey, async (req, res) => {
try {
// Forwarding the request to localhost:11434 with the prompt
const response = await axios.post(
"http://localhost:11434/api/generate",
req.body
);
// Send the response from localhost:11434 back to the client
res.status(200).json(response.data);
} catch (error) {
// Enhanced error logging
console.error("Error forwarding request to localhost:11434:", error.response ? error.response.data : error.message);
res.status(500).json({ error: "Internal Server Error", message: error.message });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});