Skip to content

Commit a757914

Browse files
committed
Converting to go 1.11 modules
Removes vendored version of hysterix. Initattes two new go module roots: - One for the git root - One for the examples/ sub-directory Change example_test.go to rely on fewer external dependenceis. Updates Travis Go version to 1.11, and enables go build cache for faster CI runs.
1 parent 250bf40 commit a757914

File tree

7 files changed

+201
-198
lines changed

7 files changed

+201
-198
lines changed

.travis.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
language: go
22
go:
3-
- 1.9
4-
- tip
3+
- 1.11
4+
- tip
5+
cache:
6+
directories:
7+
- $HOME/.cache/go-build
8+
- $HOME/gopath/pkg/mod
59
matrix:
610
allow_failures:
711
- go: tip
812
script:
9-
go test -v -race -cpu=1,2,4 ./...
13+
- env GO111MODULE=on go build ./...
14+
- env GO111MODUKE=on go test -v -race -cpu=1,2,4 ./...
15+
- env GO111MODUKE=on go test -v -race -cpu=1,2,4 ./examples/...

example_test.go

Lines changed: 95 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,95 @@
1-
// +build go1.7
2-
31
package restlayer
42

53
import (
64
"context"
5+
"fmt"
6+
"log"
77
"net/http"
88
"net/url"
9+
"os"
910
"time"
1011

11-
"github.com/justinas/alice"
1212
"github.com/rs/cors"
13-
"github.com/rs/rest-layer/resource/testing/mem"
13+
1414
"github.com/rs/rest-layer/resource"
15+
"github.com/rs/rest-layer/resource/testing/mem"
1516
"github.com/rs/rest-layer/rest"
1617
"github.com/rs/rest-layer/schema"
17-
"github.com/rs/xaccess"
18-
"github.com/rs/zerolog"
19-
"github.com/rs/zerolog/hlog"
20-
"github.com/rs/zerolog/log"
2118
)
2219

