2017-01-11 08:12:37 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2017-05-12 06:48:24 +00:00
|
|
|
"github.com/shunfei/cronsun"
|
2017-05-12 07:38:50 +00:00
|
|
|
"github.com/shunfei/cronsun/conf"
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetList)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetGroups)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.UpdateJob)
|
2017-01-11 08:12:37 +00:00
|
|
|
subrouter.Handle("/job", h).Methods("PUT")
|
2017-01-22 07:18:08 +00:00
|
|
|
// pause/start
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.ChangeJobStatus)
|
2017-01-22 07:18:08 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}", h).Methods("POST")
|
2017-01-16 06:30:55 +00:00
|
|
|
// get a job
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetJob)
|
2017-01-16 06:30:55 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}", h).Methods("GET")
|
|
|
|
// remove a job
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.DeleteJob)
|
2017-01-16 06:30:55 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE")
|
2017-01-11 08:12:37 +00:00
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetJobNodes)
|
2017-03-10 08:36:44 +00:00
|
|
|
subrouter.Handle("/job/{group}-{id}/nodes", h).Methods("GET")
|
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.JobExecute)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobHandler.GetExecutingJob)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobLogHandler.GetList)
|
2017-02-07 10:05:59 +00:00
|
|
|
subrouter.Handle("/logs", h).Methods("GET")
|
|
|
|
// get job log
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(jobLogHandler.GetDetail)
|
2017-02-07 10:05:59 +00:00
|
|
|
subrouter.Handle("/log/{id}", h).Methods("GET")
|
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.GetNodes)
|
2017-02-14 07:15:00 +00:00
|
|
|
subrouter.Handle("/nodes", h).Methods("GET")
|
2017-01-12 08:35:30 +00:00
|
|
|
// get node group list
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.GetGroups)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.GetGroupByGroupId)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.UpdateGroup)
|
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
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(nodeHandler.DeleteGroup)
|
2017-02-17 06:20:58 +00:00
|
|
|
subrouter.Handle("/node/group/{id}", h).Methods("DELETE")
|
2017-01-11 08:12:37 +00:00
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(infoHandler.Overview)
|
2017-02-20 09:57:22 +00:00
|
|
|
subrouter.Handle("/info/overview", h).Methods("GET")
|
|
|
|
|
2017-06-29 07:15:33 +00:00
|
|
|
h = NewAuthHandler(configHandler.Configuratios)
|
2017-03-09 03:39:06 +00:00
|
|
|
subrouter.Handle("/configurations", h).Methods("GET")
|
|
|
|
|
2017-01-11 08:12:37 +00:00
|
|
|
uidir := conf.Config.Web.UIDir
|
|
|
|
if len(uidir) == 0 {
|
2017-02-17 06:20:58 +00:00
|
|
|
uidir = path.Join("web", "ui", "dist")
|
2017-01-11 08:12:37 +00:00
|
|
|
}
|
|
|
|
r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui/", http.FileServer(http.Dir(uidir))))
|
|
|
|
|
|
|
|
s = &http.Server{
|
|
|
|
Handler: r,
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|