2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
package router
|
|
|
|
|
2016-05-02 21:21:25 +02:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2017-07-31 21:15:05 +02:00
|
|
|
"github.com/dimfeld/httptreemux"
|
2016-05-02 21:21:25 +02: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 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"
|
2017-07-31 21:15:05 +02:00
|
|
|
"github.com/drone/drone/server/web"
|
2016-05-02 21:21:25 +02:00
|
|
|
)
|
|
|
|
|
2016-07-09 00:40:29 +02:00
|
|
|
// Load loads the router
|
2017-07-31 21:15:05 +02:00
|
|
|
func Load(mux *httptreemux.ContextMux, middleware ...gin.HandlerFunc) http.Handler {
|
2016-05-02 21:21:25 +02:00
|
|
|
|
|
|
|
e := gin.New()
|
|
|
|
e.Use(gin.Recovery())
|
|
|
|
|
|
|
|
e.Use(header.NoCache)
|
|
|
|
e.Use(header.Options)
|
|
|
|
e.Use(header.Secure)
|
|
|
|
e.Use(middleware...)
|
|
|
|
e.Use(session.SetUser())
|
|
|
|
e.Use(token.Refresh)
|
|
|
|
|
2017-06-30 20:39:25 +02:00
|
|
|
e.NoRoute(func(c *gin.Context) {
|
2017-07-31 21:15:05 +02:00
|
|
|
req := c.Request.WithContext(
|
|
|
|
web.WithUser(
|
|
|
|
c.Request.Context(),
|
|
|
|
session.User(c),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mux.ServeHTTP(c.Writer, req)
|
2017-06-30 20:39:25 +02:00
|
|
|
})
|
2016-05-02 21:21:25 +02:00
|
|
|
|
2017-07-31 21:15:05 +02:00
|
|
|
e.GET("/logout", server.GetLogout)
|
|
|
|
e.GET("/login", server.HandleLogin)
|
|
|
|
|
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.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)
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
repo := e.Group("/api/repos/:owner/:name")
|
2016-05-02 21:21:25 +02:00
|
|
|
{
|
2017-07-14 21:58:38 +02:00
|
|
|
repo.Use(session.SetRepo())
|
|
|
|
repo.Use(session.SetPerm())
|
|
|
|
repo.Use(session.MustPull)
|
|
|
|
|
|
|
|
repo.POST("", session.MustRepoAdmin(), server.PostRepo)
|
|
|
|
repo.GET("", server.GetRepo)
|
|
|
|
repo.GET("/builds", server.GetBuilds)
|
|
|
|
repo.GET("/builds/:number", server.GetBuild)
|
2017-08-25 02:03:11 +02:00
|
|
|
repo.GET("/logs/:number/:pid", server.GetProcLogs)
|
|
|
|
repo.GET("/logs/:number/:pid/:proc", server.GetBuildLogs)
|
2017-07-14 21:58:38 +02:00
|
|
|
|
2017-07-27 19:06:24 +02:00
|
|
|
repo.GET("/files/:number", server.FileList)
|
|
|
|
repo.GET("/files/:number/:proc/*file", server.FileGet)
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
// requires push permissions
|
|
|
|
repo.GET("/secrets", session.MustPush, server.GetSecretList)
|
|
|
|
repo.POST("/secrets", session.MustPush, server.PostSecret)
|
|
|
|
repo.GET("/secrets/:secret", session.MustPush, server.GetSecret)
|
|
|
|
repo.PATCH("/secrets/:secret", session.MustPush, server.PatchSecret)
|
|
|
|
repo.DELETE("/secrets/:secret", session.MustPush, server.DeleteSecret)
|
|
|
|
|
|
|
|
// requires push permissions
|
|
|
|
repo.GET("/registry", session.MustPush, server.GetRegistryList)
|
|
|
|
repo.POST("/registry", session.MustPush, server.PostRegistry)
|
|
|
|
repo.GET("/registry/:registry", session.MustPush, server.GetRegistry)
|
|
|
|
repo.PATCH("/registry/:registry", session.MustPush, server.PatchRegistry)
|
|
|
|
repo.DELETE("/registry/:registry", session.MustPush, server.DeleteRegistry)
|
|
|
|
|
|
|
|
// requires admin permissions
|
|
|
|
repo.PATCH("", session.MustRepoAdmin(), server.PatchRepo)
|
|
|
|
repo.DELETE("", session.MustRepoAdmin(), server.DeleteRepo)
|
|
|
|
repo.POST("/chown", session.MustRepoAdmin(), server.ChownRepo)
|
|
|
|
repo.POST("/repair", session.MustRepoAdmin(), server.RepairRepo)
|
2017-08-21 23:56:37 +02:00
|
|
|
repo.POST("/move", session.MustRepoAdmin(), server.MoveRepo)
|
2017-07-14 21:58:38 +02:00
|
|
|
|
|
|
|
repo.POST("/builds/:number", session.MustPush, server.PostBuild)
|
2018-02-01 22:07:45 +02:00
|
|
|
repo.DELETE("/builds/:number", session.MustRepoAdmin(), server.ZombieKill)
|
2017-07-14 21:58:38 +02:00
|
|
|
repo.POST("/builds/:number/approve", session.MustPush, server.PostApproval)
|
|
|
|
repo.POST("/builds/:number/decline", session.MustPush, server.PostDecline)
|
|
|
|
repo.DELETE("/builds/:number/:job", session.MustPush, server.DeleteBuild)
|
2018-01-18 19:26:28 +02:00
|
|
|
repo.DELETE("/logs/:number", session.MustPush, server.DeleteBuildLogs)
|
2016-05-02 21:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-07-18 18:27:20 +02:00
|
|
|
sse := e.Group("/stream")
|
|
|
|
{
|
|
|
|
sse.GET("/events", server.EventStreamSSE)
|
2017-07-24 21:57:07 +02:00
|
|
|
sse.GET("/logs/:owner/:name/:build/:number",
|
|
|
|
session.SetRepo(),
|
|
|
|
session.SetPerm(),
|
|
|
|
session.MustPull,
|
|
|
|
server.LogStreamSSE,
|
|
|
|
)
|
2017-07-18 18:27:20 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
info := e.Group("/api/info")
|
|
|
|
{
|
|
|
|
info.GET("/queue",
|
|
|
|
session.MustAdmin(),
|
|
|
|
server.GetQueueInfo,
|
|
|
|
)
|
2016-07-09 00:40:29 +02:00
|
|
|
}
|
2016-05-02 21:21:25 +02:00
|
|
|
|
|
|
|
auth := e.Group("/authorize")
|
|
|
|
{
|
2017-07-31 21:15:05 +02:00
|
|
|
auth.GET("", server.HandleAuth)
|
|
|
|
auth.POST("", server.HandleAuth)
|
2016-05-02 21:21:25 +02:00
|
|
|
auth.POST("/token", server.GetLoginToken)
|
|
|
|
}
|
|
|
|
|
2016-05-11 07:19:45 +02:00
|
|
|
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")
|
2016-05-11 23:37:56 +02:00
|
|
|
{
|
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())
|
2016-05-11 23:37:56 +02:00
|
|
|
}
|
2016-05-11 07:19:45 +02:00
|
|
|
|
2017-04-30 23:09:58 +02:00
|
|
|
monitor := e.Group("/metrics")
|
2017-04-27 22:49:28 +02:00
|
|
|
{
|
2018-03-21 14:51:54 +02:00
|
|
|
monitor.GET("", metrics.PromHandler())
|
2017-04-27 22:49:28 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:17:27 +02:00
|
|
|
e.GET("/version", server.Version)
|
|
|
|
e.GET("/healthz", server.Health)
|
|
|
|
|
2016-07-09 00:40:29 +02:00
|
|
|
return e
|
2016-05-02 21:21:25 +02:00
|
|
|
}
|