const { Client } = require('pg'); const { DefaultAzureCredential } = require("@azure/identity"); const { SecretClient } = require("@azure/keyvault-secrets"); const express = require('express'); const path = require('path'); const cookieParser = require('cookie-parser'); const logger = require('morgan'); const createError = require('http-errors'); const indexRouter = require('./routes/index'); const usersRouter = require('./routes/users'); const app = express(); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); app.use('/users', usersRouter); app.use(function (req, res, next) { next(createError(404)); }); app.use(function (err, req, res, next) { res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; res.status(err.status || 500); res.render('error'); }); async function getSecret(secretName) { const credential = new DefaultAzureCredential(); const keyVaultName = "midterm-kv"; if (!keyVaultName) throw new Error("KEY_VAULT_NAME is empty"); const url = "https://" + keyVaultName + ".vault.azure.net"; const client = new SecretClient(url, credential); const secret = await client.getSecret(secretName); return secret.value; } const dbConfig = { user: '', host: '', database: 'postgres', password: '', port: 5432 }; app.get('/hello', async (req, res) => { try { dbConfig.password = await getSecret("dbpassword"); dbConfig.user = await getSecret("username"); dbConfig.host = await getSecret("host"); const client = new Client(dbConfig); await client.connect(); const result = await client.query('SELECT * FROM users'); console.log("result", result); res.render('hello', { data: result.rows }); await client.end(); } catch (err) { console.error('PostgreSQL hatası:', err); res.status(500).send('Sunucu hatası'); } }); const PORT = process.env.PORT || 4000; app.listen(PORT, () => { console.log(`Sunucu ${PORT} portunda çalışıyor`); }); module.exports = app;