harness-drone/router/router.go

203 lines
5.1 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package router
2016-05-02 19:21:25 +00:00
import (
"net/http"
2017-03-05 07:56:08 +00:00
"os"
2016-05-02 19:21:25 +00:00
"github.com/gin-gonic/gin"
"github.com/drone/drone/router/middleware/header"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/router/middleware/token"
"github.com/drone/drone/server"
2017-03-10 10:58:25 +00:00
"github.com/drone/drone/server/debug"
"github.com/drone/drone/server/template"
"github.com/drone/drone-ui/dist"
2016-05-02 19:21:25 +00:00
)
// Load loads the router
2016-05-02 19:21:25 +00:00
func Load(middleware ...gin.HandlerFunc) http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.SetHTMLTemplate(template.Load())
fs := http.FileServer(dist.AssetFS())
e.GET("/static/*filepath", func(c *gin.Context) {
fs.ServeHTTP(c.Writer, c.Request)
})
2016-05-02 19:21:25 +00:00
e.Use(header.NoCache)
e.Use(header.Options)
e.Use(header.Secure)
e.Use(middleware...)
e.Use(session.SetUser())
e.Use(token.Refresh)
e.GET("/login", server.ShowLogin)
e.GET("/login/form", server.ShowLoginForm)
e.GET("/logout", server.GetLogout)
e.NoRoute(server.ShowIndex)
2016-05-02 19:21:25 +00:00
// TODO above will Go away with React UI
user := e.Group("/api/user")
{
user.Use(session.MustUser())
user.GET("", server.GetSelf)
user.GET("/feed", server.GetFeed)
user.GET("/repos", server.GetRepos)
user.GET("/repos/remote", server.GetRemoteRepos)
user.POST("/token", server.PostToken)
user.DELETE("/token", server.DeleteToken)
}
users := e.Group("/api/users")
{
users.Use(session.MustAdmin())
users.GET("", server.GetUsers)
users.POST("", server.PostUser)
users.GET("/:login", server.GetUser)
users.PATCH("/:login", server.PatchUser)
users.DELETE("/:login", server.DeleteUser)
}
2016-07-25 15:20:14 +00:00
teams := e.Group("/api/teams")
{
teams.Use(session.MustTeamAdmin())
2016-07-25 15:20:14 +00:00
team := teams.Group("/:team")
{
team.GET("/secrets", server.GetTeamSecrets)
team.POST("/secrets", server.PostTeamSecret)
team.DELETE("/secrets/:secret", server.DeleteTeamSecret)
}
}
2016-10-20 00:13:21 +00:00
global := e.Group("/api/global")
{
global.Use(session.MustAdmin())
global.GET("/secrets", server.GetGlobalSecrets)
global.POST("/secrets", server.PostGlobalSecret)
global.DELETE("/secrets/:secret", server.DeleteGlobalSecret)
}
2016-05-02 19:21:25 +00:00
repos := e.Group("/api/repos/:owner/:name")
{
repos.POST("", server.PostRepo)
repo := repos.Group("")
{
repo.Use(session.SetRepo())
repo.Use(session.SetPerm())
repo.Use(session.MustPull)
repo.GET("", server.GetRepo)
repo.GET("/builds", server.GetBuilds)
repo.GET("/builds/:number", server.GetBuild)
repo.GET("/logs/:number/:job", server.GetBuildLogs)
repo.POST("/sign", session.MustPush, server.Sign)
repo.GET("/secrets", session.MustPush, server.GetSecrets)
2016-05-02 19:21:25 +00:00
repo.POST("/secrets", session.MustPush, server.PostSecret)
repo.DELETE("/secrets/:secret", session.MustPush, server.DeleteSecret)
// requires push permissions
repo.PATCH("", session.MustPush, server.PatchRepo)
repo.DELETE("", session.MustRepoAdmin(), server.DeleteRepo)
2016-07-21 21:04:42 +00:00
repo.POST("/chown", session.MustRepoAdmin(), server.ChownRepo)
2016-05-02 19:21:25 +00:00
repo.POST("/builds/:number", session.MustPush, server.PostBuild)
repo.DELETE("/builds/:number/:job", session.MustPush, server.DeleteBuild)
}
}
badges := e.Group("/api/badges/:owner/:name")
{
badges.GET("/status.svg", server.GetBadge)
badges.GET("/cc.xml", server.GetCC)
}
2017-03-05 07:56:08 +00:00
if os.Getenv("DRONE_CANARY") == "" {
e.POST("/hook", server.PostHook)
e.POST("/api/hook", server.PostHook)
} else {
e.POST("/hook", server.PostHook2)
e.POST("/api/hook", server.PostHook2)
}
2016-05-02 19:21:25 +00:00
2017-03-05 07:56:08 +00:00
if os.Getenv("DRONE_CANARY") == "" {
ws := e.Group("/ws")
{
ws.GET("/broker", server.Broker)
ws.GET("/feed", server.EventStream)
ws.GET("/logs/:owner/:name/:build/:number",
session.SetRepo(),
session.SetPerm(),
session.MustPull,
server.LogStream,
)
}
} else {
ws := e.Group("/ws")
{
ws.GET("/broker", server.RPCHandler)
ws.GET("/rpc", server.RPCHandler)
ws.GET("/feed", server.EventStream2)
ws.GET("/logs/:owner/:name/:build/:number",
session.SetRepo(),
session.SetPerm(),
session.MustPull,
server.LogStream2,
)
}
info := e.Group("/api/info")
{
info.GET("/queue",
session.MustAdmin(),
server.GetQueueInfo,
)
}
}
2016-05-02 19:21:25 +00:00
auth := e.Group("/authorize")
{
auth.GET("", server.GetLogin)
auth.POST("", server.GetLogin)
auth.POST("/token", server.GetLoginToken)
}
builds := e.Group("/api/builds")
{
builds.Use(session.MustAdmin())
builds.GET("", server.GetBuildQueue)
}
2017-03-10 10:58:25 +00:00
debugger := e.Group("/api/debug")
{
2017-03-10 10:58:25 +00:00
debugger.Use(session.MustAdmin())
debugger.GET("/pprof/", debug.IndexHandler())
debugger.GET("/pprof/heap", debug.HeapHandler())
debugger.GET("/pprof/goroutine", debug.GoroutineHandler())
debugger.GET("/pprof/block", debug.BlockHandler())
debugger.GET("/pprof/threadcreate", debug.ThreadCreateHandler())
debugger.GET("/pprof/cmdline", debug.CmdlineHandler())
debugger.GET("/pprof/profile", debug.ProfileHandler())
debugger.GET("/pprof/symbol", debug.SymbolHandler())
debugger.POST("/pprof/symbol", debug.SymbolHandler())
debugger.GET("/pprof/trace", debug.TraceHandler())
}
2016-05-02 19:21:25 +00:00
// bots := e.Group("/bots")
// {
// bots.Use(session.MustUser())
// bots.POST("/slack", Slack)
// bots.POST("/slack/:command", Slack)
// }
return e
2016-05-02 19:21:25 +00:00
}