refactor: migrate to Tencent Cloud backend
这个提交包含在:
@@ -0,0 +1,41 @@
|
||||
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') })
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const mysql = require('mysql2/promise')
|
||||
const config = require('../src/config')
|
||||
const { hashPassword, randomHex } = require('../src/lib/auth')
|
||||
|
||||
async function main() {
|
||||
const conn = await mysql.createConnection({
|
||||
host: config.db.host,
|
||||
port: config.db.port,
|
||||
user: config.db.user,
|
||||
password: config.db.password,
|
||||
database: config.db.database,
|
||||
multipleStatements: true
|
||||
})
|
||||
|
||||
const schema = fs.readFileSync(path.join(__dirname, '..', 'sql', 'schema.sql'), 'utf8')
|
||||
await conn.query(schema)
|
||||
|
||||
await conn.query("ALTER TABLE devices MODIFY product_id VARCHAR(64) NOT NULL DEFAULT 'HOX_LIGHT_MASK'").catch(() => {})
|
||||
|
||||
const [rows] = await conn.execute('SELECT admin_id FROM admin_accounts WHERE username = ?', [config.admin.username])
|
||||
if (rows.length === 0) {
|
||||
const salt = randomHex(16)
|
||||
const passwordHash = hashPassword(config.admin.password, salt)
|
||||
await conn.execute(
|
||||
'INSERT INTO admin_accounts (username, password_hash, password_salt, real_name, role, status) VALUES (?, ?, ?, ?, ?, 1)',
|
||||
[config.admin.username, passwordHash, salt, '管理员', 'admin']
|
||||
)
|
||||
}
|
||||
|
||||
await conn.end()
|
||||
console.log('Database initialized:', config.db.database)
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') })
|
||||
|
||||
const http = require('http')
|
||||
const config = require('../src/config')
|
||||
const { handle } = require('../src/app')
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const chunks = []
|
||||
req.on('data', chunk => chunks.push(chunk))
|
||||
req.on('end', async () => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
const event = {
|
||||
httpMethod: req.method,
|
||||
path: url.pathname,
|
||||
headers: req.headers,
|
||||
queryStringParameters: Object.fromEntries(url.searchParams.entries()),
|
||||
body: Buffer.concat(chunks).toString('utf8')
|
||||
}
|
||||
const result = await handle(event)
|
||||
res.writeHead(result.statusCode, result.headers)
|
||||
res.end(result.body || '')
|
||||
})
|
||||
})
|
||||
|
||||
server.listen(config.port, () => {
|
||||
console.log('SCF local server listening on http://localhost:' + config.port)
|
||||
})
|
||||
在新工单中引用
屏蔽一个用户