1 | import { DoneFunction, Reply, Request, Server } from '../types/fastify'
|
2 |
|
3 | import Connection from '../Connection'
|
4 | import schema from './schema'
|
5 |
|
6 | const checkConnection = {
|
7 | preHandler: function (request: Request, reply: Reply, done: DoneFunction) {
|
8 | const { name } = request.params
|
9 | if (!connections.has(name)) {
|
10 | reply.sendError(404, `Connection ${name} not found!`)
|
11 | return
|
12 | }
|
13 | done()
|
14 | }
|
15 | }
|
16 |
|
17 | export default async (app: Server) => {
|
18 | app.decorateRequest('getConnection', function (this: Request) {
|
19 | return connections.get(this.params.name)
|
20 | })
|
21 |
|
22 | app.get('/', (request, reply) => {
|
23 | reply.send(connections.map(connection => connection.toObject()))
|
24 | })
|
25 |
|
26 | app.post('/', {
|
27 | schema: {
|
28 | body: schema.connection
|
29 | }
|
30 | }, async (request, reply) => {
|
31 | if (connections.has(request.body.name)) return reply.sendError(409, `Connection already exists!`)
|
32 |
|
33 | const connection = await new Connection(request.body.name).save()
|
34 | if (request.body.endpoints && request.body.endpoints.length) {
|
35 | for (let endpoint of request.body.endpoints) {
|
36 | await connection.addEndpoint(endpoint)
|
37 | }
|
38 | }
|
39 |
|
40 | reply.send(connection.toObject())
|
41 | })
|
42 |
|
43 | app.get('/:name', checkConnection, async (request, reply) => {
|
44 | const connection = request.getConnection()
|
45 |
|
46 | reply.send(connection.toObject())
|
47 | })
|
48 |
|
49 | app.post('/:name/disable', checkConnection, async (request, reply) => {
|
50 | const connection = request.getConnection()
|
51 |
|
52 | if (connection.disabled) {
|
53 | return reply.sendError(400, `Connection ${connection.name} is already disabled!`)
|
54 | }
|
55 | await connection.disable().save()
|
56 |
|
57 | reply.send(connection.toObject())
|
58 | })
|
59 |
|
60 | app.post('/:name/enable', checkConnection, async (request, reply) => {
|
61 | const connection = request.getConnection()
|
62 |
|
63 | if (!connection.disabled) {
|
64 | return reply.sendError(400, `Connection ${connection.name} is already enabled!`)
|
65 | }
|
66 | await connection.enable().save()
|
67 |
|
68 | reply.send(connection.toObject())
|
69 | })
|
70 |
|
71 | app.delete('/:name', checkConnection, async (request, reply) => {
|
72 | const connection = request.getConnection()
|
73 |
|
74 | await connection.delete()
|
75 | return reply.send(connection.toObject())
|
76 | })
|
77 |
|
78 | app.get('/:name/endpoints', checkConnection, async (request, reply) => {
|
79 | const connection = request.getConnection()
|
80 |
|
81 | reply.send(connection.toObject().endpoints)
|
82 | })
|
83 |
|
84 | app.post('/:name/endpoints', {
|
85 | schema: {
|
86 | body: schema.endpoint
|
87 | },
|
88 | ...checkConnection
|
89 | }, async (request, reply) => {
|
90 | const connection = request.getConnection()
|
91 |
|
92 | if (connection.hasEndpoint(request.body.id)) {
|
93 | return reply.sendError(400, `Connection ${connection.name} already has endpoint ${request.body.id}`)
|
94 | }
|
95 |
|
96 | await connection.addEndpoint(request.body)
|
97 | reply.send(connection.toObject().endpoints)
|
98 | })
|
99 |
|
100 | app.delete('/:name/endpoints/:id', checkConnection, async (request, reply) => {
|
101 | const connection = request.getConnection()
|
102 | const { id, name } = request.params
|
103 |
|
104 | if (!connection.has(id)) return reply.sendError(404, `Connection ${name} doesn't have an endpoint ${id}!`)
|
105 |
|
106 | await connection.removeEndpoint(id).save()
|
107 |
|
108 | reply.send(connection.toObject().endpoints)
|
109 | })
|
110 | }
|