Move synapse connection and logger to separate modules
This commit is contained in:
+8
-23
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
),
|
||||
})
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user