// Copyright 2019 Drone.IO Inc. All rights reserved. // Use of this source code is governed by the Drone Non-Commercial License // that can be found in the LICENSE file. package web import ( "net/http" "github.com/drone/drone-ui/dist" "github.com/drone/drone/core" "github.com/drone/drone/handler/web/landingpage" "github.com/drone/drone/logger" "github.com/drone/go-login/login" "github.com/drone/go-scm/scm" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/unrolled/secure" ) func New( admitter core.AdmissionService, builds core.BuildStore, client *scm.Client, hooks core.HookParser, license *core.License, licenses core.LicenseService, login login.Middleware, repos core.RepositoryStore, session core.Session, syncer core.Syncer, triggerer core.Triggerer, users core.UserStore, userz core.UserService, webhook core.WebhookSender, options secure.Options, system *core.System, ) Server { return Server{ Admitter: admitter, Builds: builds, Client: client, Hooks: hooks, License: license, Licenses: licenses, Login: login, Repos: repos, Session: session, Syncer: syncer, Triggerer: triggerer, Users: users, Userz: userz, Webhook: webhook, Options: options, Host: system.Host, } } // Server is a http.Handler which exposes drone functionality over HTTP. type Server struct { Admitter core.AdmissionService Builds core.BuildStore Client *scm.Client Hooks core.HookParser License *core.License Licenses core.LicenseService Login login.Middleware Repos core.RepositoryStore Session core.Session Syncer core.Syncer Triggerer core.Triggerer Users core.UserStore Userz core.UserService Webhook core.WebhookSender Options secure.Options Host string } // Handler returns an http.Handler func (s Server) Handler() http.Handler { r := chi.NewRouter() r.Use(middleware.Recoverer) r.Use(middleware.NoCache) r.Use(logger.Middleware) sec := secure.New(s.Options) r.Use(sec.Handler) r.Route("/hook", func(r chi.Router) { r.Post("/", HandleHook(s.Repos, s.Builds, s.Triggerer, s.Hooks)) }) r.Get("/version", HandleVersion) r.Get("/healthz", HandleHealthz()) r.Get("/varz", HandleVarz(s.Client, s.License)) r.Handle("/login", s.Login.Handler( http.HandlerFunc( HandleLogin( s.Users, s.Userz, s.Syncer, s.Session, s.Admitter, s.Webhook, ), ), ), ) r.Get("/logout", HandleLogout()) h2 := http.FileServer(landingpage.New()) h := http.FileServer(dist.New()) h = setupCache(h) r.Handle("/favicon.png", h) r.Handle("/js/*filepath", h) r.Handle("/css/*filepath", h) r.Handle("/static2/*filepath", h2) r.NotFound(HandleIndex(s.Host, s.Session, s.Licenses)) return r }