-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (153 loc) · 4.99 KB
/
index.js
File metadata and controls
174 lines (153 loc) · 4.99 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Load environment variables from .env file
require('dotenv').config();
const path = require('path')
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const fs = require('fs')
const bb = require('express-busboy')
const https = require('https')
const args = require('minimist')(process.argv.slice(2))
const { create } = require('express-handlebars')
// --- Import Routers ---
const indexRouter = require('./routes/index');
const textClassifierRouter = require('./routes/text');
const gcpImageRouter = require('./routes/image');
// Check for required environment variables for GCP
if (!process.env.GCP_PROJECT_ID || !process.env.GCS_BUCKET_NAME || !process.env.GOOGLE_APPLICATION_CREDENTIALS || !process.env.GCP_REGION) {
console.error('FATAL ERROR: GCP_PROJECT_ID, GCS_BUCKET_NAME, GOOGLE_APPLICATION_CREDENTIALS, and GCP_REGION environment variables are required in .env file.');
// Optionally check if the credential file exists
try {
if (!fs.existsSync(process.env.GOOGLE_APPLICATION_CREDENTIALS)) {
console.error(`FATAL ERROR: Google credentials file not found at: ${process.env.GOOGLE_APPLICATION_CREDENTIALS}`);
}
} catch (err) {
console.error(`FATAL ERROR: Error checking credentials file path: ${process.env.GOOGLE_APPLICATION_CREDENTIALS}`, err);
}
process.exit(1); // Exit if essential config is missing
} else {
console.log('GCP configuration variables found.');
console.log(` GCP Project ID: ${process.env.GCP_PROJECT_ID}`);
console.log(` GCS Bucket Name: ${process.env.GCS_BUCKET_NAME}`);
console.log(` Credentials File: ${process.env.GOOGLE_APPLICATION_CREDENTIALS}`);
console.log(` GCP Region: ${process.env.GCP_REGION}`);
}
// Set default port if not provided in environment
const PORT = process.env.PORT || process.env.SERVER_PORT || 2634;
// SSL configuration from environment variables
const SSL_KEY_PATH = process.env.SSL_KEY_PATH;
const SSL_CERT_PATH = process.env.SSL_CERT_PATH;
const app = express()
app.use(cors())
const hbs = create({
defaultLayout: 'main'
})
app.engine('handlebars', hbs.engine)
app.set('view engine', 'handlebars')
app.use(express.static(path.join(__dirname, 'static')))
app.use(bodyParser.json({limit: '50mb'}))
app.use(bodyParser.urlencoded({
extended: false,
limit: '50mb'
}))
// --- Mount Routers ---
app.use('/', indexRouter);
app.use('/classify/text', textClassifierRouter);
app.use('/classify/image', gcpImageRouter);
// Keep express-busboy for now, though multer is used in clarifai route
// Consider standardizing later if causing issues.
bb.extend(app, {
upload: true
})
// --- Server Start Logic ---
const port = process.env.SERVER_PORT || 2634;
// Start server with appropriate protocol
if (args.http === true || !SSL_KEY_PATH || !SSL_CERT_PATH) {
// Start HTTP Server
const server = https.createServer(app);
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
});
} else {
try {
const options = {
key: fs.readFileSync(SSL_KEY_PATH),
cert: fs.readFileSync(SSL_CERT_PATH)
}
const server = https.createServer(options, app)
server.listen(port, () => {
console.log(`Server running at https://localhost:${port}`)
})
} catch (error) {
console.error('SSL configuration error:', error.message)
console.log('Falling back to HTTP')
const server = https.createServer(app);
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
});
}
}
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening(server) {
return () => {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Server listening on ' + bind);
}
}
// Optional: Add 404 and general error handlers (like in initial structure)
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
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');
});
module.exports = app;