|
| 1 | +import 'reflect-metadata' |
| 2 | +import { dirname, resolve } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { AsyncAdapter, FastifySteno } from '@stenodb/fastify' |
| 5 | +import Fastify from 'fastify' |
| 6 | +import { postsController } from './api/posts/posts.controller.js' |
| 7 | +import { userController } from './api/users/users.controller.js' |
| 8 | +import { UserService } from './api/users/users.service.js' |
| 9 | +import { Post } from './dto/posts.dto.js' |
| 10 | +import { User, Users } from './dto/users.dto.js' |
| 11 | + |
| 12 | +const fastify = Fastify() |
| 13 | + |
| 14 | +const usersInitialData = new Users( |
| 15 | + new User(1, 'john', 18, new Post(1, 'Lorem ipsum', new Date())), |
| 16 | + new User(2, 'alice', 23) |
| 17 | +) |
| 18 | + |
| 19 | +const usersAdapter = new AsyncAdapter( |
| 20 | + 'users', |
| 21 | + Users, |
| 22 | + usersInitialData |
| 23 | +) |
| 24 | + |
| 25 | +fastify.register(FastifySteno, { |
| 26 | + path: resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db'), |
| 27 | + entities: [User, Post], |
| 28 | + adapters: [usersAdapter] |
| 29 | +}) |
| 30 | + |
| 31 | +fastify.register( |
| 32 | + (instance, _, done) => { |
| 33 | + const userService = new UserService(fastify) |
| 34 | + |
| 35 | + instance.register( |
| 36 | + (instance, _, done) => { |
| 37 | + userController(instance, userService) |
| 38 | + done() |
| 39 | + }, |
| 40 | + { prefix: '/users' } |
| 41 | + ) |
| 42 | + |
| 43 | + instance.register( |
| 44 | + (instance, _, done) => { |
| 45 | + postsController(instance, userService) |
| 46 | + done() |
| 47 | + }, |
| 48 | + { prefix: '/posts' } |
| 49 | + ) |
| 50 | + |
| 51 | + done() |
| 52 | + }, |
| 53 | + { prefix: '/api' } |
| 54 | +) |
| 55 | + |
| 56 | +fastify.get('/schemas', () => { |
| 57 | + return fastify.getSchemas() |
| 58 | +}) |
| 59 | + |
| 60 | +fastify.listen({ host: '0.0.0.0', port: 3000 }, (err, address) => { |
| 61 | + if (err) throw err |
| 62 | + console.log(address) |
| 63 | +}) |
0 commit comments