1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/router/router.go

180 lines
5.0 KiB
Go
Raw Normal View History

2015-09-30 03:21:17 +02:00
package router
2016-05-02 21:21:25 +02:00
import (
"net/http"
"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 12:58:25 +02:00
"github.com/drone/drone/server/debug"
2017-04-27 22:49:28 +02:00
"github.com/drone/drone/server/metrics"
"github.com/drone/drone/server/template"
2016-05-02 21:21:25 +02:00
)
// Load loads the router
2016-05-02 21:21:25 +02:00
func Load(middleware ...gin.HandlerFunc) http.Handler {
e := gin.New()
e.Use(gin.Recovery())
2017-05-12 16:06:01 +02:00
e.SetHTMLTemplate(template.T)
2017-06-30 20:39:25 +02:00
ui := server.NewWebsite()
for _, path := range ui.Routes() {
e.GET(path, func(c *gin.Context) {
ui.File(c.Writer, c.Request)
2017-03-20 17:51:41 +02:00
})
}
2016-05-02 21:21:25 +02: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("/logout", server.GetLogout)
2017-06-30 20:39:25 +02:00
e.NoRoute(func(c *gin.Context) {
u := session.User(c)
ui.Page(c.Writer, c.Request, u)
})
2016-05-02 21:21:25 +02:00
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)
}
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)
2017-04-03 11:34:37 +02:00
repo.GET("/logs/:number/:ppid/:proc", server.GetBuildLogs)
2016-05-02 21:21:25 +02:00
// requires push permissions
repo.GET("/secrets", session.MustPush, server.GetSecretList)
2016-05-02 21:21:25 +02:00
repo.POST("/secrets", session.MustPush, server.PostSecret)
repo.GET("/secrets/:secret", session.MustPush, server.GetSecret)
repo.PATCH("/secrets/:secret", session.MustPush, server.PatchSecret)
2016-05-02 21:21:25 +02:00
repo.DELETE("/secrets/:secret", session.MustPush, server.DeleteSecret)
2017-04-06 14:51:01 +02:00
// requires push permissions
repo.GET("/registry", session.MustPush, server.GetRegistryList)
repo.POST("/registry", session.MustPush, server.PostRegistry)
2017-05-05 16:40:54 +02:00
repo.GET("/registry/:registry", session.MustPush, server.GetRegistry)
repo.PATCH("/registry/:registry", session.MustPush, server.PatchRegistry)
repo.DELETE("/registry/:registry", session.MustPush, server.DeleteRegistry)
2017-04-06 14:51:01 +02:00
2016-05-02 21:21:25 +02:00
// requires push permissions
repo.PATCH("", session.MustPush, server.PatchRepo)
repo.DELETE("", session.MustRepoAdmin(), server.DeleteRepo)
2016-07-21 23:04:42 +02:00
repo.POST("/chown", session.MustRepoAdmin(), server.ChownRepo)
2017-04-12 15:32:44 +02:00
repo.POST("/repair", session.MustRepoAdmin(), server.RepairRepo)
2016-05-02 21:21:25 +02:00
repo.POST("/builds/:number", session.MustPush, server.PostBuild)
2017-03-18 10:49:27 +02:00
repo.POST("/builds/:number/approve", session.MustPush, server.PostApproval)
repo.POST("/builds/:number/decline", session.MustPush, server.PostDecline)
2016-05-02 21:21:25 +02:00
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-16 12:14:02 +02:00
e.POST("/hook", server.PostHook)
e.POST("/api/hook", server.PostHook)
ws := e.Group("/ws")
{
ws.GET("/feed", server.EventStream)
ws.GET("/logs/:owner/:name/:build/:number",
session.SetRepo(),
session.SetPerm(),
session.MustPull,
server.LogStream,
)
2017-03-05 09:56:08 +02:00
}
2016-05-02 21:21:25 +02:00
2017-03-16 12:14:02 +02:00
info := e.Group("/api/info")
{
info.GET("/queue",
session.MustAdmin(),
server.GetQueueInfo,
)
}
2016-05-02 21:21:25 +02: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 12:58:25 +02:00
debugger := e.Group("/api/debug")
{
2017-03-10 12:58:25 +02: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())
}
monitor := e.Group("/metrics")
2017-04-27 22:49:28 +02:00
{
monitor.GET("",
2017-04-27 22:49:28 +02:00
session.MustAdmin(),
metrics.PromHandler(),
)
}
return e
2016-05-02 21:21:25 +02:00
}
2017-03-20 17:51:41 +02:00
// type FileHandler interface {
// Index(res http.ResponseWriter, data interface{}) error
// Login(res http.ResponseWriter, data interface{}) error
// Error(res http.ResponseWriter, data interface{}) error
// Asset(res http.ResponseWriter, req *http.Request)
// }