Skip to content

Commit

Permalink
Refactor existing backend code, optimize display for smaller screens
Browse files Browse the repository at this point in the history
  • Loading branch information
aliiyuu committed Aug 14, 2024
1 parent c763f12 commit 426f2f2
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 85 deletions.
13 changes: 13 additions & 0 deletions frontend/src/components/Chatbot.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
opacity: 0.95;
overflow-y: auto;
}

@media only screen and (max-width: 600px) {
.container {
width: 75vw;
padding: 30px;
}
}

#chatbox {
border-radius: 10px;
Expand All @@ -31,6 +38,12 @@
font-size: 250%;
text-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

@media only screen and (max-width: 600px) {
h1 {
font-size: 210%;
}
}

#messages {
list-style: none;
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function downloadCodebase() {
const url = document.getElementById('codebase-url').value;
if (url.trim() === '') return;

const response = await fetch(`${apiURL}/api/download`, {
const response = await fetch(`${local}/api/download`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -64,9 +64,9 @@ export async function fetchChatGPTResponse(userInput) {
const ec2 = 'https://18.226.98.245:443';
const local = 'http://localhost:3000';
const apiURL = 'https://syntaxsorcerer-backend.fly.dev'
const codebasePath = '../../server/codebases/main';
const codebasePath = '../../codebases';

const response = await fetch(`${apiURL}/api/chatgpt`, {
const response = await fetch(`${local}/api/chatgpt`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down
9 changes: 9 additions & 0 deletions server/config/openaiConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const OpenAI = require("openai");
require("dotenv").config();

// Initialize OpenAI with the API key
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

module.exports = openai;
41 changes: 41 additions & 0 deletions server/controllers/downloadCodebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const axios = require("axios");
const AdmZip = require("adm-zip");
const fs = require("fs");
const path = require("path");

// Endpoint to download and cache a codebase
const downloadCodebase = async (req, res) => {
const { url } = req.body;

if (!url) {
return res.status(400).json({ error: "URL is required" });
}

try {
const response = await axios({
url,
method: "GET",
responseType: "arraybuffer",
});

const zip = new AdmZip(response.data);
const extractPath = path.join(
path.resolve(__dirname, "../../"), // Navigate to the root directory and add the codebases directory
"codebases",
path.basename(url, ".zip"),
);
zip.extractAllTo(extractPath, true);

res.json({
message: "Codebase downloaded and extracted",
path: extractPath,
});
} catch (error) {
console.error("Error downloading codebase:", error);
res.status(500).json({ error: "Failed to download codebase" });
}
};

module.exports = {
downloadCodebase
};
36 changes: 36 additions & 0 deletions server/controllers/gptController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const openai = require("../config/openaiConfig");

// Send a request to the ChatGPT API
const getChatGPTResponse = async (req, res) => {
const userInput = req.body.prompt;

if (!userInput) {
return res.status(400).json({ error: "Prompt is required" });
}

if (!process.env.OPENAI_API_KEY) {
return res.status(500).json({ error: "API key is missing" });
}

try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo", // Updated model name
messages: [{ role: "user", content: userInput }],
max_tokens: 150,
});

const data = response.choices;
if (data && data.length > 0) {
res.json({ text: data[0].message.content.trim() });
} else {
res.status(500).json({ error: "Invalid response from API" });
}
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Something went wrong" });
}
};

module.exports = {
getChatGPTResponse
};
9 changes: 9 additions & 0 deletions server/routes/downloadRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express");
const { downloadCodebase } = require("../controllers/downloadCodebase");

const router = express.Router();

router.post("/download", downloadCodebase);

module.exports = router;

9 changes: 9 additions & 0 deletions server/routes/gptRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express");
const { getChatGPTResponse } = require("../controllers/gptController");

const router = express.Router();

router.post("/chatgpt", getChatGPTResponse);

module.exports = router;

94 changes: 12 additions & 82 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,21 @@
const express = require("express");
const OpenAI = require("openai");
const axios = require("axios");
const AdmZip = require("adm-zip");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 3000;
var cors = require("cors");
const chatgptRoutes = require("./routes/gptRoutes");
const downloadRoutes = require("./routes/downloadRoutes");

app.use(cors());
app.use(express.json());

// Specify where the static frontend files are locateds
app.use(express.static(path.join(__dirname, "client/build")));
//app.use('/', express.static(path.join(__dirname, 'public')))

// Initialize OpenAI with the API key
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

// Endpoint to download and cache a codebase
app.post("/api/download", async (req, res) => {
const { url } = req.body;

if (!url) {
return res.status(400).json({ error: "URL is required" });
}

try {
const response = await axios({
url,
method: "GET",
responseType: "arraybuffer",
});

const zip = new AdmZip(response.data);
const extractPath = path.join(
__dirname,
"codebases",
path.basename(url, ".zip"),
);
zip.extractAllTo(extractPath, true);

res.json({
message: "Codebase downloaded and extracted",
path: extractPath,
});
} catch (error) {
console.error("Error downloading codebase:", error);
res.status(500).json({ error: "Failed to download codebase" });
}
});

// Send a request to the ChatGPT API
app.post("/api/chatgpt", async (req, res) => {
const userInput = req.body.prompt;

if (!userInput) {
return res.status(400).json({ error: "Prompt is required" });
}

if (!process.env.OPENAI_API_KEY) {
return res.status(500).json({ error: "API key is missing" });
}
const app = express();
const PORT = process.env.PORT || 3000;

try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo", // Updated model name
messages: [{ role: "user", content: userInput }],
max_tokens: 150,
});
app.use(cors({
origin: "*" // Update as needed for security, currently accessible by any domain
}),
);

const data = response.choices;
if (data && data.length > 0) {
res.json({ text: data[0].message.content.trim() });
} else {
res.status(500).json({ error: "Invalid response from API" });
}
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Something went wrong" });
}
});
app.use(express.json());
app.use("/api", chatgptRoutes);
app.use("/api", downloadRoutes);

app.get("/", (req, res) => res.send("Deployed! 🚀"));
app.get("/", (req, res) => res.send(`Server is running on port ${PORT}`));

app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
Expand Down

0 comments on commit 426f2f2

Please sign in to comment.