Move synapse connection and logger to separate modules

This commit is contained in:
Henrik Hüttemann 2023-05-22 16:02:52 +02:00
parent b138df0dac
commit a06ed05011
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
4 changed files with 40 additions and 24 deletions

View File

@ -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

View File

@ -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()

10
src/logger.ts Normal file
View File

@ -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()
),
})

22
src/synapse.ts Normal file
View File

@ -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<void>((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()
})
})