diff --git a/docker-compose.yml b/docker-compose.yml index 973ab2a..00040b0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,7 +24,6 @@ services: # instance expose the TLS port directly: ports: - 8008:8008/tcp - # ... or use a reverse proxy, here is an example for traefik: # db: # image: docker.io/postgres:12-alpine diff --git a/src/app.ts b/src/app.ts index 0a04c01..d9723b5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,24 +1,9 @@ -import { access_token } from './config/synapse_access_token.json' -// import rcUsers from '../inputs/users.json' import fs from 'node:fs' import readline from 'node:readline' -import axios from 'axios' -import winston from 'winston' - -const log = (module.exports = winston.createLogger({ - level: 'debug', - transports: [new winston.transports.Console()], - format: winston.format.combine( - winston.format.colorize({ all: true }), - winston.format.simple() - ), -})) +import log from './logger' +import { whoami } from './synapse' log.info('rocketchat2matrix starts.') -axios.defaults.baseURL = 'http://localhost:8008' -axios.defaults.headers.common['Authorization'] = ` Bearer ${access_token}` -axios.defaults.headers.post['Content-Type'] = 'application/json' - interface RcUser { username: string name: string @@ -42,12 +27,12 @@ function loadRcExport(filename: string) { } async function main() { - const whoami = axios.get('/_matrix/client/v3/account/whoami') - whoami - .then((response) => log.info('Logged into synapse as', response.data)) - .catch((reason) => log.error(`Login to synapse failed: ${reason}`)) - - loadRcExport('users.json') + try { + await whoami() + await loadRcExport('users.json') + } catch (error) { + log.error(`Encountered an error booting up`) + } } main() diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 0000000..356139b --- /dev/null +++ b/src/logger.ts @@ -0,0 +1,10 @@ +import winston from 'winston' + +export default winston.createLogger({ + level: 'debug', + transports: [new winston.transports.Console()], + format: winston.format.combine( + winston.format.colorize({ all: true }), + winston.format.simple() + ), +}) diff --git a/src/synapse.ts b/src/synapse.ts new file mode 100644 index 0000000..0673cf6 --- /dev/null +++ b/src/synapse.ts @@ -0,0 +1,22 @@ +import { access_token } from './config/synapse_access_token.json' +import axios from 'axios' +import log from './logger' + +axios.defaults.baseURL = 'http://localhost:8008' +axios.defaults.headers.common['Authorization'] = ` Bearer ${access_token}` +axios.defaults.headers.post['Content-Type'] = 'application/json' + +export { default as axios } from 'axios' +export const whoami = () => + new Promise((resolve, reject) => { + axios + .get('/_matrix/client/v3/account/whoami') + .then((response) => { + log.info('Logged into synapse as', response.data) + resolve() + }) + .catch((reason) => { + log.error(`Login to synapse failed: ${reason}`) + reject() + }) + })