1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-30 08:06:52 +02:00
woodpecker/drone.go

198 lines
4.7 KiB
Go
Raw Normal View History

package main
import (
2015-04-09 01:19:25 +02:00
"flag"
"html/template"
"net/http"
2015-04-09 01:19:25 +02:00
"github.com/gin-gonic/gin"
"github.com/drone/drone/remote/github"
"github.com/drone/drone/server"
"github.com/drone/drone/server/session"
"github.com/drone/drone/settings"
"github.com/elazarl/go-bindata-assetfs"
2015-04-24 23:25:03 +02:00
store "github.com/drone/drone/datastore/builtin"
2015-04-29 00:30:51 +02:00
eventbus "github.com/drone/drone/eventbus/builtin"
2015-04-24 23:25:03 +02:00
queue "github.com/drone/drone/queue/builtin"
runner "github.com/drone/drone/runner/builtin"
)
var conf = flag.String("config", "drone.toml", "")
func main() {
2015-04-09 01:59:57 +02:00
flag.Parse()
2015-04-09 01:19:25 +02:00
settings, err := settings.Parse(*conf)
2015-04-09 01:19:25 +02:00
if err != nil {
panic(err)
}
store := store.Must(settings.Database.Path)
defer store.Close()
2015-04-09 01:19:25 +02:00
remote := github.New(settings.Service)
session := session.New(settings.Session)
eventbus_ := eventbus.New()
queue_ := queue.New()
updater := runner.NewUpdater(eventbus_, store)
runner_ := runner.Runner{updater}
go run(&runner_, queue_)
2015-04-09 01:19:25 +02:00
r := gin.Default()
api := r.Group("/api")
api.Use(server.SetHeaders())
api.Use(server.SetBus(eventbus_))
api.Use(server.SetDatastore(store))
2015-04-09 01:19:25 +02:00
api.Use(server.SetRemote(remote))
api.Use(server.SetQueue(queue_))
2015-04-09 01:19:25 +02:00
api.Use(server.SetSettings(settings))
2015-04-30 23:23:46 +02:00
api.Use(server.SetSession(session))
2015-04-09 01:19:25 +02:00
api.Use(server.SetUser(session))
user := api.Group("/user")
{
user.Use(server.MustUser())
user.GET("", server.GetUserCurr)
user.PATCH("", server.PutUserCurr)
2015-04-09 01:19:25 +02:00
user.GET("/repos", server.GetUserRepos)
2015-04-13 10:22:51 +02:00
user.GET("/tokens", server.GetUserTokens)
user.POST("/tokens", server.PostToken)
user.DELETE("/tokens/:label", server.DelToken)
2015-04-09 01:19:25 +02:00
}
users := api.Group("/users")
{
users.Use(server.MustAdmin())
users.GET("", server.GetUsers)
users.GET("/:name", server.GetUser)
users.POST("/:name", server.PostUser)
users.PATCH("/:name", server.PutUser)
2015-04-09 01:19:25 +02:00
users.DELETE("/:name", server.DeleteUser)
}
2015-04-30 23:41:52 +02:00
agents := api.Group("/agents")
{
agents.Use(server.MustAdmin())
agents.GET("/token", server.GetAgentToken)
}
2015-04-09 01:19:25 +02:00
repos := api.Group("/repos/:owner/:name")
{
repos.POST("", server.PostRepo)
repo := repos.Group("")
{
repo.Use(server.SetRepo())
repo.Use(server.SetPerm())
repo.Use(server.CheckPull())
repo.Use(server.CheckPush())
repo.GET("", server.GetRepo)
repo.PATCH("", server.PutRepo)
2015-04-09 01:19:25 +02:00
repo.DELETE("", server.DeleteRepo)
repo.POST("/watch", server.Subscribe)
repo.DELETE("/unwatch", server.Unsubscribe)
repo.GET("/builds", server.GetBuilds)
repo.GET("/builds/:number", server.GetBuild)
2015-04-17 07:07:13 +02:00
repo.POST("/builds/:number", server.RunBuild)
repo.DELETE("/builds/:number", server.KillBuild)
repo.GET("/logs/:number/:task", server.GetBuildLogs)
repo.POST("/status/:number", server.PostBuildStatus)
2015-04-09 01:19:25 +02:00
}
}
badges := api.Group("/badges/:owner/:name")
{
badges.Use(server.SetRepo())
badges.GET("/status.svg", server.GetBadge)
badges.GET("/cc.xml", server.GetCC)
}
hooks := api.Group("/hook")
{
hooks.POST("", server.PostHook)
}
2015-04-24 23:25:03 +02:00
queue := api.Group("/queue")
{
2015-04-30 23:57:53 +02:00
queue.Use(server.MustAgent())
2015-04-24 23:25:03 +02:00
queue.GET("", server.GetQueue)
queue.POST("/pull", server.PollBuild)
push := queue.Group("/push/:owner/:name")
{
push.Use(server.SetRepo())
push.POST("", server.PushBuild)
push.POST("/:build", server.PushTask)
push.POST("/:build/:task/logs", server.PushLogs)
}
2015-04-24 23:25:03 +02:00
}
2015-04-25 02:06:46 +02:00
events := api.Group("/stream")
{
events.GET("/user", server.GetEvents)
2015-04-30 04:57:43 +02:00
stream := events.Group("/logs")
{
stream.Use(server.SetRepo())
stream.Use(server.SetPerm())
stream.GET("/:owner/:name/:build/:number", server.GetStream)
}
2015-04-25 02:06:46 +02:00
}
2015-04-09 01:19:25 +02:00
auth := r.Group("/authorize")
{
auth.Use(server.SetHeaders())
auth.Use(server.SetDatastore(store))
2015-04-09 01:19:25 +02:00
auth.Use(server.SetRemote(remote))
auth.Use(server.SetSettings(settings))
auth.Use(server.SetSession(session))
auth.GET("", server.GetLogin)
auth.POST("", server.GetLogin)
}
r.SetHTMLTemplate(index())
2015-04-09 01:19:25 +02:00
r.NoRoute(func(c *gin.Context) {
c.HTML(200, "index.html", nil)
2015-04-09 01:19:25 +02:00
})
http.Handle("/static/", static())
http.Handle("/", r)
http.ListenAndServe(settings.Server.Addr, nil)
}
// static is a helper function that will setup handlers
// for serving static files.
func static() http.Handler {
return http.StripPrefix("/static/", http.FileServer(&assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
Prefix: "server/static",
}))
}
// index is a helper function that will setup a template
// for rendering the main angular index.html file.
func index() *template.Template {
file := MustAsset("server/static/index.html")
filestr := string(file)
return template.Must(template.New("index.html").Parse(filestr))
}
// run is a helper function for initializing the
// built-in build runner, if not running in remote
// mode.
func run(r *runner.Runner, q *queue.Queue) {
defer func() {
recover()
}()
r.Poll(q)
}