-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathAPIMyLlama.js
69 lines (61 loc) · 2.27 KB
/
APIMyLlama.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require('express');
const { initializeDatabase, closeDatabase, getDb } = require('./db');
const { startServer, askForPort, askForOllamaURL, startCLI } = require('./utils');
const { setupRoutes } = require('./api');
const fs = require('fs');
const app = express();
app.use(express.json());
console.log('APIMyLlama V2 is being started. Thanks for choosing Gimer Studios.');
// Middleware for logging requests
app.use((req, res, next) => {
console.log(`Received a ${req.method} request at ${req.url}`);
next();
});
// Initialize the database
const db = initializeDatabase();
// Setup API routes
setupRoutes(app, db);
// Close the database connection when the application is closed
process.on('SIGINT', () => {
closeDatabase();
});
// Start the server
setTimeout(() => {
if (fs.existsSync('port.conf')) {
fs.readFile('port.conf', 'utf8', (err, data) => {
if (err) {
console.error('Error reading port number from file:', err.message);
askForPort(app, startServer, askForOllamaURL, startCLI, db);
} else {
const port = parseInt(data.trim());
if (isNaN(port)) {
console.error('Invalid port number in port.conf');
askForPort(app, startServer, askForOllamaURL, startCLI, db);
} else {
if (fs.existsSync('ollamaURL.conf')) {
fs.readFile('ollamaURL.conf', 'utf8', (err, data) => {
if (err) {
console.error('Error reading Ollama url from file:', err.message);
askForOllamaURL(app, startServer, startCLI, port, db);
} else {
const ollamaURL = data.trim();
if (typeof ollamaURL !== 'string' || ollamaURL === '') {
console.error('Invalid Ollama url in ollamaURL.conf');
askForOllamaURL(app, startServer, startCLI, port, db);
} else {
startServer(port, app);
startCLI(db);
}
}
});
} else {
askForOllamaURL(app, startServer, startCLI, port, db);
}
}
}
});
} else {
askForPort(app, startServer, askForOllamaURL, startCLI, db);
}
}, 1000);
module.exports = app;