harness-drone/handler/web/web.go

137 lines
3.2 KiB
Go
Raw Permalink Normal View History

2019-02-28 07:07:13 +00:00
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2019-02-19 23:56:41 +00:00
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)
2019-03-16 19:53:03 +00:00
r.Handle("/img/*filepath", h)
r.Handle("/images/*filepath", h)
2019-02-19 23:56:41 +00:00
r.Handle("/static2/*filepath", h2)
r.NotFound(HandleIndex(s.Host, s.Session, s.Licenses))
return r
}