Skip to content

Commit

Permalink
Separate API from main for better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jefgodesky committed Nov 30, 2024
1 parent fad426f commit e29807e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
29 changes: 29 additions & 0 deletions api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Application } from '@oak/oak'

import DB from './DB.ts'
import isTest from './utils/is-test.ts'

import UserRouter from './collections/users/router.ts'

const api = new Application()

const routers = [
UserRouter
]

for (const router of routers) {
api.use(router.routes())
api.use(router.allowedMethods())
}

api.addEventListener('listen', ({ hostname, port, secure }) => {
const protocol = secure ? 'https' : 'http'
const url = `${protocol}://${hostname ?? 'localhost'}:${port}`
if (!isTest()) console.log(`⚡ Listening on ${url}`)
})

api.addEventListener('close', async () => {
await DB.close()
})

export default api
23 changes: 1 addition & 22 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import { Application, type Router } from "@oak/oak"

import UserRouter from './collections/users/router.ts'

import api from './api.ts'
import getEnvNumber from './utils/get-env-number.ts'

const api = new Application()
const port = getEnvNumber('PORT', 80)

const routers = [
UserRouter
]

for (const router of routers) {
api.use(router.routes())
api.use(router.allowedMethods())
}

api.addEventListener('listen', ({ hostname, port, secure }) => {
const protocol = secure ? 'https' : 'http'
const url = `${protocol}://${hostname ?? 'localhost'}:${port}`
console.log(`⚡ Listening on ${url}`,
);
});

await api.listen({ port })

0 comments on commit e29807e

Please sign in to comment.