FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
evolution-api/src/main.ts at main · alvarezfausto/evolution-api · GitHub
alvarezfausto
/
evolution-api
Public
forked from
evolution-foundation/evolution-api
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
evolution-api
/
src
/
main.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
151 lines (124 loc) · 4.6 KB
Breadcrumbs
evolution-api
/
src
/
main.ts
Copy path
File metadata and controls
151 lines (124 loc) · 4.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import
{
ProviderFiles
}
from
'@api/provider/sessions'
;
import
{
PrismaRepository
}
from
'@api/repository/repository.service'
;
import
{
HttpStatus
,
router
}
from
'@api/routes/index.router'
;
import
{
eventManager
,
waMonitor
}
from
'@api/server.module'
;
import
{
Auth
,
configService
,
Cors
,
HttpServer
,
ProviderSession
,
Webhook
}
from
'@config/env.config'
;
import
{
onUnexpectedError
}
from
'@config/error.config'
;
import
{
Logger
}
from
'@config/logger.config'
;
import
{
ROOT_DIR
}
from
'@config/path.config'
;
import
*
as
Sentry
from
'@sentry/node'
;
import
{
ServerUP
}
from
'@utils/server-up'
;
import
axios
from
'axios'
;
import
compression
from
'compression'
;
import
cors
from
'cors'
;
import
express
,
{
json
,
NextFunction
,
Request
,
Response
,
urlencoded
}
from
'express'
;
import
{
join
}
from
'path'
;
function
initWA
(
)
{
waMonitor
.
loadInstance
(
)
;
}
async
function
bootstrap
(
)
{
const
logger
=
new
Logger
(
'SERVER'
)
;
const
app
=
express
(
)
;
const
dsn
=
process
.
env
.
SENTRY_DSN
;
if
(
dsn
)
{
logger
.
info
(
'Sentry - ON'
)
;
Sentry
.
init
(
{
dsn
:
dsn
,
environment
:
process
.
env
.
NODE_ENV
||
'development'
,
tracesSampleRate
:
1.0
,
profilesSampleRate
:
1.0
,
}
)
;
Sentry
.
setupExpressErrorHandler
(
app
)
;
}
let
providerFiles
:
ProviderFiles
=
null
;
if
(
configService
.
get
<
ProviderSession
>
(
'PROVIDER'
)
.
ENABLED
)
{
providerFiles
=
new
ProviderFiles
(
configService
)
;
await
providerFiles
.
onModuleInit
(
)
;
logger
.
info
(
'Provider:Files - ON'
)
;
}
const
prismaRepository
=
new
PrismaRepository
(
configService
)
;
await
prismaRepository
.
onModuleInit
(
)
;
app
.
use
(
cors
(
{
origin
(
requestOrigin
,
callback
)
{
const
{
ORIGIN
}
=
configService
.
get
<
Cors
>
(
'CORS'
)
;
if
(
ORIGIN
.
includes
(
'*'
)
)
{
return
callback
(
null
,
true
)
;
}
if
(
ORIGIN
.
indexOf
(
requestOrigin
)
!==
-
1
)
{
return
callback
(
null
,
true
)
;
}
return
callback
(
new
Error
(
'Not allowed by CORS'
)
)
;
}
,
methods
:
[
...
configService
.
get
<
Cors
>
(
'CORS'
)
.
METHODS
]
,
credentials
:
configService
.
get
<
Cors
>
(
'CORS'
)
.
CREDENTIALS
,
}
)
,
urlencoded
(
{
extended
:
true
,
limit
:
'136mb'
}
)
,
json
(
{
limit
:
'136mb'
}
)
,
compression
(
)
,
)
;
app
.
set
(
'view engine'
,
'hbs'
)
;
app
.
set
(
'views'
,
join
(
ROOT_DIR
,
'views'
)
)
;
app
.
use
(
express
.
static
(
join
(
ROOT_DIR
,
'public'
)
)
)
;
app
.
use
(
'/store'
,
express
.
static
(
join
(
ROOT_DIR
,
'store'
)
)
)
;
app
.
use
(
'/'
,
router
)
;
app
.
use
(
(
err
:
Error
,
req
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
if
(
err
)
{
const
webhook
=
configService
.
get
<
Webhook
>
(
'WEBHOOK'
)
;
if
(
webhook
.
EVENTS
.
ERRORS_WEBHOOK
&&
webhook
.
EVENTS
.
ERRORS_WEBHOOK
!=
''
&&
webhook
.
EVENTS
.
ERRORS
)
{
const
tzoffset
=
new
Date
(
)
.
getTimezoneOffset
(
)
*
60000
;
//offset in milliseconds
const
localISOTime
=
new
Date
(
Date
.
now
(
)
-
tzoffset
)
.
toISOString
(
)
;
const
now
=
localISOTime
;
const
globalApiKey
=
configService
.
get
<
Auth
>
(
'AUTHENTICATION'
)
.
API_KEY
.
KEY
;
const
serverUrl
=
configService
.
get
<
HttpServer
>
(
'SERVER'
)
.
URL
;
const
errorData
=
{
event
:
'error'
,
data
:
{
error
:
err
[
'error'
]
||
'Internal Server Error'
,
message
:
err
[
'message'
]
||
'Internal Server Error'
,
status
:
err
[
'status'
]
||
500
,
response
:
{
message
:
err
[
'message'
]
||
'Internal Server Error'
,
}
,
}
,
date_time
:
now
,
api_key
:
globalApiKey
,
server_url
:
serverUrl
,
}
;
logger
.
error
(
errorData
)
;
const
baseURL
=
webhook
.
EVENTS
.
ERRORS_WEBHOOK
;
const
httpService
=
axios
.
create
(
{
baseURL
}
)
;
httpService
.
post
(
''
,
errorData
)
;
}
return
res
.
status
(
err
[
'status'
]
||
500
)
.
json
(
{
status
:
err
[
'status'
]
||
500
,
error
:
err
[
'error'
]
||
'Internal Server Error'
,
response
:
{
message
:
err
[
'message'
]
||
'Internal Server Error'
,
}
,
}
)
;
}
next
(
)
;
}
,
(
req
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
const
{
method
,
url
}
=
req
;
res
.
status
(
HttpStatus
.
NOT_FOUND
)
.
json
(
{
status
:
HttpStatus
.
NOT_FOUND
,
error
:
'Not Found'
,
response
:
{
message
:
[
`Cannot
${
method
.
toUpperCase
(
)
}
${
url
}
`
]
,
}
,
}
)
;
next
(
)
;
}
,
)
;
const
httpServer
=
configService
.
get
<
HttpServer
>
(
'SERVER'
)
;
ServerUP
.
app
=
app
;
const
server
=
ServerUP
[
httpServer
.
TYPE
]
;
eventManager
.
init
(
server
)
;
server
.
listen
(
httpServer
.
PORT
,
(
)
=>
logger
.
log
(
httpServer
.
TYPE
.
toUpperCase
(
)
+
' - ON: '
+
httpServer
.
PORT
)
)
;
initWA
(
)
;
onUnexpectedError
(
)
;
}
bootstrap
(
)
;
Back
|
FazBrowse Home
|
New Git URL