Skip to content

Commit 857a9ec

Browse files
authored
chore(go): update code to pass golangci-lint (#23)
1 parent ec5d82c commit 857a9ec

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

.golangci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
linters:
2+
enable-all: true
3+
disable:
4+
- cyclop
5+
- exhaustive
6+
- exhaustivestruct
7+
- funlen
8+
- golint
9+
- interfacer
10+
- lll
11+
- maligned
12+
- nlreturn
13+
- scopelint
14+
- wrapcheck
15+
- wsl
16+
17+
issues:
18+
exclude-use-default: true

analytics/title.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func getTitle(recorder *httptest.ResponseRecorder) (title string, err error) {
1919
// The content type that was returned. The <title> tag can only be
2020
// extracted from an HTML response body.
21-
var contentType = recorder.Header().Get("Content-Type")
21+
contentType := recorder.Header().Get("Content-Type")
2222

2323
// Only bother parsing HTML if there is HTML to parse.
2424
if !strings.Contains(contentType, "text/html") {
@@ -28,7 +28,7 @@ func getTitle(recorder *httptest.ResponseRecorder) (title string, err error) {
2828
// The content encoding that was returned. The response body may be be
2929
// compressed, so detect the type of compression, if any, and decode the
3030
// response body accordingly.
31-
var contentEncoding = recorder.Header().Get("Content-Encoding")
31+
contentEncoding := recorder.Header().Get("Content-Encoding")
3232
var reader io.Reader = bytes.NewBuffer(recorder.Body.Bytes())
3333

3434
// How is the request compressed?

analytics/tracker.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
package analytics
66

77
import (
8+
"context"
89
"io"
910
"log"
1011
"net"
1112
"net/http"
1213
"net/http/httptest"
1314
"net/url"
15+
"time"
1416
)
1517

1618
// Compile-time assertion that Tracker implements http.Handler.
@@ -58,7 +60,7 @@ func (t *Tracker) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
5860
// Copy status code.
5961
writer.WriteHeader(recorder.Code)
6062
// Copy response body.
61-
io.Copy(writer, recorder.Body)
63+
_, _ = io.Copy(writer, recorder.Body)
6264
}()
6365

6466
// Set values for the various Google Analytics request query parameters.
@@ -114,7 +116,7 @@ func (t *Tracker) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
114116
// Fallback to using the IP address of the client connection itself, which
115117
// may be inaccurate (or in a private address range) depending on your
116118
// networking configuration.
117-
//See: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uip
119+
// See: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uip
118120
if value := request.Header.Get("X-Forwarded-For"); value != "" {
119121
params.Set("uip", value)
120122
} else if value, _, err := net.SplitHostPort(request.RemoteAddr); err == nil {
@@ -190,8 +192,32 @@ func (t *Tracker) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
190192
// regardless of the request. We also only log an error in order to not
191193
// interrupt responses back to the client.
192194
go func() {
193-
if _, err := http.DefaultClient.Get(googleAnalytics.String()); err != nil {
195+
if err := report(googleAnalytics.String()); err != nil {
194196
log.Printf("error: %v", err)
195197
}
196198
}()
197199
}
200+
201+
func report(url string) error {
202+
// googleAnalyticsTimeout is a (rather short) timeout used when sending
203+
// requests to Google Analytics.
204+
const googleAnalyticsTimeout = time.Second * 5
205+
206+
// Create, and automatically cancel, a context using the above timeout.
207+
ctx, cancel := context.WithTimeout(context.Background(), googleAnalyticsTimeout)
208+
defer cancel()
209+
210+
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
211+
if err != nil {
212+
return err
213+
}
214+
215+
// Send report to Google Analytics.
216+
response, err := http.DefaultClient.Do(request)
217+
if err != nil {
218+
return err
219+
}
220+
_ = response.Body.Close()
221+
222+
return nil
223+
}

main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var version = "development"
2222

2323
func main() {
2424
if err := mainCmd(); err != nil {
25-
fmt.Printf("google-analytics-proxy: %v", err)
25+
fmt.Printf("google-analytics-proxy: %v", err) //nolint:forbidigo
2626
os.Exit(1)
2727
}
2828
}
@@ -33,37 +33,37 @@ func mainCmd() error {
3333
// listenAddress is the host and port that the proxy will listen on.
3434
// See net.Dial for details of the address format.
3535
// Example: "localhost:8080" "0.0.0.0:8080" ":8080"
36-
var listenAddress = os.Getenv("LISTEN")
36+
listenAddress := os.Getenv("LISTEN")
3737

3838
// upstreamEndpoint is the address of the upstream service to be
3939
// proxied.
4040
// Example: "https://example.com" "http://:80"
41-
var upstreamEndpoint = os.Getenv("UPSTREAM_ENDPOINT")
41+
upstreamEndpoint := os.Getenv("UPSTREAM_ENDPOINT")
4242

4343
// upstreamHostname optionally is the hostname to used when proxying
4444
// requests to the upstream. Used for hostname based routing. If empty, the
4545
// value of $GOOGLE_ANALYTICS_PROPERTY_NAME will be used.
4646
// Example: "example.com"
47-
var upstreamHostname = os.Getenv("UPSTREAM_HOSTNAME")
47+
upstreamHostname := os.Getenv("UPSTREAM_HOSTNAME")
4848

4949
// googleAnalyticsTrackingID is the tracking id for the Google
5050
// Analytics property that you want to track pageview events for. This
5151
// can be found in your Google Analytics dashboard.
5252
// Example: "UA-123456789-1"
53-
var googleAnalyticsTrackingID = os.Getenv("GOOGLE_ANALYTICS_TRACKING_ID")
53+
googleAnalyticsTrackingID := os.Getenv("GOOGLE_ANALYTICS_TRACKING_ID")
5454

5555
// googleAnalyticsPropertyName is the name for the Google Analytics
5656
// property that you want to track pageview events for. This can be
5757
// found in your Google Analytics dashboard. Will be used as the upstream
5858
// hostname in proxied requests if $UPSTREAM_HOSTNAME is empty.
5959
// Example: "example.com"
60-
var googleAnalyticsPropertyName = os.Getenv("GOOGLE_ANALYTICS_PROPERTY_NAME")
60+
googleAnalyticsPropertyName := os.Getenv("GOOGLE_ANALYTICS_PROPERTY_NAME")
6161

6262
// googleAnalyticsDryRun can optionally be used to disable reporting
6363
// pageview events with Google Analytics. See strconv.ParseBool() for
6464
// acceptable values.
6565
// Example: "true"
66-
var googleAnalyticsDryRun = os.Getenv("GOOGLE_ANALYTICS_DRY_RUN")
66+
googleAnalyticsDryRun := os.Getenv("GOOGLE_ANALYTICS_DRY_RUN")
6767

6868
// Parse the upstream endpoint address to ensure that it's valid.
6969
upstreamURL, err := url.Parse(upstreamEndpoint)

0 commit comments

Comments
 (0)