Move synapse connection and logger to separate modules
This commit is contained in:
parent
b138df0dac
commit
a06ed05011
@ -24,7 +24,6 @@ services:
|
|||||||
# instance expose the TLS port directly:
|
# instance expose the TLS port directly:
|
||||||
ports:
|
ports:
|
||||||
- 8008:8008/tcp
|
- 8008:8008/tcp
|
||||||
# ... or use a reverse proxy, here is an example for traefik:
|
|
||||||
|
|
||||||
# db:
|
# db:
|
||||||
# image: docker.io/postgres:12-alpine
|
# image: docker.io/postgres:12-alpine
|
||||||
|
|||||||
31
src/app.ts
31
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 fs from 'node:fs'
|
||||||
import readline from 'node:readline'
|
import readline from 'node:readline'
|
||||||
import axios from 'axios'
|
import log from './logger'
|
||||||
import winston from 'winston'
|
import { whoami } from './synapse'
|
||||||
|
|
||||||
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()
|
|
||||||
),
|
|
||||||
}))
|
|
||||||
log.info('rocketchat2matrix starts.')
|
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 {
|
interface RcUser {
|
||||||
username: string
|
username: string
|
||||||
name: string
|
name: string
|
||||||
@ -42,12 +27,12 @@ function loadRcExport(filename: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const whoami = axios.get('/_matrix/client/v3/account/whoami')
|
try {
|
||||||
whoami
|
await whoami()
|
||||||
.then((response) => log.info('Logged into synapse as', response.data))
|
await loadRcExport('users.json')
|
||||||
.catch((reason) => log.error(`Login to synapse failed: ${reason}`))
|
} catch (error) {
|
||||||
|
log.error(`Encountered an error booting up`)
|
||||||
loadRcExport('users.json')
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
10
src/logger.ts
Normal file
10
src/logger.ts
Normal 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
22
src/synapse.ts
Normal 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()
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Reference in New Issue
Block a user