-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (78 loc) · 2.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import http from "http"
import https from "https"
import {resolve} from "path"
import { pathToFileURL } from 'url'
import deepmerge from "deepmerge"
import consola from "consola"
import initApp from "./core/app.js"
import defConfig from "./koavc.config.js"
import {getLocalConfig} from "vsfc"
process.on("uncaughtException",(e)=>{
consola.error(e)
})
// process.on("unhandledRejection",(e)=>{
// consola.error(e)
// })
// 启动生产web服务
export async function run(callback){
const config = await getConfig()
const app = await initApp(config)
const httpsOption = config.https
const serverSchame = httpsOption?https:http
const port = config.port
const host = config.host||'0.0.0.0'
let server = null
const cb = (err)=>{
callback && callback(err,server,config)
const logger = app.context.logger
if (err) {
logger.error(err)
} else {
const muse = process.memoryUsage()
logger.info(`Memory usage ${(muse.heapTotal/(1024*1024)).toFixed(2)}MB (rss:${(muse.rss/(1024*1024)).toFixed(2)}MB)`)
logger.success(`Server start success. ${httpsOption?'https':'http'}://${host}:${port}`)
}
}
if(httpsOption){
server = serverSchame.createServer(httpsOption,app.callback())
}else{
server = serverSchame.createServer(app.callback())
}
server.listen(port,host, cb)
return server
}
/**
* 加载项目自定义的config文件,合并生成运行时config文件
* @export object
*/
export async function getConfig(){
try{
const localConfig = await import(pathToFileURL(resolve("./koavc.config.js"))).then(m=>m.default)
const config = deepmerge(defConfig,localConfig)
config.app = initAppOptions(config.app)
// vsfc.config.js中 injectPath如果设置了,就自动挂载生成vue编译资源文件静态服务
const vsfcLocalConfig = await getLocalConfig()
config.vueInjectPath = vsfcLocalConfig.injectUrl||false
return config
}catch(e){
consola.error('Error in [koavc.config.js].',e)
process.exit(1)
}
}
// 初始化多应用的配置参数
function initAppOptions(options){
if(!options || options.length==0){
throw new Error("There should be at least one app")
}
options.map((option,id)=>{
if(!option.dir){
throw new Error(`The ${id}th app option [dir] is required`)
}
option._dir = option.dir
option.dir = resolve(option.dir)
// if(!option.prefix){
// option.prefix = '/'
// }
})
return options
}