1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-18 08:26:45 +02:00
woodpecker/server/main.go

217 lines
6.2 KiB
Go
Raw Normal View History

2014-06-05 00:25:38 +03:00
package main
import (
"database/sql"
"flag"
"fmt"
2014-06-05 00:25:38 +03:00
"net/http"
"strings"
2014-06-05 00:25:38 +03:00
"github.com/drone/config"
2014-06-05 00:25:38 +03:00
"github.com/drone/drone/server/handler"
2014-09-29 04:36:24 +03:00
"github.com/drone/drone/server/middleware"
2014-09-30 10:43:50 +03:00
"github.com/drone/drone/server/pubsub"
2014-06-12 22:44:19 +03:00
"github.com/drone/drone/shared/build/log"
2014-06-05 00:25:38 +03:00
2014-09-30 10:43:50 +03:00
"github.com/GeertJohan/go.rice"
2014-07-13 05:01:58 +03:00
2014-09-29 04:36:24 +03:00
"code.google.com/p/go.net/context"
webcontext "github.com/goji/context"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
_ "github.com/drone/drone/plugin/notify/email"
"github.com/drone/drone/plugin/remote/bitbucket"
"github.com/drone/drone/plugin/remote/github"
"github.com/drone/drone/plugin/remote/gitlab"
2014-09-29 04:36:24 +03:00
"github.com/drone/drone/server/blobstore"
"github.com/drone/drone/server/capability"
2014-09-29 04:36:24 +03:00
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/datastore/database"
2014-09-29 06:21:02 +03:00
"github.com/drone/drone/server/worker/director"
"github.com/drone/drone/server/worker/docker"
"github.com/drone/drone/server/worker/pool"
2014-06-05 00:25:38 +03:00
)
var (
// port the server will run on
port string
// database driver used to connect to the database
driver string
// driver specific connection information. In this
// case, it should be the location of the SQLite file
datasource string
// optional flags for tls listener
sslcert string
sslkey string
// commit sha for the current build.
version string = "0.3-dev"
2014-06-05 00:25:38 +03:00
revision string
conf string
prefix string
open bool
2014-09-29 06:21:02 +03:00
// worker pool
workers *pool.Pool
// director
worker *director.Director
2014-09-30 10:43:50 +03:00
pub *pubsub.PubSub
nodes StringArr
2014-09-29 04:36:24 +03:00
db *sql.DB
caps map[string]bool
2014-06-05 00:25:38 +03:00
)
func main() {
2014-06-12 22:44:19 +03:00
log.SetPriority(log.LOG_NOTICE)
flag.StringVar(&conf, "config", "", "")
flag.StringVar(&prefix, "prefix", "DRONE_", "")
2014-06-05 00:25:38 +03:00
flag.Parse()
2014-10-01 21:58:27 +03:00
config.StringVar(&datasource, "database-source", "drone.sqlite")
config.StringVar(&driver, "database-driver", "sqlite3")
config.Var(&nodes, "worker-nodes")
config.BoolVar(&open, "registration-open", false)
config.SetPrefix(prefix)
if err := config.Parse(conf); err != nil {
fmt.Println("Error parsing config", err)
}
// setup the remote services
bitbucket.Register()
github.Register()
gitlab.Register()
caps = map[string]bool{}
caps[capability.Registration] = open
2014-09-29 04:36:24 +03:00
// setup the database and cancel all pending
// commits in the system.
db = database.MustConnect(driver, datasource)
go database.NewCommitstore(db).KillCommits()
2014-09-29 06:21:02 +03:00
// Create the worker, director and builders
workers = pool.New()
worker = director.New()
if nodes == nil || len(nodes) == 0 {
workers.Allocate(docker.New())
workers.Allocate(docker.New())
} else {
for _, node := range nodes {
workers.Allocate(docker.NewHost(node))
2014-09-29 06:21:02 +03:00
}
}
pub = pubsub.NewPubSub()
2014-09-29 06:21:02 +03:00
2014-09-30 10:43:50 +03:00
goji.Get("/api/logins", handler.GetLoginList)
goji.Get("/api/stream/stdout/:id", handler.WsConsole)
goji.Get("/api/stream/user", handler.WsUser)
2014-09-29 04:36:24 +03:00
goji.Get("/api/auth/:host", handler.GetLogin)
2014-10-02 17:39:00 +03:00
goji.Post("/api/auth/:host", handler.GetLogin)
2014-09-29 04:36:24 +03:00
goji.Get("/api/badge/:host/:owner/:name/status.svg", handler.GetBadge)
goji.Get("/api/badge/:host/:owner/:name/cc.xml", handler.GetCC)
goji.Get("/api/hook/:host", handler.PostHook)
goji.Put("/api/hook/:host", handler.PostHook)
goji.Post("/api/hook/:host", handler.PostHook)
2014-09-29 04:36:24 +03:00
repos := web.New()
repos.Use(middleware.SetRepo)
repos.Use(middleware.RequireRepoRead)
repos.Use(middleware.RequireRepoAdmin)
repos.Get("/api/repos/:host/:owner/:name/branches/:branch/commits/:commit/console", handler.GetOutput)
repos.Get("/api/repos/:host/:owner/:name/branches/:branch/commits/:commit", handler.GetCommit)
repos.Post("/api/repos/:host/:owner/:name/branches/:branch/commits/:commit", handler.PostCommit)
repos.Get("/api/repos/:host/:owner/:name/commits", handler.GetCommitList)
repos.Get("/api/repos/:host/:owner/:name", handler.GetRepo)
repos.Put("/api/repos/:host/:owner/:name", handler.PutRepo)
repos.Post("/api/repos/:host/:owner/:name", handler.PostRepo)
repos.Delete("/api/repos/:host/:owner/:name", handler.DelRepo)
goji.Handle("/api/repos/:host/:owner/:name*", repos)
users := web.New()
users.Use(middleware.RequireUserAdmin)
users.Get("/api/users/:host/:login", handler.GetUser)
users.Post("/api/users/:host/:login", handler.PostUser)
users.Delete("/api/users/:host/:login", handler.DelUser)
users.Get("/api/users", handler.GetUserList)
goji.Handle("/api/users*", users)
user := web.New()
user.Use(middleware.RequireUser)
user.Get("/api/user/feed", handler.GetUserFeed)
user.Get("/api/user/repos", handler.GetUserRepos)
user.Get("/api/user", handler.GetUserCurrent)
user.Put("/api/user", handler.PutUser)
goji.Handle("/api/user*", user)
2014-09-30 07:34:30 +03:00
work := web.New()
work.Use(middleware.RequireUserAdmin)
work.Get("/api/work/started", handler.GetWorkStarted)
work.Get("/api/work/pending", handler.GetWorkPending)
work.Get("/api/work/assignments", handler.GetWorkAssigned)
work.Get("/api/workers", handler.GetWorkers)
2014-10-09 07:55:53 +03:00
work.Post("/api/workers", handler.PostWorker)
work.Delete("/api/workers", handler.DelWorker)
2014-09-30 07:34:30 +03:00
goji.Handle("/api/work*", work)
2014-09-30 10:43:50 +03:00
// Include static resources
assets := rice.MustFindBox("app").HTTPBox()
assetserve := http.FileServer(rice.MustFindBox("app").HTTPBox())
http.Handle("/static/", http.StripPrefix("/static", assetserve))
goji.Get("/*", func(c web.C, w http.ResponseWriter, r *http.Request) {
w.Write(assets.MustBytes("index.html"))
})
2014-09-29 04:36:24 +03:00
// Add middleware and serve
goji.Use(ContextMiddleware)
goji.Use(middleware.SetHeaders)
goji.Use(middleware.SetUser)
goji.Serve()
}
// ContextMiddleware creates a new go.net/context and
// injects into the current goji context.
func ContextMiddleware(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
var ctx = context.Background()
ctx = datastore.NewContext(ctx, database.NewDatastore(db))
ctx = blobstore.NewContext(ctx, database.NewBlobstore(db))
2014-09-29 06:21:02 +03:00
ctx = pool.NewContext(ctx, workers)
ctx = director.NewContext(ctx, worker)
2014-09-30 10:43:50 +03:00
ctx = pubsub.NewContext(ctx, pub)
ctx = capability.NewContext(ctx, caps)
2014-09-29 04:36:24 +03:00
// add the context to the goji web context
webcontext.Set(c, ctx)
h.ServeHTTP(w, r)
2014-06-05 00:25:38 +03:00
}
2014-09-29 04:36:24 +03:00
return http.HandlerFunc(fn)
2014-06-05 00:25:38 +03:00
}
type StringArr []string
func (s *StringArr) String() string {
return fmt.Sprint(*s)
}
func (s *StringArr) Set(value string) error {
for _, str := range strings.Split(value, ",") {
str = strings.TrimSpace(str)
*s = append(*s, str)
}
return nil
}