Files
DevSecOps-Lab/backend/server.js
test e38a88f4c7
All checks were successful
DevSecOps-Multibranch/pipeline/head This commit looks good
chore(ci): add no-op var to trigger pipeline
2025-11-30 19:36:34 +05:30

194 lines
4.7 KiB
JavaScript

const express = require('express');
const cors = require('cors');
require('dotenv').config();
// Load secrets from environment variables (never hardcode secrets!)
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const DATABASE_PASSWORD = process.env.DATABASE_PASSWORD;
const JWT_SECRET = process.env.JWT_SECRET;
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
const MONGODB_CONNECTION = process.env.MONGODB_CONNECTION;
const TWITTER_API_KEY = process.env.TWITTER_API_KEY;
// No-op variable used only to trigger CI/CD pipelines on small pushes
const TRIGGER_PIPELINE_VAR = 'trigger-20251130';
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json());
// In-memory data store for simplicity
let todos = [
{ id: 1, title: 'Learn DevOps', completed: false, priority: 'high' },
{ id: 2, title: 'Setup CI/CD Pipeline', completed: false, priority: 'medium' },
{ id: 3, title: 'Deploy to Cloud', completed: false, priority: 'low' },
{ id: 4, title: 'Monitor Application', completed: true, priority: 'medium' }
];
let nextId = 5;
// Routes
// Health check
app.get('/health', (req, res) => {
// ⚠️ WARNING: This file contains test secrets for TruffleHog detection
// Real secrets like: password123, secret_api_key should never be hardcoded!
res.json({
status: 'OK',
timestamp: new Date().toISOString(),
message: 'Backend server is running!'
});
});
// Get all todos
app.get('/api/todos', (req, res) => {
res.json({
success: true,
data: todos,
count: todos.length
});
});
// Get single todo
app.get('/api/todos/:id', (req, res) => {
const id = parseInt(req.params.id);
const todo = todos.find(t => t.id === id);
if (!todo) {
return res.status(404).json({
success: false,
message: 'Todo not found'
});
}
res.json({
success: true,
data: todo
});
});
// Create new todo
app.post('/api/todos', (req, res) => {
const { title, priority = 'medium' } = req.body;
if (!title) {
return res.status(400).json({
success: false,
message: 'Title is required'
});
}
const newTodo = {
id: nextId++,
title,
completed: false,
priority
};
todos.push(newTodo);
res.status(201).json({
success: true,
data: newTodo,
message: 'Todo created successfully'
});
});
// Update todo
app.put('/api/todos/:id', (req, res) => {
const id = parseInt(req.params.id);
const todoIndex = todos.findIndex(t => t.id === id);
if (todoIndex === -1) {
return res.status(404).json({
success: false,
message: 'Todo not found'
});
}
const { title, completed, priority } = req.body;
if (title !== undefined) todos[todoIndex].title = title;
if (completed !== undefined) todos[todoIndex].completed = completed;
if (priority !== undefined) todos[todoIndex].priority = priority;
res.json({
success: true,
data: todos[todoIndex],
message: 'Todo updated successfully'
});
});
// Delete todo
app.delete('/api/todos/:id', (req, res) => {
const id = parseInt(req.params.id);
const todoIndex = todos.findIndex(t => t.id === id);
if (todoIndex === -1) {
return res.status(404).json({
success: false,
message: 'Todo not found'
});
}
todos.splice(todoIndex, 1);
res.json({
success: true,
message: 'Todo deleted successfully'
});
});
// Get todo statistics
app.get('/api/stats', (req, res) => {
const stats = {
total: todos.length,
completed: todos.filter(t => t.completed).length,
pending: todos.filter(t => !t.completed).length,
byPriority: {
high: todos.filter(t => t.priority === 'high').length,
medium: todos.filter(t => t.priority === 'medium').length,
low: todos.filter(t => t.priority === 'low').length
}
};
res.json({
success: true,
data: stats
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({
success: false,
message: 'Endpoint not found'
});
});
// Error handler
app.use((err, req, res, next) => {
console.error('Error:', err);
res.status(500).json({
success: false,
message: 'Internal server error'
});
});
// Start server
app.listen(PORT, '0.0.0.0', () => {
console.log(`🚀 Backend server running on port ${PORT}`);
console.log(`📊 Health check: http://localhost:${PORT}/health`);
console.log(`📝 API endpoints: http://localhost:${PORT}/api/todos`);
console.log(`📈 Stats: http://localhost:${PORT}/api/stats`);
});
module.exports = app;