pwi-plata-type 0.4.165 → 0.4.166
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/__BUILD__/libs/sql.js +11 -4
- package/__BUILD__/libs/sql.js.map +1 -1
- package/package.json +53 -53
- package/src/@types/build.d.ts +26 -26
- package/src/@types/cluster.actions.d.ts +27 -27
- package/src/@types/cluster.d.ts +55 -55
- package/src/@types/create-cli.d.ts +8 -8
- package/src/@types/exported.d.ts +31 -31
- package/src/@types/fs.d.ts +6 -6
- package/src/@types/global.d.ts +113 -113
- package/src/@types/knex.d.ts +13 -13
- package/src/@types/model.d.ts +143 -143
- package/src/@types/router.d.ts +107 -107
- package/src/@types/sql.d.ts +75 -75
- package/src/@types/swagger.d.ts +22 -22
- package/src/bin/extras/create-api-cli.ts +218 -218
- package/src/bin/extras/create-cluster-cli.ts +160 -160
- package/src/bin/extras/create-lib-cli.ts +84 -84
- package/src/bin/extras/plata-create-tools.ts +97 -97
- package/src/bin/plata-api.ts +189 -189
- package/src/bin/plata-build.ts +17 -17
- package/src/bin/plata-cluster.ts +42 -42
- package/src/bin/plata-cmd.ts +18 -18
- package/src/bin/plata-create.ts +49 -49
- package/src/bin/plata-runtime.ts +131 -131
- package/src/bin/plata-swagger.ts +99 -99
- package/src/bin/runtime/_setupRuntime.ts +48 -48
- package/src/bin/runtime/_types.d.ts +7 -7
- package/src/bin/runtime/class.ts +20 -20
- package/src/bin/runtime/dirs.ts +25 -25
- package/src/bin/runtime/effect.ts +60 -60
- package/src/bin/runtime/fs.ts +10 -10
- package/src/bin/runtime/promises.ts +18 -18
- package/src/bin/runtime/require.ts +113 -113
- package/src/bin/runtime/sql.ts +12 -12
- package/src/bin/runtime/switch.ts +60 -60
- package/src/index.ts +19 -19
- package/src/libs/axios.ts +51 -51
- package/src/libs/build.ts +162 -162
- package/src/libs/cluster/cluster-controller.ts +93 -93
- package/src/libs/cluster.ts +330 -330
- package/src/libs/custom.ts +15 -15
- package/src/libs/env.ts +10 -10
- package/src/libs/fs.ts +399 -399
- package/src/libs/hot-reload.ts +63 -63
- package/src/libs/models/index.ts +4 -4
- package/src/libs/models/model-template-combine.ts +93 -93
- package/src/libs/models/model-template-env.ts +58 -58
- package/src/libs/models/model-template-union.ts +83 -83
- package/src/libs/models/model-template.ts +107 -107
- package/src/libs/models/model-validations.ts +443 -443
- package/src/libs/models/tools.ts +194 -194
- package/src/libs/router.ts +185 -185
- package/src/libs/sql.ts +788 -778
- package/src/libs/swagger.ts +81 -81
- package/src/libs/tools.ts +240 -240
- package/src/scripts/post-install/api.ts +32 -32
- package/src/scripts/post-install/cluster.ts +28 -28
- package/src/scripts/post-install/lib.ts +25 -25
- package/src/scripts/post-install.ts +64 -64
- package/src/scripts/pre-test.ts +33 -33
- package/src/tests/assets/sql/example.sql +76 -76
- package/src/tests/config.test.ts +12 -12
- package/src/tests/effect.test.ts +62 -62
- package/src/tests/model.env.test.ts +66 -66
- package/src/tests/model.test.ts +141 -141
- package/src/tests/model.union.test.ts +160 -160
- package/src/tests/router.test.ts +44 -44
- package/src/tests/sql.test.ts +502 -502
- package/src/tests/switch.test.ts +76 -76
- package/templates/create-cli/class.ts +13 -13
- package/templates/create-cli/cluster.ts +14 -14
- package/templates/create-cli/cmd.ts +3 -3
- package/templates/create-cli/config.ts +2 -2
- package/templates/create-cli/lib.ts +3 -3
- package/templates/create-cli/model.ts +14 -14
- package/templates/create-cli/route.ts +8 -8
- package/templates/create-cli/task.ts +13 -13
- package/templates/postinstall/api/.vscode/launch.json +20 -20
- package/templates/postinstall/api/Dockerfile +24 -24
- package/templates/postinstall/api/configs/env.ts +2 -2
- package/templates/postinstall/api/envs/debug.env +15 -15
- package/templates/postinstall/api/envs/homolog.env +15 -15
- package/templates/postinstall/api/envs/prod.env +15 -15
- package/templates/postinstall/api/tsconfig.json +24 -24
- package/templates/postinstall/cluster/.vscode/launch.json +20 -20
- package/templates/postinstall/cluster/Dockerfile +24 -24
- package/templates/postinstall/cluster/configs/env.ts +2 -2
- package/templates/postinstall/cluster/envs/debug.env +10 -10
- package/templates/postinstall/cluster/envs/homolog.env +10 -10
- package/templates/postinstall/cluster/envs/prod.env +10 -10
- package/templates/postinstall/cluster/tsconfig.json +24 -24
- package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
- package/templates/postinstall/lib/postinstall.js +33 -33
- package/templates/postinstall/lib/tsconfig.json +22 -22
- package/templates/swagger-cli/header.json +21 -21
- package/templates/swagger-cli/route/body.json +14 -14
- package/templates/swagger-cli/route/header.json +18 -18
- package/templates/swagger-cli/route/response.json +53 -53
- package/tsconfig.json +28 -28
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
import fs from 'node:fs'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
import { PlataDirs, PlataFiles } from '../../libs/tools'
|
|
5
|
-
|
|
6
|
-
export namespace tools {
|
|
7
|
-
export namespace files {
|
|
8
|
-
export async function mkdirIfNotExists(dir: string) {
|
|
9
|
-
try {
|
|
10
|
-
const p = path.resolve(dir)
|
|
11
|
-
|
|
12
|
-
if (!fs.existsSync(p)) {
|
|
13
|
-
fs.mkdirSync(p, { recursive: true })
|
|
14
|
-
}
|
|
15
|
-
} catch (err) {
|
|
16
|
-
console.error(err)
|
|
17
|
-
process.exit(1)
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export async function copyFileIfNotExists(source: string, destination: string) {
|
|
22
|
-
try {
|
|
23
|
-
const s = path.resolve(source)
|
|
24
|
-
const d = path.resolve(destination)
|
|
25
|
-
|
|
26
|
-
if (!fs.existsSync(d)) {
|
|
27
|
-
fs.copyFileSync(s, d)
|
|
28
|
-
}
|
|
29
|
-
} catch (err) {
|
|
30
|
-
console.error(err)
|
|
31
|
-
process.exit(1)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function copyFileToProject(projectFolder: string, templateFile: string, fileName: string): Promise<string> {
|
|
36
|
-
const s = path.resolve(PlataDirs.getPlataTempleteDir('create-cli', `${templateFile}.ts`))
|
|
37
|
-
let d = path.resolve(Plata.ProjectDir, projectFolder)
|
|
38
|
-
|
|
39
|
-
await mkdirIfNotExists(d)
|
|
40
|
-
d = path.join(d, fileName)
|
|
41
|
-
await copyFileIfNotExists(s, d)
|
|
42
|
-
|
|
43
|
-
return d
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export async function deployFileToProject(projectFolder: string, templateFile: string, fileName: string, args: any) {
|
|
47
|
-
const fileInProject = await copyFileToProject(projectFolder, templateFile, fileName)
|
|
48
|
-
|
|
49
|
-
const content: any = await PlataFiles.readFileSync(fileInProject, (line) => {
|
|
50
|
-
if (line === '//@ts-nocheck') {
|
|
51
|
-
return ''
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (line === '') {
|
|
55
|
-
return ''
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Procura por texto para subistituir esse texto que esta no formato ç__PARAMETRO__ç
|
|
59
|
-
let lastIndex = 0
|
|
60
|
-
while(lastIndex !== -1) {
|
|
61
|
-
lastIndex = line.indexOf('$__', lastIndex)
|
|
62
|
-
|
|
63
|
-
if (lastIndex !== -1) {
|
|
64
|
-
const pIndex = line.indexOf('__$', lastIndex)
|
|
65
|
-
const param = line.slice(lastIndex + 3, pIndex)
|
|
66
|
-
|
|
67
|
-
line = line.replace(new RegExp(`\\$__${param}__\\$`, 'g'), args[param])
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return line
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
if (content.err !== undefined) {
|
|
75
|
-
console.log(content.err)
|
|
76
|
-
process.exit(1)
|
|
77
|
-
} else {
|
|
78
|
-
fs.writeFileSync(fileInProject, content)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export namespace args {
|
|
84
|
-
export type argsCallBack = (command: string, args: string[]) => Promise<string[]>
|
|
85
|
-
|
|
86
|
-
export async function forEachArg(callback: argsCallBack) {
|
|
87
|
-
let args = process.argv.slice(process.argv.indexOf('--') + 1)
|
|
88
|
-
|
|
89
|
-
while(args.length !== 0) {
|
|
90
|
-
const command = args.shift()
|
|
91
|
-
|
|
92
|
-
if (command !== undefined) {
|
|
93
|
-
args = await callback(command, args)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { PlataDirs, PlataFiles } from '../../libs/tools'
|
|
5
|
+
|
|
6
|
+
export namespace tools {
|
|
7
|
+
export namespace files {
|
|
8
|
+
export async function mkdirIfNotExists(dir: string) {
|
|
9
|
+
try {
|
|
10
|
+
const p = path.resolve(dir)
|
|
11
|
+
|
|
12
|
+
if (!fs.existsSync(p)) {
|
|
13
|
+
fs.mkdirSync(p, { recursive: true })
|
|
14
|
+
}
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error(err)
|
|
17
|
+
process.exit(1)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function copyFileIfNotExists(source: string, destination: string) {
|
|
22
|
+
try {
|
|
23
|
+
const s = path.resolve(source)
|
|
24
|
+
const d = path.resolve(destination)
|
|
25
|
+
|
|
26
|
+
if (!fs.existsSync(d)) {
|
|
27
|
+
fs.copyFileSync(s, d)
|
|
28
|
+
}
|
|
29
|
+
} catch (err) {
|
|
30
|
+
console.error(err)
|
|
31
|
+
process.exit(1)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function copyFileToProject(projectFolder: string, templateFile: string, fileName: string): Promise<string> {
|
|
36
|
+
const s = path.resolve(PlataDirs.getPlataTempleteDir('create-cli', `${templateFile}.ts`))
|
|
37
|
+
let d = path.resolve(Plata.ProjectDir, projectFolder)
|
|
38
|
+
|
|
39
|
+
await mkdirIfNotExists(d)
|
|
40
|
+
d = path.join(d, fileName)
|
|
41
|
+
await copyFileIfNotExists(s, d)
|
|
42
|
+
|
|
43
|
+
return d
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function deployFileToProject(projectFolder: string, templateFile: string, fileName: string, args: any) {
|
|
47
|
+
const fileInProject = await copyFileToProject(projectFolder, templateFile, fileName)
|
|
48
|
+
|
|
49
|
+
const content: any = await PlataFiles.readFileSync(fileInProject, (line) => {
|
|
50
|
+
if (line === '//@ts-nocheck') {
|
|
51
|
+
return ''
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (line === '') {
|
|
55
|
+
return ''
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Procura por texto para subistituir esse texto que esta no formato ç__PARAMETRO__ç
|
|
59
|
+
let lastIndex = 0
|
|
60
|
+
while(lastIndex !== -1) {
|
|
61
|
+
lastIndex = line.indexOf('$__', lastIndex)
|
|
62
|
+
|
|
63
|
+
if (lastIndex !== -1) {
|
|
64
|
+
const pIndex = line.indexOf('__$', lastIndex)
|
|
65
|
+
const param = line.slice(lastIndex + 3, pIndex)
|
|
66
|
+
|
|
67
|
+
line = line.replace(new RegExp(`\\$__${param}__\\$`, 'g'), args[param])
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return line
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
if (content.err !== undefined) {
|
|
75
|
+
console.log(content.err)
|
|
76
|
+
process.exit(1)
|
|
77
|
+
} else {
|
|
78
|
+
fs.writeFileSync(fileInProject, content)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export namespace args {
|
|
84
|
+
export type argsCallBack = (command: string, args: string[]) => Promise<string[]>
|
|
85
|
+
|
|
86
|
+
export async function forEachArg(callback: argsCallBack) {
|
|
87
|
+
let args = process.argv.slice(process.argv.indexOf('--') + 1)
|
|
88
|
+
|
|
89
|
+
while(args.length !== 0) {
|
|
90
|
+
const command = args.shift()
|
|
91
|
+
|
|
92
|
+
if (command !== undefined) {
|
|
93
|
+
args = await callback(command, args)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
98
|
}
|
package/src/bin/plata-api.ts
CHANGED
|
@@ -1,190 +1,190 @@
|
|
|
1
|
-
import express from 'express'
|
|
2
|
-
import { PlataClusterManager } from '++/libs/cluster'
|
|
3
|
-
import { createServer } from 'node:https'
|
|
4
|
-
import fs from 'node:fs/promises'
|
|
5
|
-
import path from 'node:path'
|
|
6
|
-
import { buildExpressRouter, loadRoutesFromProject } from '++/libs/router'
|
|
7
|
-
import { getCustomExpress } from '++/libs/custom'
|
|
8
|
-
import { HotReload } from '++/libs/hot-reload'
|
|
9
|
-
|
|
10
|
-
export const main = async () => {
|
|
11
|
-
const cluster = new PlataClusterManager()
|
|
12
|
-
|
|
13
|
-
if (cluster.isPrimary()) {
|
|
14
|
-
|
|
15
|
-
let workers: number | undefined = undefined
|
|
16
|
-
|
|
17
|
-
if (Plata.config._PLATA_API_WORKERS !== undefined) {
|
|
18
|
-
workers = +Plata.config._PLATA_API_WORKERS || undefined
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const clustersPromise: RequireFolderReturn<{ default: Cluster.WorkerConfig }> = require("@@/clusters/*.*")
|
|
22
|
-
|
|
23
|
-
if (process.argv.includes('--hot'))
|
|
24
|
-
Plata.FastPromise(() => HotReload.setupHotReloadPrimary())
|
|
25
|
-
;
|
|
26
|
-
|
|
27
|
-
const clusters = await clustersPromise
|
|
28
|
-
|
|
29
|
-
if (clusters.errorID !== undefined) {
|
|
30
|
-
console.log(clusters)
|
|
31
|
-
process.exit(0)
|
|
32
|
-
}
|
|
33
|
-
// start Clusters
|
|
34
|
-
{
|
|
35
|
-
const promises: any = []
|
|
36
|
-
|
|
37
|
-
clusters.forEach(c => {
|
|
38
|
-
const config = c.exports.default
|
|
39
|
-
promises.push(cluster._startCluster(config).then(r => {
|
|
40
|
-
if (r !== null) {
|
|
41
|
-
console.log(r)
|
|
42
|
-
process.exit(0)
|
|
43
|
-
}
|
|
44
|
-
}))
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
await Promise.all(promises)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Inicia a API
|
|
51
|
-
await cluster._startCluster({
|
|
52
|
-
name: '__PLATA_EXPRESS__',
|
|
53
|
-
env: Plata.config.ENV ?? 'prod',
|
|
54
|
-
onStart: async () => {},
|
|
55
|
-
workers: workers,
|
|
56
|
-
})
|
|
57
|
-
} else {
|
|
58
|
-
if (Plata.config._PLATA_WORKER_NAME === '__PLATA_EXPRESS__') {
|
|
59
|
-
if (process.argv.includes('--hot'))
|
|
60
|
-
Plata.FastPromise(() => HotReload.setupHotReloadWorker())
|
|
61
|
-
;
|
|
62
|
-
|
|
63
|
-
const projectRoutesPromise = Plata.FastPromise(() => loadRoutesFromProject())
|
|
64
|
-
const app = await getCustomExpress()
|
|
65
|
-
|
|
66
|
-
app.disable('x-powered-by')
|
|
67
|
-
|
|
68
|
-
const apiRoots = Plata.config._PLATA_API_ROOT ?? '/api'
|
|
69
|
-
|
|
70
|
-
app.use(express.json({ limit: Plata.config._PLATA_API_JSON_SIZE ?? '100kb' }))
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const requiredRoutes = await projectRoutesPromise
|
|
74
|
-
|
|
75
|
-
if (requiredRoutes.errorID !== undefined) {
|
|
76
|
-
console.log(requiredRoutes)
|
|
77
|
-
|
|
78
|
-
await cluster.send({
|
|
79
|
-
action: 'KILL',
|
|
80
|
-
msg: 'Erro enquanto carregava as rotas'
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
{
|
|
87
|
-
const result = await buildExpressRouter(requiredRoutes)
|
|
88
|
-
|
|
89
|
-
if (result.errorID !== undefined) {
|
|
90
|
-
console.log(result)
|
|
91
|
-
|
|
92
|
-
await cluster.send({
|
|
93
|
-
action: 'KILL',
|
|
94
|
-
msg: 'Erro enquanto carregava as rotas'
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
return
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
app.use(apiRoots.split(','), result as any)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
app.use((err, _req, res, _next) => {
|
|
104
|
-
return res.status(500).json({
|
|
105
|
-
errorID: 'PRAPI0001',
|
|
106
|
-
error: 'Erro interno inesperado',
|
|
107
|
-
msg: {
|
|
108
|
-
message: err.message,
|
|
109
|
-
stack: err.stack ?? new Error().stack
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
app.all('*', (req, res) => {
|
|
115
|
-
return res.status(404).json({
|
|
116
|
-
errorID: 'PRAPI0002',
|
|
117
|
-
msg: 'Rota não encontrada',
|
|
118
|
-
error: req.path
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
const port = process.env.PORT ?? 3050
|
|
123
|
-
|
|
124
|
-
if (!Plata.config._PLATA_API_SSL_KEY || !Plata.config._PLATA_API_SSL_CERT) {
|
|
125
|
-
|
|
126
|
-
console.log(`Nenhum certificado configurado na env ${process.env.ENV}:`)
|
|
127
|
-
console.log(`_PLATA_API_SSL_KEY: ${process.env._PLATA_API_SSL_KEY}`)
|
|
128
|
-
console.log(`_PLATA_API_SSL_CERT: ${process.env._PLATA_API_SSL_CERT}`)
|
|
129
|
-
|
|
130
|
-
app.listen(port, () => {
|
|
131
|
-
console.log(`Iniciando Worker Express na porta ${port}`)
|
|
132
|
-
setTimeout(() => process.removeAllListeners('exit'), 500)
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
} else {
|
|
136
|
-
|
|
137
|
-
console.log('Iniciando a Plata com ssl usando os arquivos:')
|
|
138
|
-
console.log(`key: ${process.env._PLATA_API_SSL_KEY}`)
|
|
139
|
-
console.log(`cert: ${process.env._PLATA_API_SSL_CERT}`)
|
|
140
|
-
|
|
141
|
-
const [ cert, key ] = await Promise.all([
|
|
142
|
-
fs.readFile(path.normalize(path.join(
|
|
143
|
-
Plata.ProjectDir,
|
|
144
|
-
Plata.config._PLATA_API_SSL_CERT
|
|
145
|
-
))),
|
|
146
|
-
fs.readFile(path.normalize(path.join(
|
|
147
|
-
Plata.ProjectDir,
|
|
148
|
-
Plata.config._PLATA_API_SSL_KEY
|
|
149
|
-
)))
|
|
150
|
-
]).catch(err => {
|
|
151
|
-
console.log({
|
|
152
|
-
errorID: 'PRAPI0003',
|
|
153
|
-
msg: 'Erro ao ler o certifica e chave ssl',
|
|
154
|
-
error: {
|
|
155
|
-
message: err?.toString(),
|
|
156
|
-
stack: err?.stack
|
|
157
|
-
}
|
|
158
|
-
} satisfies PlataError)
|
|
159
|
-
|
|
160
|
-
process.exit(1)
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
const https = createServer({
|
|
164
|
-
cert,
|
|
165
|
-
key,
|
|
166
|
-
passphrase: process.env._PLATA_API_SSL_PASS
|
|
167
|
-
}, app)
|
|
168
|
-
|
|
169
|
-
https.listen(port, () => {
|
|
170
|
-
console.log(`Iniciando Worker Express na porta ${port} com SSL`)
|
|
171
|
-
setTimeout(() => process.removeAllListeners('exit'), 500)
|
|
172
|
-
})
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
if (Plata.config._PLATA_WORKER_NAME === undefined) {
|
|
176
|
-
await cluster.send({
|
|
177
|
-
action: 'KILL',
|
|
178
|
-
msg: 'tried to start a Worker without _PLATA_WORKER_NAME'
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
return
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
await cluster._onStartCluster(
|
|
185
|
-
Plata.config._PLATA_WORKER_NAME,
|
|
186
|
-
Plata.config._PLATA_WORKER_TYPE,
|
|
187
|
-
)
|
|
188
|
-
}
|
|
189
|
-
}
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import { PlataClusterManager } from '++/libs/cluster'
|
|
3
|
+
import { createServer } from 'node:https'
|
|
4
|
+
import fs from 'node:fs/promises'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { buildExpressRouter, loadRoutesFromProject } from '++/libs/router'
|
|
7
|
+
import { getCustomExpress } from '++/libs/custom'
|
|
8
|
+
import { HotReload } from '++/libs/hot-reload'
|
|
9
|
+
|
|
10
|
+
export const main = async () => {
|
|
11
|
+
const cluster = new PlataClusterManager()
|
|
12
|
+
|
|
13
|
+
if (cluster.isPrimary()) {
|
|
14
|
+
|
|
15
|
+
let workers: number | undefined = undefined
|
|
16
|
+
|
|
17
|
+
if (Plata.config._PLATA_API_WORKERS !== undefined) {
|
|
18
|
+
workers = +Plata.config._PLATA_API_WORKERS || undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const clustersPromise: RequireFolderReturn<{ default: Cluster.WorkerConfig }> = require("@@/clusters/*.*")
|
|
22
|
+
|
|
23
|
+
if (process.argv.includes('--hot'))
|
|
24
|
+
Plata.FastPromise(() => HotReload.setupHotReloadPrimary())
|
|
25
|
+
;
|
|
26
|
+
|
|
27
|
+
const clusters = await clustersPromise
|
|
28
|
+
|
|
29
|
+
if (clusters.errorID !== undefined) {
|
|
30
|
+
console.log(clusters)
|
|
31
|
+
process.exit(0)
|
|
32
|
+
}
|
|
33
|
+
// start Clusters
|
|
34
|
+
{
|
|
35
|
+
const promises: any = []
|
|
36
|
+
|
|
37
|
+
clusters.forEach(c => {
|
|
38
|
+
const config = c.exports.default
|
|
39
|
+
promises.push(cluster._startCluster(config).then(r => {
|
|
40
|
+
if (r !== null) {
|
|
41
|
+
console.log(r)
|
|
42
|
+
process.exit(0)
|
|
43
|
+
}
|
|
44
|
+
}))
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
await Promise.all(promises)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Inicia a API
|
|
51
|
+
await cluster._startCluster({
|
|
52
|
+
name: '__PLATA_EXPRESS__',
|
|
53
|
+
env: Plata.config.ENV ?? 'prod',
|
|
54
|
+
onStart: async () => {},
|
|
55
|
+
workers: workers,
|
|
56
|
+
})
|
|
57
|
+
} else {
|
|
58
|
+
if (Plata.config._PLATA_WORKER_NAME === '__PLATA_EXPRESS__') {
|
|
59
|
+
if (process.argv.includes('--hot'))
|
|
60
|
+
Plata.FastPromise(() => HotReload.setupHotReloadWorker())
|
|
61
|
+
;
|
|
62
|
+
|
|
63
|
+
const projectRoutesPromise = Plata.FastPromise(() => loadRoutesFromProject())
|
|
64
|
+
const app = await getCustomExpress()
|
|
65
|
+
|
|
66
|
+
app.disable('x-powered-by')
|
|
67
|
+
|
|
68
|
+
const apiRoots = Plata.config._PLATA_API_ROOT ?? '/api'
|
|
69
|
+
|
|
70
|
+
app.use(express.json({ limit: Plata.config._PLATA_API_JSON_SIZE ?? '100kb' }))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
const requiredRoutes = await projectRoutesPromise
|
|
74
|
+
|
|
75
|
+
if (requiredRoutes.errorID !== undefined) {
|
|
76
|
+
console.log(requiredRoutes)
|
|
77
|
+
|
|
78
|
+
await cluster.send({
|
|
79
|
+
action: 'KILL',
|
|
80
|
+
msg: 'Erro enquanto carregava as rotas'
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
{
|
|
87
|
+
const result = await buildExpressRouter(requiredRoutes)
|
|
88
|
+
|
|
89
|
+
if (result.errorID !== undefined) {
|
|
90
|
+
console.log(result)
|
|
91
|
+
|
|
92
|
+
await cluster.send({
|
|
93
|
+
action: 'KILL',
|
|
94
|
+
msg: 'Erro enquanto carregava as rotas'
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
app.use(apiRoots.split(','), result as any)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
app.use((err, _req, res, _next) => {
|
|
104
|
+
return res.status(500).json({
|
|
105
|
+
errorID: 'PRAPI0001',
|
|
106
|
+
error: 'Erro interno inesperado',
|
|
107
|
+
msg: {
|
|
108
|
+
message: err.message,
|
|
109
|
+
stack: err.stack ?? new Error().stack
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
app.all('*', (req, res) => {
|
|
115
|
+
return res.status(404).json({
|
|
116
|
+
errorID: 'PRAPI0002',
|
|
117
|
+
msg: 'Rota não encontrada',
|
|
118
|
+
error: req.path
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
const port = process.env.PORT ?? 3050
|
|
123
|
+
|
|
124
|
+
if (!Plata.config._PLATA_API_SSL_KEY || !Plata.config._PLATA_API_SSL_CERT) {
|
|
125
|
+
|
|
126
|
+
console.log(`Nenhum certificado configurado na env ${process.env.ENV}:`)
|
|
127
|
+
console.log(`_PLATA_API_SSL_KEY: ${process.env._PLATA_API_SSL_KEY}`)
|
|
128
|
+
console.log(`_PLATA_API_SSL_CERT: ${process.env._PLATA_API_SSL_CERT}`)
|
|
129
|
+
|
|
130
|
+
app.listen(port, () => {
|
|
131
|
+
console.log(`Iniciando Worker Express na porta ${port}`)
|
|
132
|
+
setTimeout(() => process.removeAllListeners('exit'), 500)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
} else {
|
|
136
|
+
|
|
137
|
+
console.log('Iniciando a Plata com ssl usando os arquivos:')
|
|
138
|
+
console.log(`key: ${process.env._PLATA_API_SSL_KEY}`)
|
|
139
|
+
console.log(`cert: ${process.env._PLATA_API_SSL_CERT}`)
|
|
140
|
+
|
|
141
|
+
const [ cert, key ] = await Promise.all([
|
|
142
|
+
fs.readFile(path.normalize(path.join(
|
|
143
|
+
Plata.ProjectDir,
|
|
144
|
+
Plata.config._PLATA_API_SSL_CERT
|
|
145
|
+
))),
|
|
146
|
+
fs.readFile(path.normalize(path.join(
|
|
147
|
+
Plata.ProjectDir,
|
|
148
|
+
Plata.config._PLATA_API_SSL_KEY
|
|
149
|
+
)))
|
|
150
|
+
]).catch(err => {
|
|
151
|
+
console.log({
|
|
152
|
+
errorID: 'PRAPI0003',
|
|
153
|
+
msg: 'Erro ao ler o certifica e chave ssl',
|
|
154
|
+
error: {
|
|
155
|
+
message: err?.toString(),
|
|
156
|
+
stack: err?.stack
|
|
157
|
+
}
|
|
158
|
+
} satisfies PlataError)
|
|
159
|
+
|
|
160
|
+
process.exit(1)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
const https = createServer({
|
|
164
|
+
cert,
|
|
165
|
+
key,
|
|
166
|
+
passphrase: process.env._PLATA_API_SSL_PASS
|
|
167
|
+
}, app)
|
|
168
|
+
|
|
169
|
+
https.listen(port, () => {
|
|
170
|
+
console.log(`Iniciando Worker Express na porta ${port} com SSL`)
|
|
171
|
+
setTimeout(() => process.removeAllListeners('exit'), 500)
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
if (Plata.config._PLATA_WORKER_NAME === undefined) {
|
|
176
|
+
await cluster.send({
|
|
177
|
+
action: 'KILL',
|
|
178
|
+
msg: 'tried to start a Worker without _PLATA_WORKER_NAME'
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
await cluster._onStartCluster(
|
|
185
|
+
Plata.config._PLATA_WORKER_NAME,
|
|
186
|
+
Plata.config._PLATA_WORKER_TYPE,
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
190
|
}
|
package/src/bin/plata-build.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { PlataBuild } from "++/libs/build"
|
|
2
|
-
import path from 'path'
|
|
3
|
-
|
|
4
|
-
export const main = async () => {
|
|
5
|
-
const config = await PlataBuild.getBuildConfig()
|
|
6
|
-
|
|
7
|
-
if (config.errorID !== undefined) {
|
|
8
|
-
console.error(config)
|
|
9
|
-
process.exit(1)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
await PlataBuild.compile({
|
|
13
|
-
...config,
|
|
14
|
-
basePath: path.join(Plata.ProjectDir, '__RELEASE__'),
|
|
15
|
-
mapFiles: true,
|
|
16
|
-
packageJsonSetup: true,
|
|
17
|
-
})
|
|
1
|
+
import { PlataBuild } from "++/libs/build"
|
|
2
|
+
import path from 'path'
|
|
3
|
+
|
|
4
|
+
export const main = async () => {
|
|
5
|
+
const config = await PlataBuild.getBuildConfig()
|
|
6
|
+
|
|
7
|
+
if (config.errorID !== undefined) {
|
|
8
|
+
console.error(config)
|
|
9
|
+
process.exit(1)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
await PlataBuild.compile({
|
|
13
|
+
...config,
|
|
14
|
+
basePath: path.join(Plata.ProjectDir, '__RELEASE__'),
|
|
15
|
+
mapFiles: true,
|
|
16
|
+
packageJsonSetup: true,
|
|
17
|
+
})
|
|
18
18
|
}
|