cronsun/bin/web/server.go

53 lines
926 B
Go
Raw Normal View History

2017-01-09 02:32:14 +00:00
package main
import (
"net"
"github.com/cockroachdb/cmux"
"sunteng/commons/event"
"sunteng/commons/log"
"sunteng/cronsun/conf"
2017-01-09 10:51:52 +00:00
"sunteng/cronsun/models"
2017-01-09 02:32:14 +00:00
"sunteng/cronsun/web"
)
func main() {
2017-01-09 10:51:52 +00:00
if err := models.Init(); err != nil {
log.Error(err.Error())
return
}
2017-01-09 02:32:14 +00:00
l, err := net.Listen("tcp", conf.Config.Web.BindAddr)
if err != nil {
2017-01-09 03:10:55 +00:00
log.Error(err.Error())
return
2017-01-09 02:32:14 +00:00
}
// Create a cmux.
m := cmux.New(l)
httpL := m.Match(cmux.HTTP1Fast())
httpServer, err := web.InitRouters()
if err != nil {
2017-01-09 03:10:55 +00:00
log.Error(err.Error())
return
2017-01-09 02:32:14 +00:00
}
2017-01-11 08:12:37 +00:00
go func() {
err := httpServer.Serve(httpL)
if err != nil {
panic(err.Error())
}
}()
go m.Serve()
2017-01-09 02:32:14 +00:00
log.Noticef("cronsun web server started on %s, Ctrl+C or send kill sign to exit", conf.Config.Web.BindAddr)
// 注册退出事件
event.On(event.EXIT, conf.Exit)
2017-01-09 02:32:14 +00:00
// 监听退出信号
event.Wait()
event.Emit(event.EXIT, nil)
log.Notice("exit success")
}