Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
node
/
app-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 549940e1
authored
Sep 21, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
完善一些功能
1 parent
ce33cdb0
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
98 additions
and
78 deletions
.env
package.json
src/config/file.ts
src/initor/doc.ts
src/initor/index.ts
src/initor/ws.ts
src/main.ts
src/modules/broadcast/broadcast.controller.ts
src/modules/config/config.controller.ts
src/modules/device/device.controller.ts
src/modules/file/file.controller.ts
src/modules/ws/ws.gateway.ts
test.http
tsconfig.json
yarn.lock
.env
View file @
549940e
...
...
@@ -5,4 +5,5 @@
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://limin:limin1018@192.168.1.129:3306/app_admin?schema=public"
SOCKET_PORT=8080
\ No newline at end of file
SOCKET_PORT= 8080
STATIC_PATH= "public"
\ No newline at end of file
package.json
View file @
549940e
...
...
@@ -44,7 +44,7 @@
"@nestjs/platform-socket.io"
:
"^10.4.1"
,
"@nestjs/platform-ws"
:
"^10.4.1"
,
"@nestjs/schedule"
:
"^0.3.1"
,
"@nestjs/swagger"
:
"
^
4.5.5"
,
"@nestjs/swagger"
:
"4.5.5"
,
"@nestjs/typeorm"
:
"^7.0.0"
,
"@nestjs/websockets"
:
"^10.4.1"
,
"@prisma/client"
:
"^5.19.1"
,
...
...
@@ -67,9 +67,8 @@
"rimraf"
:
"^3.0.2"
,
"rxjs"
:
"^7.8.1"
,
"socket.io"
:
"4.7.4"
,
"swagger-ui-express"
:
"^4.1.4"
,
"typeorm"
:
"^0.2.25"
,
"ws"
:
"^8.18.0"
"swagger-ui-express"
:
"^5.0.1"
,
"typeorm"
:
"^0.2.25"
},
"devDependencies"
:
{
"@nestjs/cli"
:
"^7.0.0"
,
...
...
src/config/file.ts
View file @
549940e
...
...
@@ -2,9 +2,9 @@ import { join,extname } from 'path';
import
{
diskStorage
}
from
'multer'
;
export
default
{
root
:
join
(
__dirname
,
'../uploads'
),
root
:
join
(
__dirname
,
'..'
,
'..'
,
process
.
env
.
STATIC_PATH
),
storage
:
diskStorage
({
destination
:
join
(
__dirname
,
`../uploads
/
${
new
Date
().
toLocaleDateString
()}
`
),
destination
:
join
(
__dirname
,
'..'
,
'..'
,
`
${
process
.
env
.
STATIC_PATH
}
/
${
new
Date
().
toLocaleDateString
()}
`
),
filename
:
(
req
,
file
,
cb
)
=>
{
const
deviceId
=
req
.
body
.
deviceId
;
// const name=file.originalname.split('.');
...
...
src/initor/doc.ts
0 → 100644
View file @
549940e
import
{
DocumentBuilder
,
SwaggerModule
}
from
'@nestjs/swagger'
import
{
name
,
description
,
version
}
from
'../../package.json'
import
{
INestApplication
}
from
'@nestjs/common'
export
const
docBuilder
=
(
app
:
INestApplication
<
any
>
)
=>
{
const
options
=
new
DocumentBuilder
()
.
setTitle
(
name
)
.
setDescription
(
description
)
.
setVersion
(
version
)
.
addBearerAuth
()
// 允许tonken鉴权
.
build
()
const
document
=
SwaggerModule
.
createDocument
(
app
,
options
)
SwaggerModule
.
setup
(
'/api/doc'
,
app
,
document
)
}
src/initor/index.ts
0 → 100644
View file @
549940e
export
*
from
'./doc'
export
*
from
'./ws'
\ No newline at end of file
src/initor/ws.ts
0 → 100644
View file @
549940e
import
{
INestApplication
}
from
'@nestjs/common'
import
{
RedisIoAdapter
}
from
'../common/adapters'
export
const
wsRedisAdapter
=
async
(
app
:
INestApplication
<
any
>
)
=>
{
const
redisIoAdapter
=
new
RedisIoAdapter
(
app
)
await
redisIoAdapter
.
connectToRedis
()
app
.
useWebSocketAdapter
(
redisIoAdapter
)
}
src/main.ts
View file @
549940e
import
{
NestFactory
}
from
'@nestjs/core'
import
{
DocumentBuilder
,
SwaggerModule
}
from
'@nestjs/swagger'
import
{
HttpExceptionFilter
,
ExceptionsFilter
}
from
'./common/filters'
import
{
ValidationPipe
}
from
'./common/pipes/validation.pipe'
import
{
LoggingInterceptor
}
from
'./common/interceptors/logging.interceptor'
import
{
RedisIoAdapter
}
from
'./common/adapters'
import
{
AppModule
}
from
'./app.module'
import
{
WsAdapter
}
from
'@nestjs/platform-w
s'
import
{
join
}
from
'path'
import
{
NestExpressApplication
}
from
'@nestjs/platform-expres
s'
import
{
docBuilder
,
wsRedisAdapter
}
from
'./initor'
async
function
bootstrap
()
{
const
app
=
await
NestFactory
.
create
(
AppModule
)
const
app
=
await
NestFactory
.
create
<
NestExpressApplication
>
(
AppModule
)
// 全局使用中间件
// app.use(logger)
...
...
@@ -17,31 +15,18 @@ async function bootstrap() {
// 全局过滤器
app
.
useGlobalFilters
(
new
ExceptionsFilter
())
app
.
useGlobalFilters
(
new
HttpExceptionFilter
())
app
.
enableCors
({
allowedHeaders
:
'*'
,
origin
:
'*'
,
credentials
:
true
,
})
const
redisIoAdapter
=
new
RedisIoAdapter
(
app
);
await
redisIoAdapter
.
connectToRedis
();
app
.
useWebSocketAdapter
(
redisIoAdapter
);
// app.useWebSocketAdapter(new WsAdapter(app))
app
.
enableCors
()
await
wsRedisAdapter
(
app
)
// app.useWebSocketAdapter(new WsAdapter(app))
// app.useGlobalFilters(new ValidateExceptionFilter())
// 全局管道
// app.useGlobalPipes(new ValidationPipe());
// 全局拦截器
// app.useGlobalInterceptors(new LoggingInterceptor());
app
.
useStaticAssets
(
join
(
__dirname
,
'..'
,
process
.
env
.
STATIC_PATH
))
// 设置swagger文档相关配置
const
swaggerOptions
=
new
DocumentBuilder
()
.
setTitle
(
'app-admin api document'
)
.
setDescription
(
'nest starter project api document'
)
.
setVersion
(
'1.0'
)
.
addBearerAuth
()
.
build
()
const
document
=
SwaggerModule
.
createDocument
(
app
,
swaggerOptions
)
SwaggerModule
.
setup
(
'doc'
,
app
,
document
)
docBuilder
(
app
)
await
app
.
listen
(
3000
)
}
bootstrap
()
src/modules/broadcast/broadcast.controller.ts
View file @
549940e
...
...
@@ -4,7 +4,8 @@ import { TransformInterceptor } from '../../common/interceptors'
import
{
WsGateway
}
from
'../ws/ws.gateway'
import
{
BroadcastDto
}
from
'../../dto'
import
{
ValidationPipe
}
from
'../../common/pipes'
import
{
ApiTags
}
from
'@nestjs/swagger'
@
ApiTags
(
'broadcast'
)
@
Controller
(
'broadcast'
)
@
UseInterceptors
(
TransformInterceptor
)
@
UsePipes
(
new
ValidationPipe
())
// 使用管道验证
...
...
src/modules/config/config.controller.ts
View file @
549940e
...
...
@@ -3,6 +3,8 @@ import { ConfigService } from './config.service'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
LogConfigDto
}
from
'../../dto/config.dto'
import
{
PagesPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
ApiTags
}
from
'@nestjs/swagger'
@
ApiTags
(
'config'
)
@
Controller
(
'config'
)
@
UseInterceptors
(
TransformInterceptor
)
@
UsePipes
(
new
ValidationPipe
())
// 使用管道验证
...
...
src/modules/device/device.controller.ts
View file @
549940e
...
...
@@ -5,7 +5,8 @@ import { AddDeviceDto, AddDeviceInfoDto, DeviceStatusDto } from '../../dto'
import
{
PagesPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
OnEvent
,
EventEmitter2
}
from
'@nestjs/event-emitter'
import
{
WsAccess
,
WsConnEvents
}
from
'../../common/enums'
import
{
ApiTags
}
from
'@nestjs/swagger'
@
ApiTags
(
'device'
)
@
Controller
(
'device'
)
@
UseInterceptors
(
TransformInterceptor
)
// @UsePipes(new ValidationPipe({transform: true}))
...
...
src/modules/file/file.controller.ts
View file @
549940e
...
...
@@ -6,7 +6,8 @@ import { FileMimeTypeFilter } from '../../common/filters'
import
{
UploadFile
}
from
'../../common/decorators'
import
{
DocTypes
}
from
'src/common/enums'
import
{
FileDto
}
from
'../../dto'
import
{
ApiTags
}
from
'@nestjs/swagger'
@
ApiTags
(
'file'
)
@
UsePipes
(
new
ValidationPipe
())
@
Controller
(
'file'
)
export
class
FileController
{
...
...
src/modules/ws/ws.gateway.ts
View file @
549940e
...
...
@@ -5,7 +5,7 @@ import { Server, Socket } from 'socket.io'
import
{
WsService
}
from
'./ws.service'
import
{
Logger
,
Module
,
UseInterceptors
}
from
'@nestjs/common'
import
{
WsMessageType
,
WsStatus
,
WsAccess
}
from
'../../common/enums'
import
{
TransformInterceptor
}
from
'../../
/
common/interceptors'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
/**
* WebSocket 网关
...
...
@@ -15,7 +15,7 @@ import { TransformInterceptor } from '../..//common/interceptors'
* @implements OnGatewayDisconnect
* @implements OnGatewayInit
*/
@
WebSocketGateway
(
8080
,
{
@
WebSocketGateway
(
+
process
.
env
.
SOCKET_PORT
,
{
origin
:
'*'
,
// 允许任何来源
methods
:
[
'GET'
,
'POST'
],
// 允许的 HTTP 请求类型
cors
:
true
// 允许跨域
...
...
@@ -105,7 +105,7 @@ export class WsGateway implements WsTypes.WebSocketGateway<Server, Observable<Ws
* @param server
*/
afterInit
(
server
:
Server
)
{
Logger
.
log
(
'websocket init... port: '
+
process
.
env
.
PORT
)
Logger
.
log
(
'websocket init... port: '
+
process
.
env
.
SOCKET_
PORT
)
this
.
wsService
.
server
=
server
// 重置 socketIds
this
.
wsService
.
resetClients
()
...
...
test.http
View file @
549940e
...
...
@@ -31,29 +31,29 @@
POST http://localhost:3000/broadcast/message HTTP/1.1
content-type: application/json
#
POST http://localhost:3000/broadcast/message HTTP/1.1
#
content-type: application/json
{
"event":"private",
"device_id":1,
"data":{
"order":"logs",
"payload":{
"message":"log message"
}
}
}
#
{
#
"event":"private",
#
"device_id":1,
#
"data":{
#
"order":"logs",
#
"payload":{
#
"message":"log message"
#
}
#
}
#
}
# http://localhost:3000/file/delete?id=16
# http://localhost:3000/config?device_id=2
#
POST http://localhost:3000/config/add HTTP/1.1
#
content-type: application/json
POST http://localhost:3000/config/add HTTP/1.1
content-type: application/json
#
{
#
"device_id":1,
#
"log_saved_days":8,
#
"user_fields":[1,2,3,4]
#
}
{
"device_id":1,
"log_saved_days":8,
"user_fields":[1,2,3,4]
}
tsconfig.json
View file @
549940e
...
...
@@ -9,7 +9,8 @@
"sourceMap"
:
true
,
"outDir"
:
"./dist"
,
"baseUrl"
:
"./"
,
"incremental"
:
true
"incremental"
:
true
,
"resolveJsonModule"
:
true
},
"exclude"
:
[
"node_modules"
,
"dist"
]
}
yarn.lock
View file @
549940e
...
...
@@ -716,10 +716,10 @@
"@types/jsonwebtoken" "9.0.5"
jsonwebtoken "9.0.2"
"@nestjs/mapped-types@0.
4.1
":
version "0.
4.1
"
resolved "https://registry.npm
.taobao.org/@nestjs/mapped-types/download/@nestjs/mapped-types-0.4.1.tgz#e7fe038f0bdda7b8f858fa79ca8516b8f9069b1a
"
integrity sha
1-5/4Djwvdp7j4WPp5yoUWuPkGmxo
=
"@nestjs/mapped-types@0.
0.3
":
version "0.
0.3
"
resolved "https://registry.npm
mirror.com/@nestjs/mapped-types/-/mapped-types-0.0.3.tgz#7b50206935452e74a270ed9a07ad05a6305b0b42
"
integrity sha
512-PKxWOVaMyvj9/82afLpMZ/+Pb5JacCURRP/FY/tCDtsoS+MABeQGj2oqbtH4uQUiL4n+H0IigvQGHWQ1zPPknA=
=
"@nestjs/passport@^7.0.0":
version "7.1.6"
...
...
@@ -772,13 +772,13 @@
jsonc-parser "3.0.0"
pluralize "8.0.0"
"@nestjs/swagger@
^
4.5.5":
version "4.
8.2
"
resolved "https://registry.npmmirror.com/@nestjs/swagger/
download/@nestjs/swagger-4.8.2.tgz#0a0b3ca1b25146e797ca77addd9fa97f82406c1c
"
integrity sha
1-Cgs8obJRRueXynet3Z+pf4JAbBw
=
"@nestjs/swagger@4.5.5":
version "4.
5.5
"
resolved "https://registry.npmmirror.com/@nestjs/swagger/
-/swagger-4.5.5.tgz#6d3c8ee182d8e0186d55dd5a5d62d1cdba07a1d2
"
integrity sha
512-c8y0XbGyqhFVfAWEC0hYkSgXmzewmv6b1DfE9A5pk8T1uaYSgQ5RNH9RxYJSbjCkyUgOPXujy704lqQWWPpFdw=
=
dependencies:
"@nestjs/mapped-types" "0.
4.1
"
lodash "4.17.
21
"
"@nestjs/mapped-types" "0.
0.3
"
lodash "4.17.
15
"
path-to-regexp "3.2.0"
"@nestjs/testing@^7.0.0":
...
...
@@ -6689,7 +6689,12 @@ lodash.union@^4.6.0:
resolved "https://registry.npmjs.org/lodash.union/-/lodash.union-4.6.0.tgz#48bb5088409f16f1821666641c44dd1aaae3cd88"
integrity sha512-c4pB2CdGrGdjMKYLA+XiRDO7Y0PRQbm/Gzg8qMj+QH+pFVAoTp5sBpO0odL3FjoPCGjK96p6qsP+yQoiLoOBcw==
lodash@4.17.21, lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4:
lodash@4.17.15:
version "4.17.15"
resolved "https://registry.npmmirror.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4:
version "4.17.21"
resolved "https://registry.npm.taobao.org/lodash/download/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha1-Z5WRxWTDv/quhFTPCz3zcMPWkRw=
...
...
@@ -9499,17 +9504,17 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
swagger-ui-dist@
^3.18.1
:
version "
3.51.1
"
resolved "https://registry.npmmirror.com/swagger-ui-dist/
download/swagger-ui-dist-3.51.1.tgz#683bda7487cca0c7d17e4dd0bf1f675e53768fc4
"
integrity sha
1-aDvadIfMoMfRfk3Qvx9nXlN2j8Q
=
swagger-ui-dist@
>=5.0.0
:
version "
5.17.14
"
resolved "https://registry.npmmirror.com/swagger-ui-dist/
-/swagger-ui-dist-5.17.14.tgz#e2c222e5bf9e15ccf80ec4bc08b4aaac09792fd6
"
integrity sha
512-CVbSfaLpstV65OnSjbXfVd6Sta3q3F7Cj/yYuvHMp1P90LztOLs6PfUnKEVAeiIVQt9u2SaPwv0LiH/OyMjHRw=
=
swagger-ui-express@^
4.1.4
:
version "
4.1.6
"
resolved "https://registry.npm
.taobao.org/swagger-ui-express/download/swagger-ui-express-4.1.6.tgz#682294af3d5c70f74a1fa4d6a9b503a9ee55ea82
"
integrity sha
1-aCKUrz1ccPdKH6TWqbUDqe5V6oI
=
swagger-ui-express@^
5.0.1
:
version "
5.0.1
"
resolved "https://registry.npm
mirror.com/swagger-ui-express/-/swagger-ui-express-5.0.1.tgz#fb8c1b781d2793a6bd2f8a205a3f4bd6fa020dd8
"
integrity sha
512-SrNU3RiBGTLLmFU8GIJdOdanJTl4TOmT27tt3bWWHppqYmAZ6IDuEuBvMU6nZq0zLEe6b/1rACXCgLZqO6ZfrA=
=
dependencies:
swagger-ui-dist "
^3.18.1
"
swagger-ui-dist "
>=5.0.0
"
symbol-observable@3.0.0:
version "3.0.0"
...
...
@@ -10500,7 +10505,7 @@ write-file-atomic@^3.0.0:
signal-exit "^3.0.2"
typedarray-to-buffer "^3.1.5"
ws@8.18.0
, ws@^8.18.0
:
ws@8.18.0:
version "8.18.0"
resolved "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc"
integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment