const { Client } = require('pg'); var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); const { SecretClient } = require("@azure/keyvault-secrets"); const { DefaultAzureCredential } = require("@azure/identity"); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); var 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 kvHandler() { // const credential = new DefaultAzureCredential({ // tenantId: process.env["AZURE_TENANT_ID"], // clientId: process.env["AZURE_CLIENT_ID"], // clientSecret: process.env["AZURE_CLIENT_SECRET"] // }); 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); var secretName = "dbpassword" const secret = await client.getSecret(secretName); console.log("secret: ", secret); } kvHandler().catch((error) => { console.error("An error occurred:", error); process.exit(1); }); async function getPostgrePassword() { const secretName = "dbpassword"; const secret = await clientKeyVault.getSecret(secretName); return secret.value; } async function getPostgreUsername() { const secretName = "username"; const secret = await clientKeyVault.getSecret(secretName); return secret.value; } async function getPostgreHost() { const secretName = "host"; const secret = await clientKeyVault.getSecret(secretName); return secret.value; } const dbConfig = { user: '', host: '', database: 'postgres', password: '', port: 5432 }; app.get('/hello', async (req, res) => { // try { // dbConfig.password = await getPostgrePassword(); // dbConfig.user = await getPostgreUsername(); // dbConfig.host = await getPostgreHost(); // 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 }); // } catch (err) { // console.error('PostgreSQL hatası:', err); // res.status(500).json({ error: 'Sunucu hatası' }); // } try { dbConfig.password = await getPostgrePassword(); dbConfig.user = await getPostgreUsername(); dbConfig.host = await getPostgreHost(); console.log('dbconfig', dbConfig); const client = await pool.connect(); client.connect(); await client.query('SELECT * FROM users', (err, res) => { if (err) { console.error('Hata:', err); } else { console.log('Kullanıcılar:'); for (let row of res.rows) { console.log(row); } } // Bağlantıyı kapat client.end(); }); client.release(); console.log("başarılı"); } catch (err) { console.error(err); res.status(500).send('Error retrieving users from database'); } }); const PORT = process.env.PORT || 4000; app.listen(PORT, () => { console.log(`Sunucu ${PORT} portunda çalışıyor`); }); module.exports = app;