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

22 files changed

+384
-1
lines changed

package.json

+1-1
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

+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+
}

templates/fastify-mysql/.env.example

+6
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

+27
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+
}

templates/fastify-mysql/src/app.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Fastify from "fastify";
2+
import cors from "@fastify/cors";
3+
import routes from "./appModule.js";
4+
import pool 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+
try {
18+
await pool.query("SELECT 1");
19+
console.log("📦 Banco de dados conectado com sucesso!");
20+
} catch (error) {
21+
console.error("❌ Erro ao conectar no banco de dados:", error);
22+
process.exit(1);
23+
}
24+
}
25+
26+
private middlewares(): void {
27+
this.app.register(cors, {
28+
origin: "*",
29+
});
30+
}
31+
32+
private routes(): void {
33+
this.app.register(routes);
34+
}
35+
36+
public async start(): Promise<void> {
37+
try {
38+
await this.app.listen({ port: 3000, host: "0.0.0.0" });
39+
console.log(`🚀 Servidor rodando em http://localhost:3000`);
40+
} catch (err) {
41+
this.app.log.error(err);
42+
process.exit(1);
43+
}
44+
}
45+
}
46+
47+
const appController = new AppController();
48+
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,17 @@
1+
import mysql from 'mysql2/promise';
2+
import dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const pool = mysql.createPool({
7+
host: process.env.DB_HOST,
8+
user: process.env.DB_USER,
9+
password: process.env.DB_PASSWORD,
10+
database: process.env.DB_NAME,
11+
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306,
12+
waitForConnections: true,
13+
connectionLimit: 10,
14+
queueLimit: 0
15+
});
16+
17+
export default pool;

templates/fastify-mysql/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-mysql/tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "@tsconfig/node20/tsconfig.json",
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"module": "NodeNext",
6+
"moduleResolution": "NodeNext",
7+
"esModuleInterop": true,
8+
"strict": true,
9+
"skipLibCheck": true,
10+
"outDir": "./dist",
11+
"rootDir": "./src",
12+
"sourceMap": true
13+
},
14+
"include": ["src/**/*.ts"],
15+
"exclude": ["node_modules"]
16+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PGHOST=localhost
2+
PGPORT=5432
3+
PGUSER=postgres
4+
PGPASSWORD=
5+
PGDATABASE=
6+
PORT=3000
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
"knex": "^2.5.1",
16+
"pg": "^8.11.3",
17+
"tsx": "^4.19.3"
18+
},
19+
"devDependencies": {
20+
"@tsconfig/node20": "^20.1.4",
21+
"@types/cors": "^2.8.17",
22+
"@types/express": "^4.17.21",
23+
"@types/node": "^20.11.0",
24+
"nodemon": "^3.0.2",
25+
"ts-node": "^10.9.2",
26+
"typescript": "^5.3.3"
27+
}
28+
}

templates/fastify-postgres/src/app.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Fastify from "fastify";
2+
import cors from "@fastify/cors";
3+
import routes from "./appModule.js";
4+
import connection 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+
try {
18+
await connection.raw("SELECT 1");
19+
console.log("📦 Banco de dados conectado com sucesso!");
20+
} catch (error) {
21+
console.error("❌ Erro ao conectar no banco de dados:", error);
22+
process.exit(1);
23+
}
24+
}
25+
26+
private middlewares(): void {
27+
this.app.register(cors, {
28+
origin: "*",
29+
});
30+
}
31+
32+
private routes(): void {
33+
this.app.register(routes);
34+
}
35+
36+
public async start(): Promise<void> {
37+
try {
38+
await this.app.listen({ port: 3000, host: "0.0.0.0" });
39+
console.log(`🚀 Servidor rodando em http://localhost:3000`);
40+
} catch (err) {
41+
this.app.log.error(err);
42+
process.exit(1);
43+
}
44+
}
45+
}
46+
47+
const appController = new AppController();
48+
export default appController;
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+
}

0 commit comments

Comments
 (0)