From d45e2d141e82b8faf936ebb5928494fda626e05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannik=20R=C3=B6del?= Date: Thu, 7 Oct 2021 16:25:15 +0200 Subject: [PATCH] Add backend boilerplate --- src/backend.mjs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/backend.mjs diff --git a/src/backend.mjs b/src/backend.mjs new file mode 100644 index 0000000..b010d8b --- /dev/null +++ b/src/backend.mjs @@ -0,0 +1,43 @@ +import express from 'express'; + +/** + * Get Zammad access credentials and verify the connection. + * @param {ReturnType} app + */ +function initializeZammad(app) { + console.info('Checking connection to Zammad...'); + + let url = process.env.ZAMMAD_URL || ''; + if (url === '') { + throw new Error( + 'Could not find the Zammad URL to connect to. Make sure it is provided using the ZAMMAD_URL environment variable. It should point to the root URL of the Zammad installation.' + ); + } + url = url.replace(/\/$/, ''); + + const token = process.env.ZAMMAD_TOKEN || ''; + if (token === '') { + throw new Error( + 'Could not find authentication credentials for Zammad. Make sure the token is provided using the ZAMMAD_TOKEN environment variable.' + ); + } + + app.set('zammad url', url); + app.set('zammad token', token); + + // TODO Verify the connection +} + +function run() { + const app = express(); + + initializeZammad(app); + + app.post('/kontakt', (req, res) => {}); + + const port = parseInt(process.env.LISTEN_PORT || '3000'); + return app.listen(port, () => { + console.log(`Server listening at http://localhost:${port}...`); + }); +} +run();