github.com/drone/runner-go@v1.12.0/handler/router/router.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package router
     6  
     7  import (
     8  	"net/http"
     9  
    10  	"github.com/drone/runner-go/handler"
    11  	"github.com/drone/runner-go/handler/static"
    12  	hook "github.com/drone/runner-go/logger/history"
    13  	"github.com/drone/runner-go/pipeline/reporter/history"
    14  
    15  	"github.com/99designs/basicauth-go"
    16  )
    17  
    18  // Config provides router configuration.
    19  type Config struct {
    20  	Username string
    21  	Password string
    22  	Realm    string
    23  }
    24  
    25  // New returns a new route handler.
    26  func New(tracer *history.History, history *hook.Hook, config Config) http.Handler {
    27  	mux := http.NewServeMux()
    28  	mux.HandleFunc("/healthz", handler.HandleHealth(tracer))
    29  
    30  	// omit dashboard handlers when no password configured.
    31  	if config.Password == "" {
    32  		return mux
    33  	}
    34  
    35  	// middleware to require basic authentication.
    36  	auth := basicauth.New(config.Realm, map[string][]string{
    37  		config.Username: {config.Password},
    38  	})
    39  
    40  	// handler to serve static assets for the dashboard.
    41  	fs := http.FileServer(static.New())
    42  
    43  	// dashboard handles.
    44  	mux.Handle("/static/", http.StripPrefix("/static/", fs))
    45  	mux.Handle("/logs", auth(handler.HandleLogHistory(history)))
    46  	mux.Handle("/view", auth(handler.HandleStage(tracer, history)))
    47  	mux.Handle("/", auth(handler.HandleIndex(tracer)))
    48  	return mux
    49  }