server.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const originalEmitWarning = process.emitWarning;
  2. process.emitWarning = function(warning, ...args) {
  3. if (typeof warning === 'string' && warning.includes('punycode')) {
  4. return;
  5. }
  6. return originalEmitWarning.apply(process, [warning, ...args]);
  7. };
  8. const express = require('express');
  9. const cors = require('cors');
  10. const path = require('path');
  11. const fs = require('fs');
  12. const config = require('./config');
  13. const text2text = require('./AIRequest/text2text');
  14. const img2text = require('./AIRequest/img2text');
  15. const app = express();
  16. app.use(cors());
  17. app.use(express.json());
  18. app.get('/health', (req, res) => {
  19. res.json({ status: 'ok' });
  20. });
  21. app.post('/api/chat', async (req, res) => {
  22. try {
  23. res.json(await text2text(req.body.prompt));
  24. } catch (error) {
  25. res.status(error.message === 'Missing prompt' ? 400 : 500).json({ success: false, error: error.message });
  26. }
  27. });
  28. app.post('/api/img2text', async (req, res) => {
  29. try {
  30. res.json(await img2text(req.body.prompt, req.body.imageUrl));
  31. } catch (error) {
  32. const statusCode = error.message === 'Missing prompt' || error.message === 'Missing imageUrl' ? 400 : 500;
  33. res.status(statusCode).json({ success: false, error: error.message });
  34. }
  35. });
  36. const PORT = config.PORT || 8848;
  37. const HOST = process.env.HOST || '0.0.0.0';
  38. try {
  39. fs.appendFileSync(path.join(__dirname, 'log.txt'), `[${new Date().toISOString()}] Starting server on http://${HOST}:${PORT}\n`);
  40. } catch (error) {
  41. console.error('Failed to write start log:', error);
  42. }
  43. app.listen(PORT, HOST, () => {
  44. const logMessage = `[${new Date().toISOString()}] Server running on http://${HOST}:${PORT}\n`;
  45. console.log(`Server running on http://${HOST}:${PORT}`);
  46. try {
  47. fs.appendFileSync(path.join(__dirname, 'log.txt'), logMessage);
  48. } catch (error) {
  49. console.error('Failed to write log:', error);
  50. }
  51. });