Skip to content

Commit

Permalink
cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
ceifa committed Jun 1, 2023
1 parent d1cf4eb commit ddcdc4a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/hosting/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { config, initializeConfiguration } from '../config.ts'
import { resolveGateway } from '../gateways/gateways.ts'
import { logger, setup } from '../log.ts'
import { handleMessage } from '../state.ts'

export const createFetchListener = async (directory: string) => {
await initializeConfiguration(directory, false)
const logLevel = config().logLevel
if (logLevel) {
setup(logLevel)
}

return (request: Request): Promise<Response> => {
try {
return resolveGateway(request, async (sourceAuthorId, message, gateway) => {
logger.info('Received message from ' + sourceAuthorId + ' via ' + gateway.sourceId, {
sourceAuthorId: sourceAuthorId,
message,
gateway: gateway.sourceId,
})

await handleMessage(sourceAuthorId, message, gateway, directory)
})
} catch (err) {
logger.error(err)
return Promise.resolve(
Response.json(
{
error: 'Error receiving message: ' + err.message,
},
{ status: 500 },
),
)
}
}
}

0 comments on commit ddcdc4a

Please sign in to comment.