Skip to content

Commit fb7f782

Browse files
authored
Merge pull request #19 from wadearnold/stringsvc2-additional-steps
Separate transport and service into respective files
2 parents 2b63f46 + 853eba8 commit fb7f782

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

_src/examples/stringsvc.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,32 @@ $ curl -XPOST -d'{"s":"hello, world"}' localhost:8080/count
195195

196196
No service can be considered production-ready without thorough logging and instrumentation.
197197

198+
## Seperation of concerns
199+
200+
Separating each layer of the call graph into individual files makes a go-kit project easier to read as you increase the number of service endpoints. Our first example [stringsvc1](https://github.com/go-kit/kit/blob/master/examples/stringsvc1) had all of these layers in a single main file. Before we had more complexity separate your code into the following files and leave all remaining code in main.go
201+
202+
Place your **services** into a service.go file with the following functions and types.
203+
204+
```
205+
type StringService
206+
func stringService
207+
var ErrEmpty
208+
```
209+
210+
Place your **transports** into a transport.go file with the following functions and types.
211+
212+
```
213+
func makeUppercaseEndpoint
214+
func makeCountEndpoint
215+
func decodeUppercaseRequest
216+
func decodeCountRequest
217+
func encodeResponse
218+
type uppercaseRequest
219+
type uppercaseResponse
220+
type countRequest
221+
type countResponse
222+
```
223+
198224
## Transport logging
199225

200226
Any component that needs to log should treat the logger like a dependency, same as a database connection.
@@ -224,7 +250,15 @@ func loggingMiddleware(logger log.Logger) Middleware {
224250
}
225251
```
226252

227-
And wire it into each of our handlers.
253+
Use the [go-kit log](https://gokit.io/faq/#logging-mdash-why-is-package-log-so-different) package and remove the standard libraries [log](https://golang.org/pkg/log/). You will need to remove `log.Fatal` from main.go
254+
255+
```go
256+
import (
257+
"github.com/go-kit/kit/log"
258+
)
259+
```
260+
261+
And wire it into each of our handlers.
228262

229263
```go
230264
logger := log.NewLogfmtLogger(os.Stderr)

0 commit comments

Comments
 (0)