Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

2.0 KB
Raw Blame
import express from 'express';
import { Server } from 'http';
import { app } from '../index.js';
import config from './config';

const ADMIN_PORT = config.env.ADMIN_PORT || 5054;
const PORT = config.env.PORT || 5053;
// eslint-disable-next-line import/no-mutable-exports

let server: Server;

function enableAppPort() {
  if (server && server.listening) {
    console.info(`Server is already running at http://localhost:${PORT}`);
    return server;
  }

  server = app.listen(PORT, '0.0.0.0', () => {
    console.info(`Server is running at http://localhost:${PORT}`);
  });

  return server;
}

// Function to disable port 5000 to stop accepting new connections
function disableAppPort() {
  server.close(() => {
    console.info(`App Server listening on port ${PORT} no longer accepting connections`);
  });
}

// Management app listening on port 5054
const managementApp = express();

// Route to handle management operations
managementApp.get('/', (req, res) => {
  res.send('Management operations.');
});

// Route to enable port 5000 for new connections
managementApp.get('/enable', (req, res) => {
  enableAppPort();
  res.send(`Port ${PORT} enabled for new connections via management port.`);
});

// Route to disable port 5000 for new connections
managementApp.get('/disable', (req, res) => {
  disableAppPort();
  res.send(`Port ${PORT} disabled for new connections via management port.`);
});

managementApp.get('/stats', (req, res) => {
  server.getConnections((err, count) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.json({ connections: count });
    }
  });
});

// managementApp.get('/metrics', async (req, res) => {
//   LOGGER.info('FLUSHING METRICS TO PROMETHEUS');
//   res.set('Content-Type', metricsManager.metricesRegister.contentType);
//   res.end(await metricsManager.metricesRegister.metrics());
// });

export function startServers() {
  managementApp.listen(ADMIN_PORT, 'localhost', () => {
    console.info(`Management server listening on port ${ADMIN_PORT}`);
  });

  const appServer = enableAppPort();
  return appServer;
}