-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate API from main for better testing
- Loading branch information
1 parent
fad426f
commit e29807e
Showing
2 changed files
with
30 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |