66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
|
// 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"
|
||
|
"time"
|
||
|
|
||
|
"github.com/drone/drone/core"
|
||
|
"github.com/drone/go-scm/scm"
|
||
|
)
|
||
|
|
||
|
type varz struct {
|
||
|
SCM *scmInfo `json:"scm"`
|
||
|
License *licenseInfo `json:"license"`
|
||
|
}
|
||
|
|
||
|
type scmInfo struct {
|
||
|
URL string `json:"url"`
|
||
|
Rate *rateInfo `json:"rate"`
|
||
|
}
|
||
|
|
||
|
type rateInfo struct {
|
||
|
Limit int `json:"limit"`
|
||
|
Remaining int `json:"remaining"`
|
||
|
Reset int64 `json:"reset"`
|
||
|
}
|
||
|
|
||
|
type licenseInfo struct {
|
||
|
Kind string `json:"kind"`
|
||
|
Seats int64 `json:"seats"`
|
||
|
SeatsUsed int64 `json:"seats_used,omitempty"`
|
||
|
SeatsAvail int64 `json:"seats_available,omitempty"`
|
||
|
Repos int64 `json:"repos"`
|
||
|
ReposUsed int64 `json:"repos_used,omitempty"`
|
||
|
ReposAvail int64 `json:"repos_available,omitempty"`
|
||
|
Expires time.Time `json:"expire_at,omitempty"`
|
||
|
}
|
||
|
|
||
|
// HandleVarz creates an http.HandlerFunc that exposes internal system
|
||
|
// information.
|
||
|
func HandleVarz(client *scm.Client, license *core.License) http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
rate := client.Rate()
|
||
|
v := &varz{
|
||
|
License: &licenseInfo{
|
||
|
Kind: license.Kind,
|
||
|
Seats: license.Users,
|
||
|
Repos: license.Repos,
|
||
|
Expires: license.Expires,
|
||
|
},
|
||
|
SCM: &scmInfo{
|
||
|
URL: client.BaseURL.String(),
|
||
|
Rate: &rateInfo{
|
||
|
Limit: rate.Limit,
|
||
|
Remaining: rate.Remaining,
|
||
|
Reset: rate.Reset,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
writeJSON(w, v, 200)
|
||
|
}
|
||
|
}
|