adding logs

This commit is contained in:
Carlos
2025-04-10 00:16:04 -04:00
parent a6a9587b1b
commit cfc7caf779

View File

@ -1,4 +1,3 @@
require("dotenv").config(); // Load environment variables from .env file
const express = require("express");
const axios = require("axios");
@ -14,14 +13,10 @@ app.use(bodyParser.json());
const validateApiKey = (req, res, next) => {
const apiKey = req.headers["api-key"];
const authHeader = req.headers['authorization'];
console.log('Authorization header:', authHeader);
console.log('!apiKey', boolean(!apiKey))
if (!apiKey) {
return res.status(400).json({ error: "API key is missing" });
}
console.log('checking api', apiKey !== process.env.API_KEY)
if (apiKey !== process.env.API_KEY) {
return res.status(403).json({ error: "Invalid API key" });
}
@ -33,18 +28,26 @@ const validateApiKey = (req, res, next) => {
app.post("/api/generate", validateApiKey, async (req, res) => {
try {
// Forwarding the request to localhost:11434 with the prompt
console.log('Body: ', req.body)
const authHeader = req.headers["authorization"];
console.log("Authorization header:", authHeader);
console.log("checking api", apiKey !== process.env.API_KEY);
console.log("Body: ", req.body);
const response = await axios.post(
"http://localhost:11434/api/generate",
req.body
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 });
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 });
}
});
@ -52,4 +55,3 @@ app.post("/api/generate", validateApiKey, async (req, res) => {
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});