traefik/pkg/api/handler.go

108 lines
3.5 KiB
Go
Raw Normal View History

package api
import (
2018-11-14 09:18:03 +00:00
"io"
"net/http"
"github.com/containous/mux"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/static"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/log"
"github.com/containous/traefik/pkg/types"
"github.com/containous/traefik/pkg/version"
2019-02-18 06:52:03 +00:00
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/unrolled/render"
)
var templateRenderer jsonRenderer = render.New(render.Options{Directory: "nowhere"})
2018-11-14 09:18:03 +00:00
type jsonRenderer interface {
JSON(w io.Writer, status int, v interface{}) error
2018-11-14 09:18:03 +00:00
}
type serviceInfoRepresentation struct {
*config.ServiceInfo
ServerStatus map[string]string `json:"serverStatus,omitempty"`
2018-11-14 09:18:03 +00:00
}
// RunTimeRepresentation is the configuration information exposed by the API handler.
type RunTimeRepresentation struct {
Routers map[string]*config.RouterInfo `json:"routers,omitempty"`
Middlewares map[string]*config.MiddlewareInfo `json:"middlewares,omitempty"`
Services map[string]*serviceInfoRepresentation `json:"services,omitempty"`
TCPRouters map[string]*config.TCPRouterInfo `json:"tcpRouters,omitempty"`
TCPServices map[string]*config.TCPServiceInfo `json:"tcpServices,omitempty"`
2018-11-14 09:18:03 +00:00
}
// Handler serves the configuration and status of Traefik on API endpoints.
type Handler struct {
dashboard bool
debug bool
// runtimeConfiguration is the data set used to create all the data representations exposed by the API.
runtimeConfiguration *config.RuntimeConfiguration
statistics *types.Statistics
// stats *thoasstats.Stats // FIXME stats
2018-11-14 09:18:03 +00:00
// StatsRecorder *middlewares.StatsRecorder // FIXME stats
dashboardAssets *assetfs.AssetFS
}
// New returns a Handler defined by staticConfig, and if provided, by runtimeConfig.
// It finishes populating the information provided in the runtimeConfig.
func New(staticConfig static.Configuration, runtimeConfig *config.RuntimeConfiguration) *Handler {
rConfig := runtimeConfig
if rConfig == nil {
rConfig = &config.RuntimeConfiguration{}
}
2018-11-14 09:18:03 +00:00
return &Handler{
dashboard: staticConfig.API.Dashboard,
statistics: staticConfig.API.Statistics,
dashboardAssets: staticConfig.API.DashboardAssets,
runtimeConfiguration: rConfig,
debug: staticConfig.Global.Debug,
}
2018-11-14 09:18:03 +00:00
}
2018-11-14 09:18:03 +00:00
// Append add api routes on a router
func (h Handler) Append(router *mux.Router) {
if h.debug {
2018-11-14 09:18:03 +00:00
DebugHandler{}.Append(router)
}
router.Methods(http.MethodGet).Path("/api/rawdata").HandlerFunc(h.getRuntimeConfiguration)
2018-11-14 09:18:03 +00:00
// FIXME stats
// health route
2018-11-14 09:18:03 +00:00
//router.Methods(http.MethodGet).Path("/health").HandlerFunc(p.getHealthHandler)
2018-11-14 09:18:03 +00:00
version.Handler{}.Append(router)
if h.dashboard {
DashboardHandler{Assets: h.dashboardAssets}.Append(router)
}
}
func (h Handler) getRuntimeConfiguration(rw http.ResponseWriter, request *http.Request) {
siRepr := make(map[string]*serviceInfoRepresentation, len(h.runtimeConfiguration.Services))
for k, v := range h.runtimeConfiguration.Services {
siRepr[k] = &serviceInfoRepresentation{
ServiceInfo: v,
ServerStatus: v.GetAllStatus(),
}
}
rtRepr := RunTimeRepresentation{
Routers: h.runtimeConfiguration.Routers,
Middlewares: h.runtimeConfiguration.Middlewares,
Services: siRepr,
TCPRouters: h.runtimeConfiguration.TCPRouters,
TCPServices: h.runtimeConfiguration.TCPServices,
}
2018-11-14 09:18:03 +00:00
err := templateRenderer.JSON(rw, http.StatusOK, rtRepr)
if err != nil {
2018-11-14 09:18:03 +00:00
log.FromContext(request.Context()).Error(err)
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}