harness-drone/router/router.go

217 lines
5.5 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package router
import (
"net/http"
2016-04-20 01:37:53 +00:00
"os"
2015-09-30 01:21:17 +00:00
"strings"
"github.com/gin-gonic/gin"
"github.com/drone/drone/api"
2016-04-22 00:10:19 +00:00
"github.com/drone/drone/router/middleware"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/router/middleware/header"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/router/middleware/token"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/static"
"github.com/drone/drone/template"
2016-03-31 06:27:53 +00:00
"github.com/drone/drone/web"
2015-09-30 01:21:17 +00:00
)
2016-04-22 00:10:19 +00:00
func Load(middlewares ...gin.HandlerFunc) http.Handler {
2016-04-11 23:03:54 +00:00
e := gin.New()
e.Use(gin.Recovery())
2015-09-30 01:21:17 +00:00
e.SetHTMLTemplate(template.Load())
e.StaticFS("/static", static.FileSystem())
e.Use(header.NoCache)
e.Use(header.Options)
e.Use(header.Secure)
2016-04-22 00:10:19 +00:00
e.Use(middlewares...)
2015-09-30 01:21:17 +00:00
e.Use(session.SetUser())
e.Use(token.Refresh)
2015-09-30 01:21:17 +00:00
2016-03-31 06:27:53 +00:00
e.GET("/", web.ShowIndex)
e.GET("/repos", web.ShowAllRepos)
2016-03-31 06:27:53 +00:00
e.GET("/login", web.ShowLogin)
e.GET("/login/form", web.ShowLoginForm)
e.GET("/logout", web.GetLogout)
2015-09-30 01:21:17 +00:00
settings := e.Group("/settings")
{
settings.Use(session.MustUser())
2016-03-31 06:27:53 +00:00
settings.GET("/profile", web.ShowUser)
settings.GET("/people", session.MustAdmin(), web.ShowUsers)
settings.GET("/nodes", session.MustAdmin(), web.ShowNodes)
2015-09-30 01:21:17 +00:00
}
repo := e.Group("/repos/:owner/:name")
{
repo.Use(session.SetRepo())
repo.Use(session.SetPerm())
repo.Use(session.MustPull)
2016-03-31 06:27:53 +00:00
repo.GET("", web.ShowRepo)
repo.GET("/builds/:number", web.ShowBuild)
repo.GET("/builds/:number/:job", web.ShowBuild)
2015-09-30 01:21:17 +00:00
repo_settings := repo.Group("/settings")
{
2016-03-31 06:27:53 +00:00
repo_settings.GET("", session.MustPush, web.ShowRepoConf)
repo_settings.GET("/encrypt", session.MustPush, web.ShowRepoEncrypt)
repo_settings.GET("/badges", web.ShowRepoBadges)
2015-09-30 01:21:17 +00:00
}
}
user := e.Group("/api/user")
{
user.Use(session.MustUser())
user.GET("", api.GetSelf)
user.GET("/feed", api.GetFeed)
user.GET("/repos", api.GetRepos)
user.GET("/repos/remote", api.GetRemoteRepos)
user.POST("/token", api.PostToken)
2016-04-09 00:16:45 +00:00
user.DELETE("/token", api.DeleteToken)
2015-09-30 01:21:17 +00:00
}
users := e.Group("/api/users")
{
users.Use(session.MustAdmin())
users.GET("", api.GetUsers)
users.POST("", api.PostUser)
users.GET("/:login", api.GetUser)
users.PATCH("/:login", api.PatchUser)
users.DELETE("/:login", api.DeleteUser)
2015-09-30 01:21:17 +00:00
}
nodes := e.Group("/api/nodes")
{
nodes.Use(session.MustAdmin())
nodes.GET("", api.GetNodes)
nodes.POST("", api.PostNode)
nodes.DELETE("/:node", api.DeleteNode)
2015-09-30 01:21:17 +00:00
}
repos := e.Group("/api/repos/:owner/:name")
{
repos.POST("", api.PostRepo)
2015-09-30 01:21:17 +00:00
repo := repos.Group("")
{
repo.Use(session.SetRepo())
repo.Use(session.SetPerm())
repo.Use(session.MustPull)
repo.GET("", api.GetRepo)
repo.GET("/key", api.GetRepoKey)
repo.POST("/key", api.PostRepoKey)
2016-03-31 06:24:47 +00:00
repo.GET("/builds", api.GetBuilds)
repo.GET("/builds/:number", api.GetBuild)
repo.GET("/logs/:number/:job", api.GetBuildLogs)
repo.POST("/sign", session.MustPush, api.Sign)
repo.POST("/secrets", session.MustPush, api.PostSecret)
repo.DELETE("/secrets/:secret", session.MustPush, api.DeleteSecret)
2015-09-30 01:21:17 +00:00
// requires authenticated user
repo.POST("/encrypt", session.MustUser(), api.PostSecure)
2015-09-30 01:21:17 +00:00
// requires push permissions
repo.PATCH("", session.MustPush, api.PatchRepo)
repo.DELETE("", session.MustPush, api.DeleteRepo)
2015-09-30 01:21:17 +00:00
2016-03-31 06:24:47 +00:00
repo.POST("/builds/:number", session.MustPush, api.PostBuild)
repo.DELETE("/builds/:number/:job", session.MustPush, api.DeleteBuild)
2015-09-30 01:21:17 +00:00
}
}
badges := e.Group("/api/badges/:owner/:name")
{
2016-03-31 06:27:53 +00:00
badges.GET("/status.svg", web.GetBadge)
badges.GET("/cc.xml", web.GetCC)
2015-09-30 01:21:17 +00:00
}
2016-03-31 06:27:53 +00:00
e.POST("/hook", web.PostHook)
e.POST("/api/hook", web.PostHook)
2015-09-30 01:21:17 +00:00
stream := e.Group("/api/stream")
{
stream.Use(session.SetRepo())
stream.Use(session.SetPerm())
stream.Use(session.MustPull)
2016-04-20 01:37:53 +00:00
if os.Getenv("CANARY") == "true" {
stream.GET("/:owner/:name", web.GetRepoEvents2)
stream.GET("/:owner/:name/:build/:number", web.GetStream2)
} else {
stream.GET("/:owner/:name", web.GetRepoEvents)
stream.GET("/:owner/:name/:build/:number", web.GetStream)
}
2015-09-30 01:21:17 +00:00
}
2016-04-12 17:13:26 +00:00
bots := e.Group("/bots")
2016-04-12 17:12:14 +00:00
{
2016-04-12 17:13:26 +00:00
bots.Use(session.MustUser())
bots.POST("/slack", web.Slack)
2016-04-12 20:08:17 +00:00
bots.POST("/slack/:command", web.Slack)
2016-04-12 17:12:14 +00:00
}
2015-09-30 01:21:17 +00:00
auth := e.Group("/authorize")
{
2016-03-31 06:27:53 +00:00
auth.GET("", web.GetLogin)
auth.POST("", web.GetLogin)
auth.POST("/token", web.GetLoginToken)
2015-09-30 01:21:17 +00:00
}
2016-04-20 01:37:53 +00:00
queue := e.Group("/api/queue")
{
2016-04-22 00:10:19 +00:00
queue.Use(middleware.AgentMust())
2016-04-20 01:37:53 +00:00
queue.POST("/pull", api.Pull)
2016-04-22 00:10:19 +00:00
queue.POST("/pull/:os/:arch", api.Pull)
2016-04-20 01:37:53 +00:00
queue.POST("/wait/:id", api.Wait)
queue.POST("/stream/:id", api.Stream)
queue.POST("/status/:id", api.Update)
}
gitlab := e.Group("/gitlab/:owner/:name")
2015-09-30 01:21:17 +00:00
{
gitlab.Use(session.SetRepo())
2016-03-31 06:27:53 +00:00
gitlab.GET("/commits/:sha", web.GetCommit)
gitlab.GET("/pulls/:number", web.GetPullRequest)
2015-09-30 01:21:17 +00:00
redirects := gitlab.Group("/redirect")
{
2016-03-31 06:27:53 +00:00
redirects.GET("/commits/:sha", web.RedirectSha)
redirects.GET("/pulls/:number", web.RedirectPullRequest)
2015-09-30 01:21:17 +00:00
}
}
return normalize(e)
}
// normalize is a helper function to work around the following
// issue with gin. https://github.com/gin-gonic/gin/issues/388
func normalize(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")[1:]
switch parts[0] {
2016-04-12 17:14:10 +00:00
case "settings", "bots", "repos", "api", "login", "logout", "", "authorize", "hook", "static", "gitlab":
2015-09-30 01:21:17 +00:00
// no-op
default:
if len(parts) > 2 && parts[2] != "settings" {
parts = append(parts[:2], append([]string{"builds"}, parts[2:]...)...)
}
// prefix the URL with /repo so that it
// can be effectively routed.
parts = append([]string{"", "repos"}, parts...)
// reconstruct the path
r.URL.Path = strings.Join(parts, "/")
}
h.ServeHTTP(w, r)
})
}