2015-04-07 08:20:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-08 23:19:25 +00:00
|
|
|
"flag"
|
2015-04-25 23:43:51 +00:00
|
|
|
"html/template"
|
2015-04-25 23:16:56 +00:00
|
|
|
"net/http"
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-04-08 23:19:25 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/drone/drone/datastore/bolt"
|
2015-04-24 21:25:03 +00:00
|
|
|
"github.com/drone/drone/eventbus"
|
2015-04-08 23:19:25 +00:00
|
|
|
"github.com/drone/drone/remote/github"
|
|
|
|
"github.com/drone/drone/server"
|
|
|
|
"github.com/drone/drone/server/session"
|
|
|
|
"github.com/drone/drone/settings"
|
2015-04-25 23:16:56 +00:00
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
2015-04-24 21:25:03 +00:00
|
|
|
|
|
|
|
queue "github.com/drone/drone/queue/builtin"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2015-04-25 23:16:56 +00:00
|
|
|
var conf = flag.String("config", "drone.toml", "")
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
func main() {
|
2015-04-08 23:59:57 +00:00
|
|
|
flag.Parse()
|
2015-04-08 23:19:25 +00:00
|
|
|
|
2015-04-25 23:16:56 +00:00
|
|
|
settings, err := settings.Parse(*conf)
|
2015-04-08 23:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
remote := github.New(settings.Service)
|
|
|
|
session := session.New(settings.Session)
|
|
|
|
|
|
|
|
ds := bolt.Must(settings.Database.Path)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
api := r.Group("/api")
|
|
|
|
api.Use(server.SetHeaders())
|
2015-04-24 21:25:03 +00:00
|
|
|
api.Use(server.SetBus(eventbus.New()))
|
2015-04-08 23:19:25 +00:00
|
|
|
api.Use(server.SetDatastore(ds))
|
|
|
|
api.Use(server.SetRemote(remote))
|
2015-04-24 21:25:03 +00:00
|
|
|
api.Use(server.SetQueue(queue.New()))
|
2015-04-08 23:19:25 +00:00
|
|
|
api.Use(server.SetSettings(settings))
|
|
|
|
api.Use(server.SetUser(session))
|
|
|
|
|
|
|
|
user := api.Group("/user")
|
|
|
|
{
|
|
|
|
user.Use(server.MustUser())
|
2015-04-13 23:33:29 +00:00
|
|
|
user.Use(server.SetSession(session))
|
2015-04-08 23:19:25 +00:00
|
|
|
|
|
|
|
user.GET("", server.GetUserCurr)
|
2015-04-15 05:04:38 +00:00
|
|
|
user.PATCH("", server.PutUserCurr)
|
2015-04-08 23:19:25 +00:00
|
|
|
user.GET("/repos", server.GetUserRepos)
|
2015-04-13 08:22:51 +00:00
|
|
|
user.GET("/tokens", server.GetUserTokens)
|
|
|
|
user.POST("/tokens", server.PostToken)
|
2015-04-13 23:33:29 +00:00
|
|
|
user.DELETE("/tokens/:label", server.DelToken)
|
2015-04-08 23:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
users := api.Group("/users")
|
|
|
|
{
|
|
|
|
users.Use(server.MustAdmin())
|
|
|
|
|
|
|
|
users.GET("", server.GetUsers)
|
|
|
|
users.GET("/:name", server.GetUser)
|
|
|
|
users.POST("/:name", server.PostUser)
|
2015-04-15 05:04:38 +00:00
|
|
|
users.PATCH("/:name", server.PutUser)
|
2015-04-08 23:19:25 +00:00
|
|
|
users.DELETE("/:name", server.DeleteUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-04-15 05:04:38 +00:00
|
|
|
repo.PATCH("", server.PutRepo)
|
2015-04-08 23:19:25 +00:00
|
|
|
repo.DELETE("", server.DeleteRepo)
|
2015-04-15 05:04:38 +00:00
|
|
|
repo.POST("/watch", server.Subscribe)
|
|
|
|
repo.DELETE("/unwatch", server.Unsubscribe)
|
|
|
|
|
|
|
|
repo.GET("/builds", server.GetBuilds)
|
|
|
|
repo.GET("/builds/:number", server.GetBuild)
|
2015-04-17 05:07:13 +00:00
|
|
|
repo.POST("/builds/:number", server.RunBuild)
|
|
|
|
repo.DELETE("/builds/:number", server.KillBuild)
|
2015-04-16 21:45:05 +00:00
|
|
|
repo.GET("/logs/:number/:task", server.GetBuildLogs)
|
|
|
|
repo.POST("/status/:number", server.PostBuildStatus)
|
2015-04-08 23:19:25 +00: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 21:25:03 +00:00
|
|
|
queue := api.Group("/queue")
|
|
|
|
{
|
|
|
|
queue.GET("", server.GetQueue)
|
|
|
|
queue.POST("/pull", server.PollBuild)
|
2015-04-26 04:27:24 +00:00
|
|
|
|
|
|
|
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 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-25 00:06:46 +00:00
|
|
|
events := api.Group("/stream")
|
|
|
|
{
|
|
|
|
events.GET("/user", server.GetEvents)
|
|
|
|
//events.GET("/logs/:owner/:name/:build/:number")
|
|
|
|
}
|
|
|
|
|
2015-04-08 23:19:25 +00:00
|
|
|
auth := r.Group("/authorize")
|
|
|
|
{
|
|
|
|
auth.Use(server.SetHeaders())
|
|
|
|
auth.Use(server.SetDatastore(ds))
|
|
|
|
auth.Use(server.SetRemote(remote))
|
|
|
|
auth.Use(server.SetSettings(settings))
|
|
|
|
auth.Use(server.SetSession(session))
|
|
|
|
auth.GET("", server.GetLogin)
|
|
|
|
auth.POST("", server.GetLogin)
|
|
|
|
}
|
|
|
|
|
2015-04-25 23:43:51 +00:00
|
|
|
r.SetHTMLTemplate(index())
|
2015-04-08 23:19:25 +00:00
|
|
|
r.NoRoute(func(c *gin.Context) {
|
2015-04-25 23:43:51 +00:00
|
|
|
c.HTML(200, "index.html", nil)
|
2015-04-08 23:19:25 +00:00
|
|
|
})
|
2015-04-25 23:16:56 +00: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",
|
|
|
|
}))
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
2015-04-25 23:43:51 +00:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|