2017-03-24 05:06:53 +00:00
|
|
|
|
package routers
|
|
|
|
|
|
2017-03-24 09:55:44 +00:00
|
|
|
|
import (
|
2017-04-02 02:38:49 +00:00
|
|
|
|
"github.com/go-macaron/binding"
|
2017-04-07 01:22:00 +00:00
|
|
|
|
"github.com/ouqiang/gocron/routers/install"
|
2017-04-02 02:38:49 +00:00
|
|
|
|
"gopkg.in/macaron.v1"
|
2017-04-07 01:22:00 +00:00
|
|
|
|
"github.com/ouqiang/gocron/routers/task"
|
|
|
|
|
"github.com/ouqiang/gocron/routers/host"
|
2017-04-08 09:15:30 +00:00
|
|
|
|
"github.com/ouqiang/gocron/routers/tasklog"
|
2017-04-13 09:35:59 +00:00
|
|
|
|
"github.com/ouqiang/gocron/modules/utils"
|
|
|
|
|
"github.com/go-macaron/session"
|
|
|
|
|
"github.com/go-macaron/csrf"
|
|
|
|
|
"github.com/go-macaron/toolbox"
|
|
|
|
|
"github.com/go-macaron/gzip"
|
|
|
|
|
"strings"
|
|
|
|
|
"github.com/ouqiang/gocron/modules/app"
|
2017-03-24 09:55:44 +00:00
|
|
|
|
)
|
2017-03-24 05:06:53 +00:00
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
// 静态文件目录
|
|
|
|
|
const StaticDir = "public"
|
|
|
|
|
|
2017-03-24 05:06:53 +00:00
|
|
|
|
// 路由注册
|
|
|
|
|
func Register(m *macaron.Macaron) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
// 所有GET方法,自动注册HEAD方法
|
|
|
|
|
m.SetAutoHead(true)
|
|
|
|
|
// 404错误
|
|
|
|
|
m.NotFound(func(ctx *macaron.Context) {
|
2017-04-13 09:35:59 +00:00
|
|
|
|
if isGetRequest(ctx) && !isAjaxRequest(ctx) {
|
|
|
|
|
ctx.Data["Title"] = "404 - NOT FOUND"
|
|
|
|
|
ctx.HTML(404, "error/404")
|
|
|
|
|
} else {
|
|
|
|
|
json := utils.JsonResponse{}
|
|
|
|
|
ctx.Resp.Write([]byte(json.Failure(utils.NotFound, "您访问的地址不存在")))
|
|
|
|
|
}
|
2017-04-02 02:38:49 +00:00
|
|
|
|
})
|
|
|
|
|
// 50x错误
|
|
|
|
|
m.InternalServerError(func(ctx *macaron.Context) {
|
2017-04-13 09:35:59 +00:00
|
|
|
|
if isGetRequest(ctx) && !isAjaxRequest(ctx) {
|
2017-04-16 08:24:09 +00:00
|
|
|
|
ctx.Data["Title"] = "500 - INTERNAL SERVER ERROR"
|
2017-04-13 09:35:59 +00:00
|
|
|
|
ctx.HTML(500, "error/500")
|
|
|
|
|
} else {
|
|
|
|
|
json := utils.JsonResponse{}
|
|
|
|
|
ctx.Resp.Write([]byte(json.Failure(utils.ServerError, "网站暂时无法访问,请稍后再试")))
|
|
|
|
|
}
|
2017-04-02 02:38:49 +00:00
|
|
|
|
})
|
|
|
|
|
// 首页
|
2017-04-13 09:35:59 +00:00
|
|
|
|
m.Get("/", Home)
|
2017-04-02 02:38:49 +00:00
|
|
|
|
// 系统安装
|
|
|
|
|
m.Group("/install", func() {
|
2017-04-07 01:13:36 +00:00
|
|
|
|
m.Get("", install.Create)
|
|
|
|
|
m.Post("/store", binding.Bind(install.InstallForm{}), install.Store)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 用户
|
|
|
|
|
m.Group("/user", func() {
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 任务
|
|
|
|
|
m.Group("/task", func() {
|
|
|
|
|
m.Get("/create", task.Create)
|
2017-04-07 09:26:46 +00:00
|
|
|
|
m.Post("/store", binding.Bind(task.TaskForm{}), task.Store)
|
2017-04-17 10:04:30 +00:00
|
|
|
|
m.Get("/edit/:id", task.Edit)
|
2017-04-08 09:15:30 +00:00
|
|
|
|
m.Get("", task.Index)
|
|
|
|
|
m.Get("/log", tasklog.Index)
|
2017-04-13 09:35:59 +00:00
|
|
|
|
m.Post("/log/clear", tasklog.Clear)
|
|
|
|
|
m.Post("/remove/:id", task.Remove)
|
|
|
|
|
m.Post("/enable/:id", task.Enable)
|
|
|
|
|
m.Post("/disable/:id", task.Disable)
|
2017-04-07 01:13:36 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 主机
|
|
|
|
|
m.Group("/host", func() {
|
|
|
|
|
m.Get("/create", host.Create)
|
2017-04-16 08:24:09 +00:00
|
|
|
|
m.Get("/Edit", host.Edit)
|
2017-04-07 09:26:46 +00:00
|
|
|
|
m.Post("/store", binding.Bind(host.HostForm{}), host.Store)
|
2017-04-08 09:15:30 +00:00
|
|
|
|
m.Get("", host.Index)
|
2017-04-13 09:35:59 +00:00
|
|
|
|
m.Post("/remove/:id", host.Remove)
|
2017-04-07 01:13:36 +00:00
|
|
|
|
})
|
2017-04-13 09:35:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 中间件注册
|
|
|
|
|
func RegisterMiddleware(m *macaron.Macaron) {
|
|
|
|
|
m.Use(macaron.Logger())
|
|
|
|
|
m.Use(macaron.Recovery())
|
|
|
|
|
m.Use(gzip.Gziper())
|
|
|
|
|
m.Use(macaron.Static(StaticDir))
|
|
|
|
|
m.Use(macaron.Renderer(macaron.RenderOptions{
|
|
|
|
|
Directory: "templates",
|
|
|
|
|
Extensions: []string{".html"},
|
|
|
|
|
// 模板语法分隔符,默认为 ["{{", "}}"]
|
|
|
|
|
Delims: macaron.Delims{"{{{", "}}}"},
|
|
|
|
|
// 追加的 Content-Type 头信息,默认为 "UTF-8"
|
|
|
|
|
Charset: "UTF-8",
|
|
|
|
|
// 渲染具有缩进格式的 JSON,默认为不缩进
|
|
|
|
|
IndentJSON: true,
|
|
|
|
|
// 渲染具有缩进格式的 XML,默认为不缩进
|
|
|
|
|
IndentXML: true,
|
|
|
|
|
}))
|
|
|
|
|
m.Use(session.Sessioner())
|
|
|
|
|
m.Use(csrf.Csrfer())
|
|
|
|
|
m.Use(toolbox.Toolboxer(m))
|
2017-04-07 01:13:36 +00:00
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
// 系统未安装,重定向到安装页面
|
|
|
|
|
m.Use(func(ctx *macaron.Context) {
|
|
|
|
|
installUrl := "/install"
|
|
|
|
|
if strings.HasPrefix(ctx.Req.RequestURI, installUrl) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !app.Installed {
|
|
|
|
|
ctx.Redirect(installUrl)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
// 设置模板共享变量
|
|
|
|
|
m.Use(func(ctx *macaron.Context) {
|
|
|
|
|
ctx.Data["URI"] = ctx.Req.RequestURI
|
2017-04-02 02:38:49 +00:00
|
|
|
|
})
|
2017-04-02 02:19:52 +00:00
|
|
|
|
}
|
2017-04-08 09:15:30 +00:00
|
|
|
|
|
|
|
|
|
func isAjaxRequest(ctx *macaron.Context) bool {
|
|
|
|
|
req := ctx.Req.Header.Get("X-Requested-With")
|
|
|
|
|
if req == "XMLHttpRequest" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isGetRequest(ctx *macaron.Context) bool {
|
|
|
|
|
return ctx.Req.Method == "GET"
|
|
|
|
|
}
|