Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dbdata
dbdata
.env
Empty file added backend/logs/errors.log
Empty file.
127 changes: 66 additions & 61 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"license": "ISC",
"description": "API for a simple notes web app",
"dependencies": {
"@google/generative-ai": "^0.24.1",
"bcrypt": "^6.0.0",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"dotenv": "^17.2.1",
"express": "^5.1.0",
"jsonwebtoken": "^9.0.2",
"passport": "^0.7.0",
Expand Down
22 changes: 16 additions & 6 deletions backend/src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import { notesRouter } from './routes/notesRouter.js';
Expand All @@ -12,30 +13,38 @@ import { addLogger } from './config/logger.config.js';
import { sequelize } from './config/database.js';
import './models/models.js';
import { createAdminUserIfNotExists } from './utils.js';
import { aiRouter } from './routes/aiRouter.js';

const PORT = process.env.PORT || 3000;

const app = express();

const allowedOrigins = [`http://localhost:${PORT}`, 'http://localhost:3001', 'http://localhost:3004', 'http://localhost:8080'];

console.log("CORS is configured for these origins1:", allowedOrigins);

app.use(cors({
origin: allowedOrigins,
credentials: true
}));



app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
initializePassport();
app.use(passport.initialize());

const allowedOrigins = [`http://localhost:${PORT}`, `http://localhost:3001`];

app.use(cors({
origin: allowedOrigins,
credentials: true
}));


app.use(addLogger);
app.use('/api/notes', notesRouter.getRouter());
app.use('/api/categories', categoriesRouter.getRouter());
app.use('/api/sessions', sessionsRouter.getRouter());
app.use('/api/docs', swaggerUiExpress.serve, swaggerUiExpress.setup(docsSpecs, { swaggerOptions: { withCredentials: true } }));
app.use('/api/ai', aiRouter.getRouter());

sequelize.authenticate()
.then(async () => {
Expand All @@ -49,4 +58,5 @@ sequelize.authenticate()
.catch((error) => {
console.error('Unable to connect to the database:', error);
process.exit(1);
});
});

21 changes: 21 additions & 0 deletions backend/src/controllers/ai.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { aiService } from '../services/ai.service.js';

class AIController {
async generate(req, res) {
const { prompt } = req.body;

if (!prompt) {
return res.sendBadRequestError("'prompt' is required in the request body.");
}

try {
const generatedText = await aiService.generateContent(prompt);
res.sendOk({ content: generatedText });
} catch (error) {
req.logger.error(`AI content generation failed: ${error.message}`);
res.sendInternalServerError("Error generating content with AI.");
}
}
}

export const aiController = new AIController();
11 changes: 11 additions & 0 deletions backend/src/routes/aiRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Router from './router.js';
import { aiController } from '../controllers/ai.controller.js';

class AIRouter extends Router {
init() {
// Only authenticated users can access the AI feature.
this.post('/generate', ["USER"], async (req, res) => this.controller.generate(req, res));
}
}

export const aiRouter = new AIRouter(aiController);
Loading