Merge remote-tracking branch 'origin/main'
This commit is contained in:
40
server.js
40
server.js
@@ -6,6 +6,9 @@ import { fileURLToPath } from 'node:url';
|
||||
import 'dotenv/config';
|
||||
|
||||
import { connectDb, closeDb } from './src/db.js';
|
||||
import { isDemoMode } from './src/demo/mode.js';
|
||||
import { loadDemoCatalog } from './src/demo/store.js';
|
||||
import { readCertMetadata } from './src/cert-meta.js';
|
||||
import { createJtlPosServer } from './src/jtl-server.js';
|
||||
import { createPairingStore } from './src/pairing.js';
|
||||
import { closeOrderLog } from './src/order-log.js';
|
||||
@@ -59,11 +62,18 @@ function formatBody(buffer) {
|
||||
return `[binary ${buffer.length} bytes]`;
|
||||
}
|
||||
|
||||
const certPem = fs.readFileSync(certPath);
|
||||
const keyPem = fs.readFileSync(keyPath);
|
||||
const certMeta = readCertMetadata(certPem);
|
||||
|
||||
const pairingStore = createPairingStore();
|
||||
pairingStore.setPairingCode(PAIRING_CODE, 'JTL-POS');
|
||||
pairingStore.registerDevice(AUTH_TOKEN, 'JTL-POS');
|
||||
|
||||
const jtlHandler = createJtlPosServer(pairingStore, { authToken: AUTH_TOKEN });
|
||||
const jtlHandler = createJtlPosServer(pairingStore, {
|
||||
authToken: AUTH_TOKEN,
|
||||
...certMeta,
|
||||
});
|
||||
|
||||
const loggedJtlHandler = async (req, res) => {
|
||||
const started = Date.now();
|
||||
@@ -108,8 +118,8 @@ const loggedJtlHandler = async (req, res) => {
|
||||
|
||||
const httpsServer = https.createServer(
|
||||
{
|
||||
key: fs.readFileSync(keyPath),
|
||||
cert: fs.readFileSync(certPath),
|
||||
key: keyPem,
|
||||
cert: certPem,
|
||||
},
|
||||
loggedJtlHandler
|
||||
);
|
||||
@@ -117,14 +127,22 @@ const httpsServer = https.createServer(
|
||||
const httpServer = http.createServer(loggedJtlHandler);
|
||||
|
||||
async function start() {
|
||||
try {
|
||||
const pool = await connectDb();
|
||||
logger.success(`MSSQL connected: ${process.env.MSSQL_SERVER}/${process.env.MSSQL_DATABASE}`);
|
||||
const activeShop = await fetchActiveShop(pool);
|
||||
logger.info(`Active shop ID: ${activeShop}`);
|
||||
} catch (err) {
|
||||
logger.warn(`MSSQL connection skipped: ${err.message}`);
|
||||
logger.warn('POS handshake will still work; sync from database is not available yet.');
|
||||
if (isDemoMode()) {
|
||||
const stats = await loadDemoCatalog();
|
||||
logger.success(
|
||||
`DEMO_MODE: loaded catalog (${stats.products} products, ${stats.categories} categories, ${stats.customerGroups} customer groups, ${stats.composites} composite links)`
|
||||
);
|
||||
logger.info('MSSQL is skipped while DEMO_MODE=true');
|
||||
} else {
|
||||
try {
|
||||
const pool = await connectDb();
|
||||
logger.success(`MSSQL connected: ${process.env.MSSQL_SERVER}/${process.env.MSSQL_DATABASE}`);
|
||||
const activeShop = await fetchActiveShop(pool);
|
||||
logger.info(`Active shop ID: ${activeShop}`);
|
||||
} catch (err) {
|
||||
logger.warn(`MSSQL connection skipped: ${err.message}`);
|
||||
logger.warn('POS handshake will still work; sync from database is not available yet.');
|
||||
}
|
||||
}
|
||||
|
||||
httpsServer.listen(PORT, '0.0.0.0', () => {
|
||||
|
||||
Reference in New Issue
Block a user