feat: 添加构建时间信息和相关功能
This commit is contained in:
@@ -1,23 +1,29 @@
|
||||
const express = require('express')
|
||||
const { success } = require('../utils/response')
|
||||
const { getBuildTimestamp } = require('../utils/buildInfo')
|
||||
const wechatRoutes = require('./wechatRoutes')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.post('/test-helloworld', (req, res) => {
|
||||
function respondHelloWorld(req, res) {
|
||||
return success(res, '请求成功', {
|
||||
message: 'Hello, World!',
|
||||
timestamp: new Date().toISOString(),
|
||||
status: 'success',
|
||||
build_time: getBuildTimestamp(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
router.post('/health', (req, res) => {
|
||||
function respondHealth(req, res) {
|
||||
return success(res, '服务运行正常', {
|
||||
status: 'healthy',
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
router.post('/test-helloworld', respondHelloWorld)
|
||||
|
||||
router.post('/health', respondHealth)
|
||||
|
||||
router.use('/wechat', wechatRoutes)
|
||||
|
||||
|
||||
30
back-end/src/utils/buildInfo.js
Normal file
30
back-end/src/utils/buildInfo.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
function getBuildInfoFilePath() {
|
||||
return path.resolve(__dirname, '..', '..', 'build-info.json')
|
||||
}
|
||||
|
||||
function getBuildTimestamp() {
|
||||
if (process.env.BUILD_TIME) {
|
||||
return process.env.BUILD_TIME
|
||||
}
|
||||
|
||||
const filePath = getBuildInfoFilePath()
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf8')
|
||||
const payload = JSON.parse(content)
|
||||
return payload.buildTime || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getBuildTimestamp,
|
||||
}
|
||||
Reference in New Issue
Block a user