改进 打包以及 Dockerfile
This commit is contained in:
parent
4d84f44ea4
commit
bad16623be
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,4 +6,5 @@ tmp/
|
|||||||
temp/
|
temp/
|
||||||
yarn.lock
|
yarn.lock
|
||||||
ormconfig.json
|
ormconfig.json
|
||||||
.env
|
.env
|
||||||
|
dist/
|
17
Dockerfile
17
Dockerfile
@ -2,18 +2,9 @@ FROM node:20
|
|||||||
LABEL authors="ivamp"
|
LABEL authors="ivamp"
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
# 忽略 node_modules 目录
|
|
||||||
COPY src /app/src
|
|
||||||
COPY package.json /app/package.json
|
|
||||||
COPY .env.example /app/.env.example
|
|
||||||
COPY main.ts /app/main.ts
|
|
||||||
|
|
||||||
RUN npm config set registry https://registry.npm.taobao.org/
|
# 将本文件夹 dist 下的目录复制
|
||||||
RUN npm install
|
ADD ./dist /app
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
CMD [ "npm", "run", "migrate" ]
|
ENTRYPOINT ["node", "index.js"]
|
||||||
|
|
||||||
CMD [ "npm", "run", "start" ]
|
|
||||||
|
|
||||||
EXPOSE 8080
|
|
||||||
|
@ -23,6 +23,9 @@
|
|||||||
"start": "ts-node main.ts",
|
"start": "ts-node main.ts",
|
||||||
"typeorm": "typeorm-ts-node-commonjs",
|
"typeorm": "typeorm-ts-node-commonjs",
|
||||||
"migrate": "typeorm-ts-node-commonjs migration:run -d src/config/typeorm.ts",
|
"migrate": "typeorm-ts-node-commonjs migration:run -d src/config/typeorm.ts",
|
||||||
"migrate:rollback": "typeorm-ts-node-commonjs migration:revert -d src/config/typeorm.ts"
|
"migrate:rollback": "typeorm-ts-node-commonjs migration:revert -d src/config/typeorm.ts",
|
||||||
|
"make:migration": "typeorm-ts-node-commonjs migration:create",
|
||||||
|
"migration:fresh": "typeorm-ts-node-commonjs schema:drop -d src/config/typeorm.ts",
|
||||||
|
"build": "ncc build main.ts -o dist"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,12 @@ import typeorm from "./typeorm";
|
|||||||
|
|
||||||
typeorm.initialize().then(async () => {
|
typeorm.initialize().then(async () => {
|
||||||
console.log("Datasource initialized.")
|
console.log("Datasource initialized.")
|
||||||
|
|
||||||
|
console.log("Run database migrations.")
|
||||||
|
typeorm.runMigrations().then(async () => {
|
||||||
|
console.log("Migration run successfully.")
|
||||||
|
}).catch(error => console.log(error))
|
||||||
}).catch(error => console.log(error))
|
}).catch(error => console.log(error))
|
||||||
|
|
||||||
|
|
||||||
typeorm.runMigrations().then(async () => {
|
|
||||||
console.log("Migration run successfully.")
|
|
||||||
}).catch(error => console.log(error))
|
|
||||||
|
|
||||||
export default typeorm.manager
|
export default typeorm.manager
|
@ -1,8 +1,19 @@
|
|||||||
import {NextFunction, Request, Response} from 'express'
|
import {NextFunction, Response} from 'express'
|
||||||
import {JWTRequest} from "../types/JWTRequest";
|
import {JWTRequest} from "../types/JWTRequest";
|
||||||
|
|
||||||
const getUser = (req: JWTRequest, res: Response, next: NextFunction) => {
|
const getUser = (req: JWTRequest, res: Response, next: NextFunction) => {
|
||||||
|
|
||||||
|
// 检测有无 authorization
|
||||||
|
if (!req.headers.authorization) {
|
||||||
|
return res.status(401).send({
|
||||||
|
'message': 'Unauthorized with no authorization header.'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 从 header 中获取 token
|
// 从 header 中获取 token
|
||||||
|
|
||||||
|
|
||||||
const token = req.headers.authorization.split(' ')[1]
|
const token = req.headers.authorization.split(' ')[1]
|
||||||
|
|
||||||
// 获取 JWT 的用户部分
|
// 获取 JWT 的用户部分
|
||||||
|
Loading…
Reference in New Issue
Block a user