Semaphore/routes/router.go

193 lines
5.2 KiB
Go
Raw Normal View History

2016-01-05 00:32:53 +01:00
package routes
import (
2016-01-06 12:20:07 +01:00
"strings"
2016-03-16 22:49:43 +01:00
2016-05-17 17:18:26 +02:00
"github.com/russross/blackfriday"
2016-04-02 14:40:07 +02:00
"github.com/ansible-semaphore/semaphore/routes/projects"
"github.com/ansible-semaphore/semaphore/routes/sockets"
2016-04-04 15:44:34 +02:00
"github.com/ansible-semaphore/semaphore/routes/tasks"
2016-05-17 17:18:26 +02:00
"github.com/ansible-semaphore/semaphore/upgrade"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
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) {
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
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)
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)
api.GET("/events", getEvents)
2016-04-02 14:40:07 +02: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)
2016-04-02 14:40:07 +02:00
func(api *gin.RouterGroup) {
api.Use(projects.ProjectMiddleware)
api.GET("", projects.GetProject)
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)
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)
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)
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,
"update": upgrade.UpdateAvailable,
"config": map[string]string{
"dbHost": util.Config.MySQL.Hostname,
"dbName": util.Config.MySQL.DbName,
"dbUser": util.Config.MySQL.Username,
"path": util.Config.TmpPath,
"cmdPath": upgrade.FindSemaphore(),
2016-05-17 17:18:26 +02:00
},
}
if upgrade.UpdateAvailable != nil {
body["updateBody"] = string(blackfriday.MarkdownCommon([]byte(*upgrade.UpdateAvailable.Body)))
}
c.JSON(200, body)
}
func checkUpgrade(c *gin.Context) {
if err := upgrade.CheckUpdate(util.Version); err != nil {
c.JSON(500, err)
return
}
if upgrade.UpdateAvailable != nil {
getSystemInfo(c)
return
}
c.AbortWithStatus(204)
}
func doUpgrade(c *gin.Context) {
upgrade.Upgrade(util.Version)
}