2016-05-24 11:55:48 +02:00
|
|
|
package api
|
2016-01-05 00:32:53 +01:00
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-24 11:55:48 +02:00
|
|
|
"github.com/russross/blackfriday"
|
2016-01-05 00:32:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Declare all routes
|
|
|
|
func Route(r *gin.Engine) {
|
|
|
|
r.GET("/api/ping", func(c *gin.Context) {
|
|
|
|
c.String(200, "PONG")
|
|
|
|
})
|
|
|
|
|
2016-01-06 12:20:07 +01:00
|
|
|
r.NoRoute(servePublic)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
|
|
|
// set up the namespace
|
|
|
|
api := r.Group("/api")
|
|
|
|
|
2016-03-19 00:23:03 +01:00
|
|
|
func(api *gin.RouterGroup) {
|
2016-04-09 21:09:57 +02:00
|
|
|
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
|
|
|
|
2016-04-30 14:28:47 +02:00
|
|
|
api.Use(authentication)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2016-04-02 14:40:07 +02:00
|
|
|
api.GET("/ws", sockets.Handler)
|
|
|
|
|
2016-05-17 17:18:26 +02:00
|
|
|
api.GET("/info", getSystemInfo)
|
|
|
|
api.GET("/upgrade", checkUpgrade)
|
|
|
|
api.POST("/upgrade", doUpgrade)
|
|
|
|
|
2016-04-09 21:09:57 +02:00
|
|
|
func(api *gin.RouterGroup) {
|
|
|
|
api.GET("", getUser)
|
|
|
|
// api.PUT("/user", misc.UpdateUser)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
api.GET("/projects", projects.GetProjects)
|
|
|
|
api.POST("/projects", projects.AddProject)
|
2016-04-16 21:42:57 +02:00
|
|
|
api.GET("/events", getEvents)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2016-04-10 20:03:12 +02:00
|
|
|
api.GET("/users", getUsers)
|
|
|
|
api.POST("/users", addUser)
|
2016-05-23 21:29:38 +02:00
|
|
|
api.GET("/users/:user_id", getUserMiddleware, getUser)
|
2016-04-10 20:03:12 +02:00
|
|
|
api.PUT("/users/:user_id", getUserMiddleware, updateUser)
|
|
|
|
api.POST("/users/:user_id/password", getUserMiddleware, updateUserPassword)
|
2016-05-23 21:29:38 +02:00
|
|
|
api.DELETE("/users/:user_id", getUserMiddleware, deleteUser)
|
2016-04-10 20:03:12 +02:00
|
|
|
|
2016-04-02 14:40:07 +02:00
|
|
|
func(api *gin.RouterGroup) {
|
|
|
|
api.Use(projects.ProjectMiddleware)
|
|
|
|
|
|
|
|
api.GET("", projects.GetProject)
|
|
|
|
|
2016-04-16 21:42:57 +02:00
|
|
|
api.GET("/events", getEvents)
|
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
api.GET("/users", projects.GetUsers)
|
|
|
|
api.POST("/users", projects.AddUser)
|
|
|
|
api.POST("/users/:user_id/admin", projects.UserMiddleware, projects.MakeUserAdmin)
|
|
|
|
api.DELETE("/users/:user_id/admin", projects.UserMiddleware, projects.MakeUserAdmin)
|
|
|
|
api.DELETE("/users/:user_id", projects.UserMiddleware, projects.RemoveUser)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
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)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
api.GET("/repositories", projects.GetRepositories)
|
|
|
|
api.POST("/repositories", projects.AddRepository)
|
2016-06-14 04:37:40 +02:00
|
|
|
api.PUT("/repositories/:repository_id", projects.RepositoryMiddleware, projects.UpdateRepository)
|
2016-04-04 15:44:34 +02:00
|
|
|
api.DELETE("/repositories/:repository_id", projects.RepositoryMiddleware, projects.RemoveRepository)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
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)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
api.GET("/environment", projects.GetEnvironment)
|
|
|
|
api.POST("/environment", projects.AddEnvironment)
|
2016-04-13 18:09:44 +02:00
|
|
|
api.PUT("/environment/:environment_id", projects.EnvironmentMiddleware, projects.UpdateEnvironment)
|
2016-04-09 21:38:14 +02:00
|
|
|
api.DELETE("/environment/:environment_id", projects.EnvironmentMiddleware, projects.RemoveEnvironment)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
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)
|
|
|
|
|
2016-04-17 02:20:23 +02:00
|
|
|
api.GET("/tasks", tasks.GetAll)
|
2016-04-04 15:44:34 +02:00
|
|
|
api.POST("/tasks", tasks.AddTask)
|
2016-04-17 20:01:51 +02:00
|
|
|
api.GET("/tasks/:task_id/output", tasks.GetTaskMiddleware, tasks.GetTaskOutput)
|
2016-04-02 14:40:07 +02:00
|
|
|
}(api.Group("/project/:project_id"))
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func servePublic(c *gin.Context) {
|
2016-01-06 12:20:07 +01:00
|
|
|
path := c.Request.URL.Path
|
|
|
|
|
2016-03-19 00:23:03 +01:00
|
|
|
if strings.HasPrefix(path, "/api") {
|
|
|
|
c.Next()
|
|
|
|
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 {
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-18 23:03:28 +01:00
|
|
|
path = "/public/html/index.html"
|
2016-01-06 12:20:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
path = strings.Replace(path, "/", "", 1)
|
|
|
|
split := strings.Split(path, ".")
|
|
|
|
suffix := split[len(split)-1]
|
|
|
|
|
|
|
|
res, err := util.Asset(path)
|
|
|
|
if err != nil {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-01-06 12:20:07 +01:00
|
|
|
c.Writer.Header().Set("content-type", contentType)
|
|
|
|
c.String(200, string(res))
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|
2016-05-17 17:18:26 +02:00
|
|
|
|
|
|
|
func getSystemInfo(c *gin.Context) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkUpgrade(c *gin.Context) {
|
2016-05-24 11:55:48 +02:00
|
|
|
if err := util.CheckUpdate(util.Version); err != nil {
|
2016-05-17 17:18:26 +02:00
|
|
|
c.JSON(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:55:48 +02:00
|
|
|
if util.UpdateAvailable != nil {
|
2016-05-17 17:18:26 +02:00
|
|
|
getSystemInfo(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.AbortWithStatus(204)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doUpgrade(c *gin.Context) {
|
2016-05-24 11:55:48 +02:00
|
|
|
util.DoUpgrade(util.Version)
|
2016-05-17 17:18:26 +02:00
|
|
|
}
|