// | ------------------------------------------------------------ // | @版本: version 0.1 // | @创建人: 【Nie-x7129】 // | @E-mail: x71291@outlook.com // | @所在项目: initkoa // | @文件描述: bootstrap.js - Koa项目启动文件 // | @创建时间: 2023-11-25 21:17 // | @更新时间: 2023-11-25 21:17 // | @修改记录: // | -*-*-*- (时间--修改人--修改说明) -*-*-*- // | = // | ------------------------------------------------------------ import devConfig from '#root/development.env.js'; import prodConfig from '#root/production.env.js'; import startApp from '#home/app.js'; import winston from "winston"; import {logger, colorizer} from "#common/logger/index.js"; import createDatabase from "#common/database/index.js"; import {createCatch} from "#cache/index.js"; import initData from "#common/database/initData.js"; // | 获取ENV const ENV = process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase().trim(); if (ENV === 'development' || ENV === undefined) { global.config = devConfig; logger.add(new winston.transports.Console({ format:winston.format.combine( winston.format.printf( (i) => { return colorizer.colorize( i.level, i.message ); }, ), ) })) } else if (ENV === 'production') { global.config = prodConfig; logger.exceptions.handle(new winston.transports.File({ filename: 'winston-logs/winston-exceptions.log' })); logger.rejections.handle(new winston.transports.File({ filename: 'winston-logs/winston-rejections.log' })) } else { throw new Error(`未识别的环境变量${ENV}`); } global.logger = logger global.ENV = ENV; // = 函数名: checkPort // = 描述: 检测服务端口是否正常 // = 参数: None // = 返回值: undefined // = 创建人: nie // = 创建时间: 2023-11-25 21:49:22 - function checkPort() { if ( typeof global.config.port !== 'number' || global.config.port > 65536 || global.config.port < 3000 ) { throw new Error(`服务监听端口不合法:${global.config.port}`); } } checkPort(); // = 函数名: checkAppName // = 描述: 检测服务名是否正常 // = 参数: None // = 返回值: undefined // = 创建人: nie // = 创建时间: 2023-11-25 21:51:03 - function checkAppName() { if (!global.config.appName) { throw new Error(`服务名不存在:${global.config.appName}`); } } checkAppName() // process.stdout.write('\u001b[2J\u001b[0;0H'); async function createApp(){ const sequelize = createDatabase(logger); await sequelize.sync({alter: true}) await sequelize.authenticate().catch(e => { console.error(`数据库连接失败, ${e}`); throw new Error(e) }); logger.info(`== 已成功与数据库建立连接。 ==`); await initData(sequelize) await createCatch(sequelize) const app = startApp(); sequelize.getQueryInterface().showAllTables().then(data => { // console.log(data) }).catch(e => { console.error(e) }) app.context.sequelize = sequelize app.listen(config.port); logger.info( `Web服务 ${global.config.appName} 启动成功,访问: http://127.0.0.1:${global.config.port}`) } createApp()