2017-01-11 08:12:37 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path"
|
2017-08-30 06:56:50 +00:00
|
|
|
"strings"
|
2017-01-11 08:12:37 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2017-05-12 06:48:24 +00:00
|
|
|
"github.com/shunfei/cronsun"
|
2017-01-11 08:12:37 +00:00
|
|
|
)
|
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
func GetVersion(ctx *Context) {
|
|
|
|
outJSON(ctx.W, cronsun.Version)
|
2017-04-19 04:09:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
func initRouters() (s *http.Server, err error) {
|
2017-01-11 08:12:37 +00:00
|
|
|
jobHandler := &Job{}
|
|
|
|
nodeHandler := &Node{}
|
2017-02-07 10:05:59 +00:00
|
|
|
jobLogHandler := &JobLog{}
|
2017-02-20 09:57:22 +00:00
|
|
|
infoHandler := &Info{}
|
2017-03-16 02:19:57 +00:00
|
|
|
configHandler := &Configuration{}
|
2017-06-29 07:15:33 +00:00
|
|
|
authHandler := &Authentication{}
|
|
|
|
adminHandler := &Administrator{}
|
2017-01-11 08:12:37 +00:00
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
subrouter := r.PathPrefix("/v1").Subrouter()
|
2017-06-29 07:15:33 +00:00
|
|
|
subrouter.Handle("/version", NewBaseHandler(GetVersion)).Methods("GET")
|
|
|
|
|
|
|
|
h := NewBaseHandler(authHandler.GetAuthSession)
|
|
|
|
subrouter.Handle("/session", h).Methods("GET")
|
|
|
|
h = NewBaseHandler(authHandler.DeleteAuthSession)
|
|
|
|
subrouter.Handle("/session", h).Methods("DELETE")
|
|
|
|
|
|
|
|
h = NewBaseHandler(authHandler.SetPassword)
|
|
|
|
subrouter.Handle("/user/setpwd", h).Methods("POST")
|
|
|
|
|
|
|
|
h = NewAdminAuthHandler(adminHandler.GetAccount)
|
|
|
|
subrouter.Handle("/admin/account/{email}", h).Methods("GET")
|
|
|
|
h = NewAdminAuthHandler(adminHandler.GetAccountList)
|
|
|
|
subrouter.Handle("/admin/accounts", h).Methods("GET")
|
|
|
|
h = NewAdminAuthHandler(adminHandler.AddAccount)
|
|
|
|
subrouter.Handle("/admin/account", h).Methods("PUT")
|
|
|
|
h = NewAdminAuthHandler(adminHandler.UpdateAccount)
|
|
|
|
subrouter.Handle("/admin/account", h).Methods("POSt")
|
2017-01-11 08:12:37 +00:00
|
|
|
|
2017-02-16 03:57:58 +00:00
|
|
|
// get job list
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetList, cronsun.Reporter)
|
2017-02-16 03:57:58 +00:00
|
|
|
subrouter.Handle("/jobs", h).Methods("GET")
|
2017-01-16 06:30:55 +00:00
|
|
|
// get a job group list
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetGroups, cronsun.Reporter)
|
2017-01-11 08:12:37 +00:00
|
|
|
subrouter.Handle("/job/groups", h).Methods("GET")
|
2017-01-16 06:30:55 +00:00
|
|
|
// create/update a job
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.UpdateJob, cronsun.Developer)
|
2017-01-11 08:12:37 +00:00
|
|
|
subrouter.Handle("/job", h).Methods("PUT")
|
2017-01-22 07:18:08 +00:00
|
|
|
// pause/start
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.ChangeJobStatus, cronsun.Developer)
|
2017-01-22 07:18:08 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}", h).Methods("POST")
|
2017-12-14 08:38:10 +00:00
|
|
|
// batch pause/start
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.BatchChangeJobStatus, cronsun.Developer)
|
2017-12-14 08:38:10 +00:00
|
|
|
subrouter.Handle("/jobs/{op}", h).Methods("POST")
|
2017-01-16 06:30:55 +00:00
|
|
|
// get a job
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetJob, cronsun.Reporter)
|
2017-01-16 06:30:55 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}", h).Methods("GET")
|
|
|
|
// remove a job
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.DeleteJob, cronsun.Developer)
|
2017-01-16 06:30:55 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE")
|
2017-01-11 08:12:37 +00:00
|
|
|
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetJobNodes, cronsun.Reporter)
|
2017-03-10 08:36:44 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}/nodes", h).Methods("GET")
|
|
|
|
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.JobExecute, cronsun.Developer)
|
2017-03-10 08:36:44 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}/execute", h).Methods("PUT")
|
|
|
|
|
2017-03-10 03:06:40 +00:00
|
|
|
// query executing job
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetExecutingJob, cronsun.Reporter)
|
2017-03-10 03:06:40 +00:00
|
|
|
subrouter.Handle("/job/executing", h).Methods("GET")
|
|
|
|
|
2017-02-07 10:05:59 +00:00
|
|
|
// get job log list
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobLogHandler.GetList, cronsun.Reporter)
|
2017-02-07 10:05:59 +00:00
|
|
|
subrouter.Handle("/logs", h).Methods("GET")
|
|
|
|
// get job log
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(jobLogHandler.GetDetail, cronsun.Developer)
|
2017-02-07 10:05:59 +00:00
|
|
|
subrouter.Handle("/log/{id}", h).Methods("GET")
|
|
|
|
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.GetNodes, cronsun.Developer)
|
2017-02-14 07:15:00 +00:00
|
|
|
subrouter.Handle("/nodes", h).Methods("GET")
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.DeleteNode, cronsun.Developer)
|
2017-10-18 06:44:51 +00:00
|
|
|
subrouter.Handle("/node/{ip}", h).Methods("DELETE")
|
2017-01-12 08:35:30 +00:00
|
|
|
// get node group list
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.GetGroups, cronsun.Reporter)
|
2017-02-17 06:20:58 +00:00
|
|
|
subrouter.Handle("/node/groups", h).Methods("GET")
|
2017-01-12 08:35:30 +00:00
|
|
|
// get a node group by group id
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.GetGroupByGroupId, cronsun.Reporter)
|
2017-02-17 06:20:58 +00:00
|
|
|
subrouter.Handle("/node/group/{id}", h).Methods("GET")
|
2017-01-12 08:35:30 +00:00
|
|
|
// create/update a node group
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.UpdateGroup, cronsun.Developer)
|
2017-02-17 06:20:58 +00:00
|
|
|
subrouter.Handle("/node/group", h).Methods("PUT")
|
2017-01-12 08:35:30 +00:00
|
|
|
// delete a node group
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.DeleteGroup, cronsun.Developer)
|
2017-02-17 06:20:58 +00:00
|
|
|
subrouter.Handle("/node/group/{id}", h).Methods("DELETE")
|
2017-01-11 08:12:37 +00:00
|
|
|
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(infoHandler.Overview, cronsun.Reporter)
|
2017-02-20 09:57:22 +00:00
|
|
|
subrouter.Handle("/info/overview", h).Methods("GET")
|
|
|
|
|
2018-05-05 02:17:05 +00:00
|
|
|
h = NewAuthHandler(configHandler.Configuratios, cronsun.Reporter)
|
2017-03-09 03:39:06 +00:00
|
|
|
subrouter.Handle("/configurations", h).Methods("GET")
|
|
|
|
|
2017-08-30 06:56:50 +00:00
|
|
|
r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui/", newEmbeddedFileServer("", "index.html")))
|
2017-11-29 02:14:11 +00:00
|
|
|
r.NotFoundHandler = NewBaseHandler(notFoundHandler)
|
2017-01-11 08:12:37 +00:00
|
|
|
|
|
|
|
s = &http.Server{
|
|
|
|
Handler: r,
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2017-08-30 06:56:50 +00:00
|
|
|
|
|
|
|
type embeddedFileServer struct {
|
|
|
|
Prefix string
|
|
|
|
IndexFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEmbeddedFileServer(prefix, index string) *embeddedFileServer {
|
|
|
|
index = strings.TrimLeft(index, "/")
|
|
|
|
return &embeddedFileServer{Prefix: prefix, IndexFile: index}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *embeddedFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fp := path.Clean(s.Prefix + r.URL.Path)
|
|
|
|
if fp == "." {
|
|
|
|
fp = ""
|
|
|
|
} else {
|
|
|
|
fp = strings.TrimLeft(fp, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := Asset(fp)
|
|
|
|
if err == nil {
|
|
|
|
w.Write(b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fp) > 0 {
|
|
|
|
fp += "/"
|
|
|
|
}
|
|
|
|
fp += s.IndexFile
|
|
|
|
|
2018-03-09 07:25:10 +00:00
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
w.Header().Set("Expires", "0")
|
|
|
|
|
2017-08-30 06:56:50 +00:00
|
|
|
b, err = Asset(fp)
|
|
|
|
if err == nil {
|
|
|
|
w.Write(b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:14:11 +00:00
|
|
|
_notFoundHandler(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func notFoundHandler(c *Context) {
|
|
|
|
_notFoundHandler(c.W, c.R)
|
|
|
|
}
|
|
|
|
|
|
|
|
func _notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
const html = `<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>404 page not found</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
The page you are looking for is not found. Redirect to <a href="/ui/">Dashboard</a> after <span id="s">5</span> seconds.
|
|
|
|
</body>
|
|
|
|
<script type="text/javascript">
|
|
|
|
var s = 5;
|
|
|
|
setInterval(function(){
|
|
|
|
s--;
|
|
|
|
document.getElementById('s').innerText = s;
|
|
|
|
if (s === 0) location.href = '/ui/';
|
|
|
|
}, 1000);
|
|
|
|
</script>
|
|
|
|
</html>`
|
|
|
|
|
|
|
|
if strings.HasPrefix(strings.TrimLeft(r.URL.Path, "/"), "v1") {
|
|
|
|
outJSONWithCode(w, http.StatusNotFound, "Api not found.")
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte(html))
|
|
|
|
}
|
2017-08-30 06:56:50 +00:00
|
|
|
}
|