This commit is contained in:
Jakub Kaniecki
2025-03-30 18:59:15 +02:00
parent 18a019fbc2
commit 1befef102f
11 changed files with 244 additions and 28 deletions

View File

@@ -6,7 +6,7 @@ WORKDIR /app
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
RUN npm install --only=production
# Copy source code
COPY . .

View File

@@ -11,18 +11,26 @@ dotenv.config();
const app = express();
const port = process.env.PORT || 3001;
// Trust proxy for rate limiting behind Traefik
app.set('trust proxy', 1);
// Middleware
app.use(helmet());
app.use(express.json());
app.use(cors({
origin: process.env.CORS_ORIGIN,
origin: process.env.CORS_ORIGIN || 'https://knck.pl',
methods: ['POST']
}));
// Rate limiting
const limiter = rateLimit({
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS) || 900000, // 15 minutes
max: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) || 5 // 5 requests per window
max: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) || 5, // 5 requests per window
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
keyGenerator: (req) => {
return req.ip || req.socket.remoteAddress;
}
});
app.use('/api/contact', limiter);
@@ -30,10 +38,34 @@ app.use('/api/contact', limiter);
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT),
secure: false,
secure: false, // true for 465, false for other ports like 587
auth: {
type: 'login',
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
},
tls: {
rejectUnauthorized: false // Required for some SMTP servers
},
debug: true, // Enable debug logging
logger: true // Enable logger
});
// Verify SMTP connection configuration
transporter.verify(function(error, success) {
if (error) {
console.error('SMTP connection error:', {
message: error.message,
code: error.code,
response: error.response,
command: error.command,
stack: error.stack,
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
user: process.env.SMTP_USER
});
} else {
console.log('SMTP server is ready to take our messages');
}
});
@@ -73,7 +105,13 @@ ${message}
res.status(200).json({ message: 'Email sent successfully' });
} catch (error) {
console.error('Error sending email:', error);
console.error('Error sending email:', {
message: error.message,
code: error.code,
response: error.response,
command: error.command,
stack: error.stack
});
res.status(500).json({ error: 'Failed to send email' });
}
});