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

157 lines
4.3 KiB
Go
Raw Normal View History

2014-06-05 00:25:38 +03:00
package main
import (
"database/sql"
"flag"
"net/http"
"runtime"
"strings"
2014-06-05 00:25:38 +03:00
"github.com/drone/drone/server/database"
2014-06-13 01:02:19 +03:00
"github.com/drone/drone/server/database/schema"
2014-06-05 00:25:38 +03:00
"github.com/drone/drone/server/handler"
"github.com/drone/drone/server/pubsub"
2014-06-05 00:25:38 +03:00
"github.com/drone/drone/server/session"
"github.com/drone/drone/server/worker"
2014-06-12 22:44:19 +03:00
"github.com/drone/drone/shared/build/log"
"github.com/drone/drone/shared/model"
2014-06-05 00:25:38 +03:00
"github.com/gorilla/pat"
//"github.com/justinas/nosurf"
2014-06-12 03:42:49 +03:00
"github.com/GeertJohan/go.rice"
2014-06-05 00:25:38 +03:00
_ "github.com/mattn/go-sqlite3"
"github.com/russross/meddler"
2014-07-13 05:01:58 +03:00
_ "github.com/drone/drone/plugin/remote/bitbucket"
_ "github.com/drone/drone/plugin/remote/github"
_ "github.com/drone/drone/plugin/remote/gitlab"
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
// Number of concurrent build workers to run
// default to number of CPUs on machine
workers int
2014-06-05 00:25:38 +03:00
)
func main() {
2014-06-12 22:44:19 +03:00
log.SetPriority(log.LOG_NOTICE)
2014-06-05 00:25:38 +03:00
// parse command line flags
flag.StringVar(&port, "port", ":8080", "")
flag.StringVar(&driver, "driver", "sqlite3", "")
flag.StringVar(&datasource, "datasource", "drone.sqlite", "")
flag.StringVar(&sslcert, "sslcert", "", "")
flag.StringVar(&sslkey, "sslkey", "", "")
flag.IntVar(&workers, "workers", runtime.NumCPU(), "")
2014-06-05 00:25:38 +03:00
flag.Parse()
// setup the database
meddler.Default = meddler.SQLite
db, _ := sql.Open(driver, datasource)
2014-06-13 01:02:19 +03:00
schema.Load(db)
2014-06-05 00:25:38 +03:00
// setup the database managers
repos := database.NewRepoManager(db)
users := database.NewUserManager(db)
perms := database.NewPermManager(db)
commits := database.NewCommitManager(db)
servers := database.NewServerManager(db)
remotes := database.NewRemoteManager(db)
2014-07-13 05:01:58 +03:00
//configs := database.NewConfigManager(filepath.Join(home, "config.toml"))
2014-06-05 00:25:38 +03:00
// message broker
pubsub := pubsub.NewPubSub()
// cancel all previously running builds
go commits.CancelAll()
2014-07-13 05:01:58 +03:00
queue := make(chan *model.Request)
workers := make(chan chan *model.Request)
worker.NewDispatch(queue, workers).Start()
// there must be a minimum of 1 worker
if workers <= 0 {
workers = 1
}
// create the specified number of worker nodes
for i := 0; i < workers; i++ {
worker.NewWorker(workers, users, repos, commits, pubsub, &model.Server{}).Start()
}
2014-06-12 22:44:19 +03:00
2014-06-05 00:25:38 +03:00
// setup the session managers
sess := session.NewSession(users)
// setup the router and register routes
router := pat.New()
handler.NewUsersHandler(users, sess).Register(router)
handler.NewUserHandler(users, repos, commits, sess).Register(router)
2014-07-13 05:01:58 +03:00
handler.NewHookHandler(users, repos, commits, remotes, queue).Register(router)
handler.NewLoginHandler(users, repos, perms, sess, remotes).Register(router)
2014-06-12 03:42:49 +03:00
handler.NewCommitHandler(repos, commits, perms, sess, queue).Register(router)
2014-06-05 00:25:38 +03:00
handler.NewBranchHandler(repos, commits, perms, sess).Register(router)
2014-07-13 05:01:58 +03:00
handler.NewRepoHandler(repos, commits, perms, sess, remotes).Register(router)
2014-06-05 00:25:38 +03:00
handler.NewBadgeHandler(repos, commits).Register(router)
handler.NewServerHandler(servers, sess).Register(router)
handler.NewRemoteHandler(users, remotes, sess).Register(router)
handler.NewWsHandler(repos, commits, perms, sess, pubsub).Register(router)
box := rice.MustFindBox("app/")
fserver := http.FileServer(box.HTTPBox())
index, _ := box.Bytes("index.html")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, "/favicon.ico"),
strings.HasPrefix(r.URL.Path, "/scripts/"),
strings.HasPrefix(r.URL.Path, "/styles/"),
strings.HasPrefix(r.URL.Path, "/views/"):
fserver.ServeHTTP(w, r)
case strings.HasPrefix(r.URL.Path, "/logout"),
strings.HasPrefix(r.URL.Path, "/login/"),
strings.HasPrefix(r.URL.Path, "/v1/"),
strings.HasPrefix(r.URL.Path, "/ws/"):
router.ServeHTTP(w, r)
default:
w.Write(index)
}
})
2014-06-05 00:25:38 +03:00
// start webserver using HTTPS or HTTP
if len(sslcert) != 0 {
2014-06-05 00:25:38 +03:00
panic(http.ListenAndServeTLS(port, sslcert, sslkey, nil))
} else {
panic(http.ListenAndServe(port, nil))
}
}
2014-06-13 03:17:59 +03:00
func setupDatabase() {
}
func setupQueue() {
}
func setupHandlers() {
}