Skip to content

Commit d9dd689

Browse files
committed
feat: Implement Fastify application with MongoDB connection, routing, and environment configuration. Add .env.example for environment variables. Closes #36
1 parent ff9cc84 commit d9dd689

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

templates/fastify-mongo/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MONGO_URI=

templates/fastify-mongo/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "express-mongo-template",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "tsx watch src/server.ts"
7+
},
8+
"dependencies": {
9+
"fastify": "^5.2.1",
10+
"@fastify/cors": "^11.0.0",
11+
"cors": "^2.8.5",
12+
"dotenv": "^16.3.1",
13+
"mongoose": "^8.0.3",
14+
"tsx": "^4.19.3"
15+
},
16+
"devDependencies": {
17+
"@types/express": "^4.17.21",
18+
"@types/node": "^20.11.19",
19+
"ts-node": "^10.9.2",
20+
"typescript": "^5.3.3"
21+
}
22+
}

templates/fastify-mongo/src/app.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Fastify from "fastify";
2+
import cors from "@fastify/cors";
3+
import routes from "./appModule.js";
4+
import connectDB from "./config/database.js";
5+
6+
class AppController {
7+
public app: Fastify.FastifyInstance;
8+
9+
constructor() {
10+
this.app = Fastify({ logger: true });
11+
this.database();
12+
this.middlewares();
13+
this.routes();
14+
}
15+
16+
private async database(): Promise<void> {
17+
await connectDB();
18+
}
19+
20+
private middlewares(): void {
21+
this.app.register(cors, {
22+
origin: "*",
23+
});
24+
}
25+
26+
private routes(): void {
27+
this.app.register(routes);
28+
}
29+
30+
public async start(): Promise<void> {
31+
try {
32+
await this.app.listen({ port: 3000, host: "0.0.0.0" });
33+
console.log(`🚀 Servidor rodando em http://localhost:3000`);
34+
} catch (err) {
35+
this.app.log.error(err);
36+
process.exit(1);
37+
}
38+
}
39+
}
40+
41+
const appController = new AppController();
42+
export default appController;
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { FastifyInstance } from "fastify";
2+
3+
export default async function routes(fastify: FastifyInstance) {
4+
fastify.get("/test", async (_request, reply) => {
5+
reply.send({ message: "API funcionando!" });
6+
});
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import mongoose from "mongoose";
2+
import dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
const MONGO_URI = process.env.MONGO_URI as string;
7+
8+
const connectDB = async () => {
9+
try {
10+
await mongoose.connect(MONGO_URI);
11+
console.log("Conectado ao MongoDB!");
12+
} catch (error) {
13+
console.error("Erro ao conectar ao MongoDB:", error);
14+
process.exit(1);
15+
}
16+
};
17+
18+
export default connectDB;

templates/fastify-mongo/src/server.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import appController from "./app.js";
2+
import { config } from "dotenv";
3+
4+
config();
5+
6+
const PORT = process.env.PORT || 3000;
7+
8+
9+
appController.start();

templates/fastify-mongo/tsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"resolveJsonModule": true,
7+
"strict": true,
8+
"esModuleInterop": true,
9+
"skipLibCheck": true,
10+
"rootDir": "src",
11+
"outDir": "build",
12+
"emitDeclarationOnly": false,
13+
"noUnusedLocals": true,
14+
"noUnusedParameters": true,
15+
"allowImportingTsExtensions": true
16+
},
17+
"include": ["src"],
18+
"exclude": ["node_modules", "build"]
19+
}

0 commit comments

Comments
 (0)