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
|
|
|
|
|
|
|
"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")
|
|
|
|
|
|
|
|
api.Use(authentication)
|
|
|
|
|
|
|
|
// serve /api/auth
|
|
|
|
|
|
|
|
api.Use(MustAuthenticate)
|
|
|
|
|
2016-03-18 23:03:28 +01:00
|
|
|
api.GET("/user", getUser)
|
2016-01-05 00:32:53 +01:00
|
|
|
// api.PUT("/user", misc.UpdateUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
func servePublic(c *gin.Context) {
|
2016-01-06 12:20:07 +01:00
|
|
|
path := c.Request.URL.Path
|
|
|
|
|
|
|
|
if !strings.HasPrefix(path, "/public") {
|
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-03-18 23:03:28 +01:00
|
|
|
|
|
|
|
func getUser(c *gin.Context) {
|
|
|
|
c.JSON(200, c.MustGet("user"))
|
|
|
|
}
|