Skip to content

Commit 7731ea0

Browse files
authored
Merge pull request #37 from ScaffoldAPI/dev
Feat: New templates using Fastify and bump to 1.1.5
2 parents 70a90a0 + 99af09e commit 7731ea0

File tree

22 files changed

+384
-1
lines changed

22 files changed

+384
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scaffold-api",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "Scaffold CLI",
55
"main": "build/index.js",
66
"type": "module",

templates/fastify-mongo/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MONGO_URI=

templates/fastify-mongo/package.json

Lines changed: 22 additions & 0 deletions
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

Lines changed: 42 additions & 0 deletions
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;
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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+
}

templates/fastify-mysql/.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DB_HOST=localhost
2+
DB_USER=root
3+
DB_PASSWORD=example
4+
DB_NAME=express_mysql
5+
DB_PORT=3306
6+
PORT=3000

templates/fastify-mysql/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "fastify-mysql-template",
3+
"version": "1.0.0",
4+
"main": "src/server.ts",
5+
"type": "module",
6+
"scripts": {
7+
"start": "node --no-warnings --loader ts-node/esm src/server.ts",
8+
"dev": "tsx watch src/server.ts"
9+
},
10+
"dependencies": {
11+
"@fastify/cors": "^11.0.0",
12+
"cors": "^2.8.5",
13+
"dotenv": "^16.3.1",
14+
"fastify": "^5.2.1",
15+
"mysql2": "^3.6.1",
16+
"tsx": "^4.19.3"
17+
},
18+
"devDependencies": {
19+
"@tsconfig/node20": "^20.1.4",
20+
"@types/cors": "^2.8.17",
21+
"@types/express": "^4.17.21",
22+
"@types/node": "^20.11.0",
23+
"nodemon": "^3.0.2",
24+
"ts-node": "^10.9.2",
25+
"typescript": "^5.3.3"
26+
}
27+
}

0 commit comments

Comments
 (0)