const express = require('express');
const mariadb = require('mariadb');
const config = require('./config');

const app = express();
const port = 443; // Use port 443

const pool = mariadb.createPool({
  host: config.host,
  user: config.user,
  password: config.password,
  database: config.database,
  connectionLimit: 5,
  acquireTimeout: 30000, // Increase timeout to 30 seconds
});

app.get('/', async (req, res) => {
  let conn;
  try {
    conn = await pool.getConnection();
    console.log("Connected to MariaDB!");
    const rows = await conn.query("SELECT * FROM Exercise ORDER BY RAND() LIMIT 1");
    console.log(rows); // [{val: 1}]
    res.json(rows); // Send the result as JSON response
  } catch (err) {
    console.error("Unable to connect to MariaDB:", err);
    res.status(500).send("Unable to connect to MariaDB");
  } finally {
    if (conn) conn.release(); // Release the connection back to the pool
  }
});

app.listen(port, '145.92.8.132', () => {
  console.log(`Server is listening on http://145.92.8.132:${port}`);
});
