You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ims/app/app.go

50 lines
1.1 KiB

2 months ago
package app
import (
"context"
"ims/app/http"
"ims/app/listeners"
"ims/database"
"ims/util"
"ims/util/evio"
"ims/util/log"
"sync/atomic"
)
var closed uint32
func Start(ctx context.Context) error {
var err error
check := func(fn func(ctx context.Context) error) {
if err == nil {
err = fn(ctx)
}
}
check(log.Init) // 初始化日志
check(listeners.Init) // 初始化注册事件
check(util.Init) // 初始化工具包
check(database.Init) // 初始化数据库
check(callEvent("app.init")) // 触发初始化事件
check(evio.Start) // 启动事件总线
check(http.Start) // 启动HTTP服务器
check(callEvent("app.boot")) // 触发启动事件
return err
}
func callEvent(topic string) func(ctx context.Context) error {
return func(ctx context.Context) error {
evio.Call(topic, nil) // 触发事件
return nil
}
}
func Stop() error {
if !atomic.CompareAndSwapUint32(&closed, 0, 1) {
return nil
}
err := http.Stop() // 停止HTTP服务器
evio.Call("app.exit", err) // 触发退出事件
evio.Stop() // 停止事件总线
return err
}