20+
// ResponseRecorder extends http.ResponseWriter with the ability to capture
21+
// the status and number of bytes written
22+
type ResponseRecorder struct {
23+
http.ResponseWriter
24+
25+
statusCode int
26+
length int
27+
}
28+
29+
// NewResponseRecorder returns a ResponseRecorder that wraps w.
30+
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
31+
return &ResponseRecorder{ResponseWriter: w, statusCode: http.StatusOK}
32+
}
33+
34+
// Write writes b to the underlying response writer and stores how many bytes
35+
// have been written.
36+
func (w *ResponseRecorder) Write(b []byte) (n int, err error) {
37+
n, err = w.ResponseWriter.Write(b)
38+
w.length += n
39+
return
40+
}
41+
42+
// WriteHeader stores and writes the HTTP status code.
43+
func (w *ResponseRecorder) WriteHeader(code int) {
44+
w.statusCode = code
45+
w.ResponseWriter.WriteHeader(code)
46+
}
47+
48+
// StatusCode returns the status-code written to the response or 200 (OK).
49+
func (w *ResponseRecorder) StatusCode() int {
50+
if w.statusCode == 0 {
51+
return http.StatusOK
52+
}
53+
return w.statusCode
54+
}
55+
56+
func AccessLog(next http.Handler) http.Handler {
57+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58+
start := time.Now()
59+
rec := NewResponseRecorder(w)
60+
61+
next.ServeHTTP(rec, r)
62+
status := rec.StatusCode()
63+
length := rec.length
64+
65+
// In this example we use the standard library logger. Structured logs
66+
// may prove more parsable in a production environment.
67+
log.Printf("D! Served HTTP Request %s %s with Response %d %s [%d bytes] in %d ms",
68+
r.Method,
69+
r.URL,
70+
status,
71+
http.StatusText(status),
72+
length,
73+
time.Since(start).Nanoseconds()/1e6,
74+
)
75+
})
76+
}
77+
78+
var logLevelPrefixes = map[resource.LogLevel]string{
79+
resource.LogLevelFatal: "E!",
80+
resource.LogLevelError: "E!",
81+
resource.LogLevelWarn: "W!",
82+
resource.LogLevelInfo: "I!",
83+
resource.LogLevelDebug: "D!",
84+
}
85+
2386
func Example() {
87+
// Configure a log-addapter for the resource pacakge.
88+
resource.LoggerLevel = resource.LogLevelDebug
89+
resource.Logger = func(ctx context.Context, level resource.LogLevel, msg string, fields map[string]interface{}) {
90+
fmt.Printf("%s %s %v", logLevelPrefixes[level], msg, fields)
91+
}
92+
2493
var (
2594
// Define a user resource schema
2695
user = schema.Schema{
@@ -115,12 +184,12 @@ func Example() {
115184
}
116185
)
117186

118-
// Create a REST API root resource
187+
// Create a REST API root resource.
119188
index := resource.NewIndex()
120189

121190
// Add a resource on /users[/:user_id]
122191
users := index.Bind("users", user, mem.NewHandler(), resource.Conf{
123-
// We allow all REST methods
192+
// We allow all REST methods.
124193
// (rest.ReadWrite is a shortcut for []rest.Mode{Create, Read, Update, Delete, List})
125194
AllowedModes: resource.ReadWrite,
126195
})
@@ -132,53 +201,32 @@ func Example() {
132201
AllowedModes: []resource.Mode{resource.Read, resource.List, resource.Create, resource.Delete},
133202
})
134203

135-
// Add a friendly alias to public posts
204+
// Add a friendly alias to public posts.
136205
// (equivalent to /users/:user_id/posts?filter={"public":true})
137206
posts.Alias("public", url.Values{"filter": []string{"{\"public\"=true}"}})
138207

139-
// Create API HTTP handler for the resource graph
208+
// Create API HTTP handler for the resource graph.
209+
var api http.Handler
140210
api, err := rest.NewHandler(index)
141211
if err != nil {
142-
log.Fatal().Err(err).Msg("Invalid API configuration")
212+
log.Printf("E! Invalid API configuration: %s", err)
213+
os.Exit(1)
143214
}
144215

145-
// Init an alice handler chain (use your preferred one)
146-
c := alice.New()
147-
148-
// Install a logger
149-
c = c.Append(hlog.NewHandler(log.With().Logger()))
150-
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
151-
hlog.FromRequest(r).Info().
152-
Str("method", r.Method).
153-
Str("url", r.URL.String()).
154-
Int("status", status).
155-
Int("size", size).
156-
Dur("duration", duration).
157-
Msg("")
158-
}))
159-
c = c.Append(hlog.RequestHandler("req"))
160-
c = c.Append(hlog.RemoteAddrHandler("ip"))
161-
c = c.Append(hlog.UserAgentHandler("ua"))
162-
c = c.Append(hlog.RefererHandler("ref"))
163-
c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
164-
resource.LoggerLevel = resource.LogLevelDebug
165-
resource.Logger = func(ctx context.Context, level resource.LogLevel, msg string, fields map[string]interface{}) {
166-
zerolog.Ctx(ctx).WithLevel(zerolog.Level(level)).Fields(fields).Msg(msg)
167-
}
168-
169-
// Log API access
170-
c = c.Append(xaccess.NewHandler())
171-
172216
// Add CORS support with passthrough option on so rest-layer can still
173-
// handle OPTIONS method
174-
c = c.Append(cors.New(cors.Options{OptionsPassthrough: true}).Handler)
217+
// handle OPTIONS method.
218+
api = cors.New(cors.Options{OptionsPassthrough: true}).Handler(api)
219+
220+
// Wrap the api & cors handler with an access log middleware.
221+
api = AccessLog(api)
175222

176-
// Bind the API under /api/ path
177-
http.Handle("/api/", http.StripPrefix("/api/", c.Then(api)))
223+
// Bind the API under the /api/ path.
224+
http.Handle("/api/", http.StripPrefix("/api/", api))
178225

179-
// Serve it
180-
log.Info().Msg("Serving API on http://localhost:8080")
226+
// Serve it.
227+
log.Printf("I! Serving API on http://localhost:8080")
181228
if err := http.ListenAndServe(":8080", nil); err != nil {
182-
log.Fatal().Err(err).Msg("")
229+
log.Printf("E! Cannot serve API: %s", err)
230+
os.Exit(1)
183231
}
184232
}

examples/go.mod

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module github.com/rs/rest-layer/examples
2+
3+
require (
4+
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
5+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
6+
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
7+
github.com/jtolds/gls v4.2.1+incompatible // indirect
8+
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da
9+
github.com/rs/cors v1.6.0
10+
github.com/rs/rest-layer v0.1.0
11+
github.com/rs/rest-layer-hystrix v0.0.0-20170801073253-2b89f63b98ec
12+
github.com/rs/xaccess v0.0.0-20160803170743-f63036252bcc
13+
github.com/rs/xhandler v0.0.0-20170707052532-1eb70cf1520d // indirect
14+
github.com/rs/xlog v0.0.0-20171227185259-131980fab91b // indirect
15+
github.com/rs/xstats v0.0.0-20170813190920-c67367528e16 // indirect
16+
github.com/rs/zerolog v1.11.0
17+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
18+
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect
19+
github.com/zenazn/goji v0.9.0 // indirect
20+
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76 // indirect
21+
)
22+
23+
replace github.com/rs/rest-layer => ./../

