Semaphore/api/router.go

213 lines
6.3 KiB
Go
Raw Normal View History

2016-05-24 11:55:48 +02:00
package api
2016-01-05 00:32:53 +01:00
import (
2017-02-23 00:21:49 +01:00
"net/http"
2016-01-06 12:20:07 +01:00
"strings"
2016-03-16 22:49:43 +01:00
2016-05-24 11:55:48 +02:00
"github.com/ansible-semaphore/semaphore/api/projects"
"github.com/ansible-semaphore/semaphore/api/sockets"
"github.com/ansible-semaphore/semaphore/api/tasks"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/util"
2017-02-23 00:21:49 +01:00
"github.com/castawaylabs/mulekick"
"github.com/gobuffalo/packr"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/mux"
2016-05-24 11:55:48 +02:00
"github.com/russross/blackfriday"
2016-01-05 00:32:53 +01:00
)
var publicAssets = packr.NewBox("../web/public")
2016-01-05 00:32:53 +01:00
// Declare all routes
2017-02-23 00:21:49 +01:00
func Route() mulekick.Router {
r := mulekick.New(mux.NewRouter(), mulekick.CorsMiddleware)
r.NotFoundHandler = http.HandlerFunc(servePublic)
2016-01-05 00:32:53 +01:00
2017-02-23 00:21:49 +01:00
r.Get("/api/ping", mulekick.PongHandler)
2016-01-05 00:32:53 +01:00
// set up the namespace
api := r.Group("/api")
2017-02-23 00:21:49 +01:00
func(api mulekick.Router) {
api.Post("/login", login)
api.Post("/logout", logout)
2016-03-19 00:23:03 +01:00
}(api.Group("/auth"))
2016-01-05 00:32:53 +01:00
api.Use(authentication)
2016-01-05 00:32:53 +01:00
2017-02-23 00:21:49 +01:00
api.Get("/ws", sockets.Handler)
2016-04-02 14:40:07 +02:00
2017-02-23 00:21:49 +01:00
api.Get("/info", getSystemInfo)
api.Get("/upgrade", checkUpgrade)
api.Post("/upgrade", doUpgrade)
2016-05-17 17:18:26 +02:00
2017-02-23 00:21:49 +01:00
func(api mulekick.Router) {
api.Get("", getUser)
// api.PUT("/user", misc.UpdateUser)
2017-02-23 00:21:49 +01:00
api.Get("/tokens", getAPITokens)
api.Post("/tokens", createAPIToken)
api.Delete("/tokens/:token_id", expireAPIToken)
}(api.Group("/user"))
2016-04-02 14:40:07 +02:00
2017-02-23 00:21:49 +01:00
api.Get("/projects", projects.GetProjects)
api.Post("/projects", projects.AddProject)
api.Get("/events", getAllEvents)
api.Get("/events/last", getLastEvents)
2016-04-02 14:40:07 +02:00
2017-02-23 00:21:49 +01:00
api.Get("/users", getUsers)
api.Post("/users", addUser)
api.Get("/users/{user_id}", getUserMiddleware, getUser)
api.Put("/users/{user_id}", getUserMiddleware, updateUser)
api.Post("/users/{user_id}/password", getUserMiddleware, updateUserPassword)
api.Delete("/users/{user_id}", getUserMiddleware, deleteUser)
2017-02-23 00:21:49 +01:00
func(api mulekick.Router) {
2016-04-02 14:40:07 +02:00
api.Use(projects.ProjectMiddleware)
2017-02-23 00:21:49 +01:00
api.Get("", projects.GetProject)
api.Put("", projects.MustBeAdmin, projects.UpdateProject)
api.Delete("", projects.MustBeAdmin, projects.DeleteProject)
api.Get("/events", getAllEvents)
api.Get("/events/last", getLastEvents)
2017-02-23 00:21:49 +01:00
api.Get("/users", projects.GetUsers)
api.Post("/users", projects.MustBeAdmin, projects.AddUser)
api.Post("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin)
api.Delete("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin)
api.Delete("/users/{user_id}", projects.MustBeAdmin, projects.UserMiddleware, projects.RemoveUser)
api.Get("/keys", projects.GetKeys)
api.Post("/keys", projects.AddKey)
api.Put("/keys/{key_id}", projects.KeyMiddleware, projects.UpdateKey)
api.Delete("/keys/{key_id}", projects.KeyMiddleware, projects.RemoveKey)
api.Get("/repositories", projects.GetRepositories)
api.Post("/repositories", projects.AddRepository)
api.Put("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.UpdateRepository)
api.Delete("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.RemoveRepository)
api.Get("/inventory", projects.GetInventory)
api.Post("/inventory", projects.AddInventory)
api.Put("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.UpdateInventory)
api.Delete("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.RemoveInventory)
api.Get("/environment", projects.GetEnvironment)
api.Post("/environment", projects.AddEnvironment)
api.Put("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.UpdateEnvironment)
api.Delete("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.RemoveEnvironment)
api.Get("/templates", projects.GetTemplates)
api.Post("/templates", projects.AddTemplate)
api.Put("/templates/{template_id}", projects.TemplatesMiddleware, projects.UpdateTemplate)
api.Delete("/templates/{template_id}", projects.TemplatesMiddleware, projects.RemoveTemplate)
2017-07-25 06:44:09 +02:00
api.Get("/tasks", tasks.GetAllTasks)
api.Get("/tasks/last", tasks.GetLastTasks)
2017-02-23 00:21:49 +01:00
api.Post("/tasks", tasks.AddTask)
api.Get("/tasks/{task_id}/output", tasks.GetTaskMiddleware, tasks.GetTaskOutput)
api.Get("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.GetTask)
2017-02-23 00:21:49 +01:00
api.Delete("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.RemoveTask)
}(api.Group("/project/{project_id}"))
return r
2016-01-05 00:32:53 +01:00
}
2017-02-22 23:17:36 +01:00
func servePublic(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
2016-01-06 12:20:07 +01:00
2016-03-19 00:23:03 +01:00
if strings.HasPrefix(path, "/api") {
2017-02-23 00:21:49 +01:00
mulekick.NotFoundHandler(w, r)
2016-03-19 00:23:03 +01:00
return
}
2016-01-06 12:20:07 +01:00
if !strings.HasPrefix(path, "/public") {
2016-04-04 15:44:34 +02:00
if len(strings.Split(path, ".")) > 1 {
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNotFound)
2016-04-04 15:44:34 +02:00
return
}
path = "/html/index.html"
2016-01-06 12:20:07 +01:00
}
path = strings.Replace(path, "/public/", "", 1)
2016-01-06 12:20:07 +01:00
split := strings.Split(path, ".")
suffix := split[len(split)-1]
res, err := publicAssets.MustBytes(path)
2016-01-06 12:20:07 +01:00
if err != nil {
2017-02-23 00:21:49 +01:00
mulekick.NotFoundHandler(w, r)
2016-01-06 12:20:07 +01:00
return
}
2017-05-20 16:14:36 +02:00
// replace base path
if util.WebHostURL != nil && path == "html/index.html" {
2017-05-20 16:14:36 +02:00
res = []byte(strings.Replace(string(res),
"<base href=\"/\">",
"<base href=\""+util.WebHostURL.String()+"\">",
1))
}
2016-01-06 12:20:07 +01:00
contentType := "text/plain"
switch suffix {
case "png":
contentType = "image/png"
case "jpg", "jpeg":
contentType = "image/jpeg"
case "gif":
contentType = "image/gif"
case "js":
contentType = "application/javascript"
case "css":
contentType = "text/css"
case "woff":
contentType = "application/x-font-woff"
case "ttf":
contentType = "application/x-font-ttf"
case "otf":
contentType = "application/x-font-otf"
case "html":
contentType = "text/html"
}
2016-01-05 00:32:53 +01:00
2017-02-23 00:21:49 +01:00
w.Header().Set("content-type", contentType)
w.Write(res)
2016-01-05 00:32:53 +01:00
}
2016-05-17 17:18:26 +02:00
2017-02-22 23:17:36 +01:00
func getSystemInfo(w http.ResponseWriter, r *http.Request) {
2016-05-17 17:18:26 +02:00
body := map[string]interface{}{
"version": util.Version,
2016-05-24 11:55:48 +02:00
"update": util.UpdateAvailable,
2016-05-17 17:18:26 +02:00
"config": map[string]string{
"dbHost": util.Config.MySQL.Hostname,
"dbName": util.Config.MySQL.DbName,
"dbUser": util.Config.MySQL.Username,
"path": util.Config.TmpPath,
2016-05-24 11:55:48 +02:00
"cmdPath": util.FindSemaphore(),
2016-05-17 17:18:26 +02:00
},
}
2016-05-24 11:55:48 +02:00
if util.UpdateAvailable != nil {
body["updateBody"] = string(blackfriday.MarkdownCommon([]byte(*util.UpdateAvailable.Body)))
2016-05-17 17:18:26 +02:00
}
2017-02-23 00:21:49 +01:00
mulekick.WriteJSON(w, http.StatusOK, body)
2016-05-17 17:18:26 +02:00
}
2017-02-22 23:17:36 +01:00
func checkUpgrade(w http.ResponseWriter, r *http.Request) {
2016-05-24 11:55:48 +02:00
if err := util.CheckUpdate(util.Version); err != nil {
2017-02-23 00:21:49 +01:00
mulekick.WriteJSON(w, 500, err)
2016-05-17 17:18:26 +02:00
return
}
2016-05-24 11:55:48 +02:00
if util.UpdateAvailable != nil {
2017-02-23 00:21:49 +01:00
getSystemInfo(w, r)
2016-05-17 17:18:26 +02:00
return
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
2016-05-17 17:18:26 +02:00
}
2017-02-22 23:17:36 +01:00
func doUpgrade(w http.ResponseWriter, r *http.Request) {
2016-05-24 11:55:48 +02:00
util.DoUpgrade(util.Version)
2016-05-17 17:18:26 +02:00
}