Skip to content
Permalink
Browse files

Merge pull request #303 from erbesharat/fix/go-vet

[vet]: Fix `go vet` errors
  • Loading branch information
stp-ip committed Dec 4, 2019
2 parents 6ddcdf4 + 8e9c1f8 commit 1593301684c8dbf3db329aa38ba973110305f38e
Showing with 44 additions and 4 deletions.
  1. +1 −0 .travis.yml
  2. +3 −0 dockerv2.go
  3. +1 −0 fallback.go
  4. +5 −0 git.go
  5. +5 −1 gometa.go
  6. +2 −0 host.go
  7. +8 −0 path.go
  8. +1 −0 placeholders.go
  9. +7 −2 prometheus.go
  10. +6 −0 proxy.go
  11. +4 −0 qr.go
  12. +1 −1 setup.go
@@ -14,5 +14,6 @@ before_script:
- docker pull k8s.gcr.io/coredns:1.6.2

script:
- go vet
- make docker-test
- make endtoend-test
@@ -23,13 +23,15 @@ import (
"strings"
)

// Dockerv2 keeps data for "dockerv2" type requests
type Dockerv2 struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}

// NewDockerv2 returns a fresh instance of Dockerv2 struct
func NewDockerv2(w http.ResponseWriter, r *http.Request, rec record, c Config) *Dockerv2 {
return &Dockerv2{
rw: w,
@@ -44,6 +46,7 @@ var dockerRegexes = map[string]*regexp.Regexp{
"container": regexp.MustCompile("v2\\/(([\\w\\d-]+\\/?)+)\\/(tags|manifests|_catalog|blobs)"),
}

// Redirect handles the requests for "dockerv2" type
func (d *Dockerv2) Redirect() error {
path := d.req.URL.Path
if !strings.HasPrefix(path, "/v2") {
@@ -8,6 +8,7 @@ import (
"strings"
)

// Fallback keeps the data necessary for the fallback flow
type Fallback struct {
rw http.ResponseWriter
request *http.Request
5 git.go
@@ -10,13 +10,15 @@ import (
"github.com/prometheus/common/log"
)

// Git keeps data for "git" type requests
type Git struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}

// NewGit returns a fresh instance of Git struct
func NewGit(w http.ResponseWriter, r *http.Request, c Config, rec record) *Git {
return &Git{
rw: w,
@@ -26,6 +28,7 @@ func NewGit(w http.ResponseWriter, r *http.Request, c Config, rec record) *Git {
}
}

// Proxy handles the requests for "git" type
func (g *Git) Proxy() error {
to, _, err := getBaseTarget(g.rec, g.req)
if err != nil {
@@ -68,6 +71,8 @@ func (g *Git) Proxy() error {
return nil
}

// ValidGitQuery checks the User-Agent header to make sure the requests are
// coming from a Git client.
func (g *Git) ValidGitQuery() bool {
if !strings.HasPrefix(g.req.Header.Get("User-Agent"), "git") {
fallback(g.rw, g.req, "website", g.rec.Code, g.c)
@@ -20,13 +20,15 @@ import (
"strings"
)

// Gometa keeps data for "gometa" type requests
type Gometa struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}

// NewGometa returns a fresh instance of Gometa struct
func NewGometa(w http.ResponseWriter, r *http.Request, rec record, c Config) *Gometa {
return &Gometa{
rw: w,
@@ -44,7 +46,7 @@ var tmpl = template.Must(template.New("").Parse(`<!DOCTYPE html>
</head>
</html>`))

// gometa executes a template on the given ResponseWriter
// Serve executes a template on the given ResponseWriter
// that contains go-import meta tag
func (g *Gometa) Serve() error {
if g.rec.Vcs == "" {
@@ -72,6 +74,8 @@ func (g *Gometa) Serve() error {
})
}

// ValidQuery checks the request query to make sure the requests are
// coming from the Go tool.
func (g *Gometa) ValidQuery() bool {
if g.req.URL.Query().Get("go-get") != "1" {
fallback(g.rw, g.req, "website", http.StatusFound, g.c)
@@ -7,13 +7,15 @@ import (
"strconv"
)

// Host keeps data for "host" type requests
type Host struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}

// NewHost returns a fresh instance of Host struct
func NewHost(w http.ResponseWriter, r *http.Request, rec record, c Config) *Host {
return &Host{
rw: w,
@@ -45,9 +45,17 @@ type RegexRecord struct {
Submatches []string
}

// PathRegex is the default regex to parse request's path
// It can be replaced using the re= field in the records
var PathRegex = regexp.MustCompile("\\/([A-Za-z0-9-._~!$'()*+,;=:@]+)")

// FromRegex parses the from= field
var FromRegex = regexp.MustCompile("\\/\\$(\\d+)")

// GroupRegex parses the re= field to find the regex groups
var GroupRegex = regexp.MustCompile("P<[a-zA-Z]+[a-zA-Z0-9]*>")

// GroupOrderRegex finds the order of regex groups inside re=
var GroupOrderRegex = regexp.MustCompile("P<([a-zA-Z]+[a-zA-Z0-9]*)>")

// NewPath returns an instance of Path struct using the given data
@@ -24,6 +24,7 @@ import (
"strings"
)

// PlaceholderRegex finds the placeholders like {x}
var PlaceholderRegex = regexp.MustCompile("{[~>?]?\\w+}")

// parsePlaceholders gets a string input and looks for placeholders inside
@@ -33,36 +33,40 @@ type Prometheus struct {
Address string
Path string

once sync.Once
next httpserver.Handler
handler http.Handler
}

var (
// RequestsCount counts the total requests per host
RequestsCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "txtdirect",
Name: "redirect_count_total",
Help: "Total requests per host",
}, []string{"host"})

// RequestsByStatus counts the total returned statuses per host
RequestsByStatus = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "txtdirect",
Name: "redirect_status_count_total",
Help: "Total returned statuses per host",
}, []string{"host", "status"})

// RequestsCountBasedOnType counts the total requests for each host based on type
RequestsCountBasedOnType = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "txtdirect",
Name: "redirect_type_count_total",
Help: "Total requests for each host based on type",
}, []string{"host", "type"})

// FallbacksCount counts the total fallbacks triggered for each type
FallbacksCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "txtdirect",
Name: "fallback_type_count_total",
Help: "Total fallbacks triggered for each type",
}, []string{"host", "type", "fallback"})

// PathRedirectCount counts the total redirects per path for each host
PathRedirectCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "txtdirect",
Name: "redirect_path_count_total",
@@ -91,7 +95,7 @@ func (p *Prometheus) SetDefaults() {
}

func (p *Prometheus) start() error {
p.once.Do(func() {
once.Do(func() {
prometheus.MustRegister(RequestsCount)
prometheus.MustRegister(RequestsByStatus)
prometheus.MustRegister(RequestsCountBasedOnType)
@@ -108,6 +112,7 @@ func (p *Prometheus) start() error {
return nil
}

// Setup registers the metrics on startup and creates the promethues request handler
func (p *Prometheus) Setup(c *caddy.Controller) {
p.handler = promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
ErrorHandling: promhttp.HTTPErrorOnError,
@@ -25,6 +25,8 @@ import (
"github.com/caddyserver/caddy/caddyhttp/proxy"
)

// ProxyResponse keeps the custom response writer data and is
// a custom implementation of net/http's default response writer
type ProxyResponse struct {
headers http.Header
body []byte
@@ -41,6 +43,7 @@ type Proxy struct {
rec record
}

// NewProxy returns a fresh instance of Proxy struct
func NewProxy(w http.ResponseWriter, r *http.Request, rec record, c Config) *Proxy {
return &Proxy{
rw: w,
@@ -109,6 +112,8 @@ func (p *ProxyResponse) WriteHeader(status int) {
p.status = status
}

// ReplaceBody replaces the URIs inside the response body
// to use the request's host instead of the endpoint's host
func (p *ProxyResponse) ReplaceBody(scheme, to, host string) error {
replacedBody := bytes.Replace(p.bodyWriter.Bytes(), []byte(scheme+"://"+to), []byte(scheme+"://"+host), -1)
p.bodyWriter.Reset()
@@ -118,6 +123,7 @@ func (p *ProxyResponse) ReplaceBody(scheme, to, host string) error {
return nil
}

// WriteBody writes the response body to custom response writer's body
func (p *ProxyResponse) WriteBody() error {
switch p.Header().Get("Content-Encoding") {
case "gzip":
4 qr.go
@@ -23,6 +23,7 @@ type Qr struct {
foregroundColor color.Color
}

// Redirect handles the requests for "qr" requests
func (qr *Qr) Redirect(w http.ResponseWriter, r *http.Request) error {
Qr, err := qrcode.New(r.Host+r.URL.String(), qr.RecoveryLevel)
if err != nil {
@@ -37,6 +38,7 @@ func (qr *Qr) Redirect(w http.ResponseWriter, r *http.Request) error {
return nil
}

// ParseColors parses the hex colors in the QR config to color.Color instances
func (qr *Qr) ParseColors() error {
bg, err := hex.DecodeString(qr.BackgroundColor)
if err != nil {
@@ -51,6 +53,7 @@ func (qr *Qr) ParseColors() error {
return nil
}

// ParseQr parses the config for QR requests
func (qr *Qr) ParseQr(c *caddy.Controller) error {
switch c.Val() {
case "size":
@@ -75,6 +78,7 @@ func (qr *Qr) ParseQr(c *caddy.Controller) error {
return nil
}

// SetDefaults sets the default values for QR config
func (qr *Qr) SetDefaults() {
if qr.Size == 0 {
qr.Size = 256
@@ -199,7 +199,7 @@ func removeArrayFromArray(array, toBeRemoved []string) []string {
return tmp
}

// Redirect is middleware to redirect requests based on TXT records
// TXTDirect is middleware to redirect requests based on TXT records
type TXTDirect struct {
Next httpserver.Handler
Config Config

0 comments on commit 1593301

Please sign in to comment.
You can’t perform that action at this time.