examples/go.sum

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 h1:rFw4nCn9iMW+Vajsk51NtYIcwSTkXr+JGrMd36kTDJw=
2+
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
6+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
7+
github.com/evanphx/json-patch v4.1.0+incompatible h1:K1MDoo4AZ4wU0GIU/fPmtZg7VpzLjCxu+UwBD1FvwOc=
8+
github.com/evanphx/json-patch v4.1.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
9+
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
10+
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
11+
github.com/graphql-go/graphql v0.7.6 h1:3Bn1IFB5OvPoANEfu03azF8aMyks0G/H6G1XeTfYbM4=
12+
github.com/graphql-go/graphql v0.7.6/go.mod h1:k6yrAYQaSP59DC5UVxbgxESlmVyojThKdORUqGDGmrI=
13+
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
14+
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
15+
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da h1:5y58+OCjoHCYB8182mpf/dEsq0vwTKPOo4zGfH0xW9A=
16+
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da/go.mod h1:oLH0CmIaxCGXD67VKGR5AacGXZSMznlmeqM8RzPrcY8=
17+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
18+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
20+
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
21+
github.com/rs/rest-layer-hystrix v0.0.0-20170801073253-2b89f63b98ec h1:kZpeRs9xIb78yukn4OnXQmRk0OluJcuKSHWrsV4ekRw=
22+
github.com/rs/rest-layer-hystrix v0.0.0-20170801073253-2b89f63b98ec/go.mod h1:OL5uSpiXaPH0ur9ni/u/CJi4MLxL3Hu1dK2h4jnF1y0=
23+
github.com/rs/xaccess v0.0.0-20160803170743-f63036252bcc h1:8XPfocIg5yoqhb776Oo1c+on8F4FMtewE+LZzLLaCgQ=
24+
github.com/rs/xaccess v0.0.0-20160803170743-f63036252bcc/go.mod h1:he5kXRmw8ajCYHpcXNno5IYxodyENhcVMp7FQd+qeHM=
25+
github.com/rs/xhandler v0.0.0-20170707052532-1eb70cf1520d h1:8Tt7DYYdFqLlOIuyiE0RluKem4T+048AUafnIjH80wg=
26+
github.com/rs/xhandler v0.0.0-20170707052532-1eb70cf1520d/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ=
27+
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
28+
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
29+
github.com/rs/xlog v0.0.0-20171227185259-131980fab91b h1:65vbRzwfvVUk63GnEiBy1lsY40FLZQev13NK+LnyHAE=
30+
github.com/rs/xlog v0.0.0-20171227185259-131980fab91b/go.mod h1:PJ0wmxt3GdhZAbIT0S8HQXsHuLt11tPiF8bUKXUV77w=
31+
github.com/rs/xstats v0.0.0-20170813190920-c67367528e16 h1:m0aigb++JZXs+tzTO60LOOKSOXWyr7scDxlaSvU6HN8=
32+
github.com/rs/xstats v0.0.0-20170813190920-c67367528e16/go.mod h1:5Cg6M3g+Dp4RSFNYBtjJxxjksZc00LbESra5Sz6fGSU=
33+
github.com/rs/zerolog v1.11.0 h1:DRuq/S+4k52uJzBQciUcofXx45GrMC6yrEbb/CoK6+M=
34+
github.com/rs/zerolog v1.11.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
35+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
36+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
37+
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w=
38+
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
39+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
40+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
41+
github.com/zenazn/goji v0.9.0 h1:RSQQAbXGArQ0dIDEq+PI6WqN6if+5KHu6x2Cx/GXLTQ=
42+
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
43+
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
44+
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
45+
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76 h1:xx5MUFyRQRbPk6VjWjIE1epE/K5AoDD8QUN116NCy8k=
46+
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/rs/rest-layer
2+
3+
require (
4+
github.com/davecgh/go-spew v1.1.1 // indirect
5+
github.com/evanphx/json-patch v4.1.0+incompatible
6+
github.com/graphql-go/graphql v0.7.6
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/rs/cors v1.6.0
9+
github.com/rs/xid v1.2.1
10+
github.com/stretchr/testify v1.2.2
11+
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85
12+
)

go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/evanphx/json-patch v4.1.0+incompatible h1:K1MDoo4AZ4wU0GIU/fPmtZg7VpzLjCxu+UwBD1FvwOc=
4+
github.com/evanphx/json-patch v4.1.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
5+
github.com/graphql-go/graphql v0.7.6 h1:3Bn1IFB5OvPoANEfu03azF8aMyks0G/H6G1XeTfYbM4=
6+
github.com/graphql-go/graphql v0.7.6/go.mod h1:k6yrAYQaSP59DC5UVxbgxESlmVyojThKdORUqGDGmrI=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
10+
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
11+
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
12+
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
13+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
14+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
15+
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
16+
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

0 commit comments

Comments
 (0)