Skip to content

Commit a36fd0f

Browse files
committed
v0.0.2
1 parent a950582 commit a36fd0f

File tree

4 files changed

+7249
-4
lines changed

4 files changed

+7249
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
2+
import { routes } from './routes';
3+
4+
const FASTIFY_REST_METHODS = {
5+
GET: 'GET',
6+
POST: 'POS',
7+
PUT: 'PUT',
8+
DELETE: 'DELETE',
9+
};
10+
11+
const router = async (fastify: FastifyInstance) => {
12+
for (const route of routes) {
13+
const { method, url, controller } = route;
14+
switch (method) {
15+
case FASTIFY_REST_METHODS.GET:
16+
fastify.get(url, {}, async (req: FastifyRequest, reply: FastifyReply) => {
17+
return controller.execute(req, reply);
18+
});
19+
break;
20+
case FASTIFY_REST_METHODS.POST:
21+
fastify.post(url, {}, async (req: FastifyRequest, reply: FastifyReply) => {
22+
return controller.execute(req, reply);
23+
});
24+
break;
25+
case FASTIFY_REST_METHODS.PUT:
26+
fastify.put(url, {}, async (req: FastifyRequest, reply: FastifyReply) => {
27+
return controller.execute(req, reply);
28+
});
29+
break;
30+
case FASTIFY_REST_METHODS.DELETE:
31+
fastify.delete(url, {}, async (req: FastifyRequest, reply: FastifyReply) => {
32+
return controller.execute(req, reply);
33+
});
34+
break;
35+
default:
36+
throw new Error(`Unknown method ${method}`);
37+
}
38+
}
39+
};
40+
41+
export default router;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Bitloops Language
3+
* Copyright (C) 2022 Bitloops S.A.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* For further information you can contact legal(at)bitloops.com.
19+
*/
20+
import { FastifyInstance } from 'fastify';
21+
// @TEMPLATE - Add imports here
22+
const router = async (fastify: FastifyInstance, _opts: any) => {
23+
// @TEMPLATE - Add routes here
24+
};
25+
26+
export { router };

src/shared/infra/rest/fastify/app.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ import formBodyPlugin from '@fastify/formbody';
2222
import fastifyCors from '@fastify/cors';
2323
import { router } from './api';
2424

25+
// TODO manage cors options
2526
const corsOptions = {
2627
// origin: isProduction ? 'https://yourwhitelisteddomain.com' : '*',
2728
origin: '*',
2829
};
2930

30-
// TODO Cors config
3131
const fastify = Fastify({
3232
logger: true,
3333
});
3434
fastify.register(fastifyCors, corsOptions);
3535
fastify.register(formBodyPlugin);
36-
fastify.register(fastifyCors);
3736
fastify.register(router, {
3837
prefix: process.env.API_PREFIX,
3938
});
4039

41-
const port = process.env.FASTIFY_PORT || 5002; // @TEMPLATE
40+
// @TEMPLATE `const port = ${PORT};`
41+
const port = process.env.FASTIFY_PORT || 5001;
4242

4343
const start = async () => {
4444
try {
45-
await fastify.listen({ port: +port }); // @TEMPLATE
45+
await fastify.listen({ port: +port });
4646
} catch (err) {
4747
fastify.log.error(err);
4848
process.exit(1);

0 commit comments

Comments
 (0)