cronsun/web/routers.go

96 lines
2.9 KiB
Go
Raw Normal View History

2017-01-11 08:12:37 +00:00
package web
import (
"net/http"
"path"
"github.com/gorilla/mux"
2017-05-09 10:27:32 +00:00
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models"
2017-01-11 08:12:37 +00:00
)
2017-04-19 04:09:11 +00:00
func GetVersion(w http.ResponseWriter, r *http.Request) {
outJSON(w, models.Version)
}
2017-01-11 08:12:37 +00:00
func InitRouters() (s *http.Server, err error) {
jobHandler := &Job{}
nodeHandler := &Node{}
jobLogHandler := &JobLog{}
2017-02-20 09:57:22 +00:00
infoHandler := &Info{}
2017-03-16 02:19:57 +00:00
configHandler := &Configuration{}
2017-01-11 08:12:37 +00:00
r := mux.NewRouter()
subrouter := r.PathPrefix("/v1").Subrouter()
2017-04-19 04:09:11 +00:00
subrouter.Handle("/version", BaseHandler{Handle: GetVersion}).Methods("GET")
2017-01-11 08:12:37 +00:00
// get job list
h := BaseHandler{Handle: jobHandler.GetList}
subrouter.Handle("/jobs", h).Methods("GET")
2017-01-16 06:30:55 +00:00
// get a job group list
h = BaseHandler{Handle: 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
h = BaseHandler{Handle: jobHandler.UpdateJob}
2017-01-11 08:12:37 +00:00
subrouter.Handle("/job", h).Methods("PUT")
// pause/start
h = BaseHandler{Handle: jobHandler.ChangeJobStatus}
subrouter.Handle("/job/{group}-{id}", h).Methods("POST")
2017-01-16 06:30:55 +00:00
// get a job
h = BaseHandler{Handle: jobHandler.GetJob}
subrouter.Handle("/job/{group}-{id}", h).Methods("GET")
// remove a job
h = BaseHandler{Handle: jobHandler.DeleteJob}
subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE")
2017-01-11 08:12:37 +00:00
2017-03-10 08:36:44 +00:00
h = BaseHandler{Handle: jobHandler.GetJobNodes}
subrouter.Handle("/job/{group}-{id}/nodes", h).Methods("GET")
h = BaseHandler{Handle: jobHandler.JobExecute}
subrouter.Handle("/job/{group}-{id}/execute", h).Methods("PUT")
2017-03-10 03:06:40 +00:00
// query executing job
h = BaseHandler{Handle: jobHandler.GetExecutingJob}
subrouter.Handle("/job/executing", h).Methods("GET")
// get job log list
h = BaseHandler{Handle: jobLogHandler.GetList}
subrouter.Handle("/logs", h).Methods("GET")
// get job log
h = BaseHandler{Handle: jobLogHandler.GetDetail}
subrouter.Handle("/log/{id}", h).Methods("GET")
2017-02-14 07:15:00 +00:00
h = BaseHandler{Handle: nodeHandler.GetNodes}
subrouter.Handle("/nodes", h).Methods("GET")
2017-01-12 08:35:30 +00:00
// get node group list
2017-01-11 08:12:37 +00:00
h = BaseHandler{Handle: 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
h = BaseHandler{Handle: 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
h = BaseHandler{Handle: 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
h = BaseHandler{Handle: 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-02-20 09:57:22 +00:00
h = BaseHandler{Handle: infoHandler.Overview}
subrouter.Handle("/info/overview", h).Methods("GET")
h = BaseHandler{Handle: configHandler.Configuratios}
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
}