2017-03-10 09:24:06 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-04-07 01:22:00 +00:00
|
|
|
"github.com/ouqiang/gocron/modules/app"
|
|
|
|
"github.com/ouqiang/gocron/routers"
|
2017-04-02 02:38:49 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
"gopkg.in/macaron.v1"
|
2017-04-04 06:42:33 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-04-07 01:22:00 +00:00
|
|
|
"github.com/ouqiang/gocron/modules/logger"
|
2017-03-10 09:24:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// web服务器默认端口
|
|
|
|
const DefaultPort = 5920
|
2017-04-02 02:19:52 +00:00
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
var CmdWeb = cli.Command{
|
2017-04-02 02:38:49 +00:00
|
|
|
Name: "server",
|
|
|
|
Usage: "start scheduler web server",
|
|
|
|
Action: run,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "port,p",
|
|
|
|
Value: DefaultPort,
|
|
|
|
Usage: "bind port number",
|
|
|
|
},
|
2017-04-03 07:27:19 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "env,e",
|
2017-04-23 09:00:47 +00:00
|
|
|
Value: "prod",
|
2017-04-03 07:27:19 +00:00
|
|
|
Usage: "runtime environment, dev|test|prod",
|
|
|
|
},
|
2017-04-02 02:38:49 +00:00
|
|
|
},
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func run(ctx *cli.Context) {
|
2017-04-04 06:42:33 +00:00
|
|
|
// 设置运行环境
|
2017-04-03 07:27:19 +00:00
|
|
|
setEnvironment(ctx)
|
2017-04-04 06:42:33 +00:00
|
|
|
// 初始化应用
|
2017-04-02 02:38:49 +00:00
|
|
|
app.InitEnv()
|
2017-04-04 06:42:33 +00:00
|
|
|
// 捕捉信号,配置热更新等
|
|
|
|
go catchSignal()
|
2017-04-02 02:38:49 +00:00
|
|
|
m := macaron.Classic()
|
|
|
|
// 注册路由
|
|
|
|
routers.Register(m)
|
2017-04-10 09:37:16 +00:00
|
|
|
// 注册中间件.
|
2017-04-13 09:35:59 +00:00
|
|
|
routers.RegisterMiddleware(m)
|
2017-04-02 02:38:49 +00:00
|
|
|
port := parsePort(ctx)
|
|
|
|
m.Run(port)
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 解析端口
|
|
|
|
func parsePort(ctx *cli.Context) int {
|
2017-04-03 07:27:19 +00:00
|
|
|
var port int = DefaultPort
|
2017-04-02 02:38:49 +00:00
|
|
|
if ctx.IsSet("port") {
|
|
|
|
port = ctx.Int("port")
|
|
|
|
}
|
|
|
|
if port <= 0 || port >= 65535 {
|
|
|
|
port = DefaultPort
|
|
|
|
}
|
2017-03-10 09:24:06 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
return port
|
2017-04-02 02:19:52 +00:00
|
|
|
}
|
2017-04-03 07:27:19 +00:00
|
|
|
|
|
|
|
func setEnvironment(ctx *cli.Context) {
|
2017-04-23 09:00:47 +00:00
|
|
|
var env string = "prod"
|
2017-04-03 07:27:19 +00:00
|
|
|
if ctx.IsSet("env") {
|
|
|
|
env = ctx.String("env")
|
|
|
|
}
|
|
|
|
|
2017-04-23 09:00:47 +00:00
|
|
|
switch env {
|
|
|
|
case "prod":
|
|
|
|
macaron.Env = macaron.PROD
|
|
|
|
case "test":
|
|
|
|
macaron.Env = macaron.TEST
|
|
|
|
case "dev":
|
|
|
|
macaron.Env = macaron.DEV
|
2017-04-03 07:27:19 +00:00
|
|
|
}
|
2017-04-04 06:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 捕捉信号
|
|
|
|
func catchSignal() {
|
|
|
|
c := make(chan os.Signal)
|
2017-04-06 06:40:48 +00:00
|
|
|
// todo 配置热更新, windows 不支持 syscall.SIGUSR1, syscall.SIGUSR2
|
|
|
|
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
2017-04-04 09:05:55 +00:00
|
|
|
for {
|
|
|
|
s := <- c
|
|
|
|
logger.Info("收到信号 -- ", s)
|
|
|
|
switch s {
|
|
|
|
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM:
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2017-04-03 07:27:19 +00:00
|
|
|
}
|