| 1 |
--- |
| 2 |
title: "Server" |
| 3 |
date: 2019-06-21T10:36:43-07:00 |
| 4 |
showInSidenav: true |
| 5 |
toc: true |
| 6 |
--- |
| 7 |
|
| 8 |
The Go CDK's `server` package provides a pre-configured HTTP server with |
| 9 |
diagnostic hooks for request logging, health checks, and trace exporting via |
| 10 |
OpenCensus. This guide will show you how to start up and shut down the server, |
| 11 |
as well as how to work with the request logging and health checks. |
| 12 |
|
| 13 |
## Starting up the server |
| 14 |
|
| 15 |
The Go CDK Server constructor takes an `http.Handler` and an `Options` struct. |
| 16 |
The simplest way to start the server is to use `http.DefaultServeMux` and |
| 17 |
pass `nil` for the options. |
| 18 |
|
| 19 |
{{< goexample src="gocloud.dev/server.ExampleServer" >}} |
| 20 |
|
| 21 |
### Adding a request logger |
| 22 |
|
| 23 |
You can use the `server.Options` struct to specify a request logger. |
| 24 |
|
| 25 |
The example is shown with the Go CDK [`requestlog`](https://godoc.org/gocloud.dev/server/requestlog) package's `NCSALogger`. |
| 26 |
To get logs in the Stackdriver JSON format, use `NewStackdriverLogger` in place |
| 27 |
of `NewNCSALogger`. |
| 28 |
|
| 29 |
{{< goexample src="gocloud.dev/server.ExampleServer_withRequestLogger" >}} |
| 30 |
|
| 31 |
### Adding health checks |
| 32 |
|
| 33 |
The Go CDK `server` package affords a hook for you to define health checks for |
| 34 |
your application and see the results at `/healthz/readiness`. The server also |
| 35 |
runs an endpoint at `/healthz/liveness`, which is a conventional name for a |
| 36 |
liveness check and is where Kubernetes, if you are using it, will look. |
| 37 |
|
| 38 |
Health checks are an important part of application monitoring, and readiness |
| 39 |
checks are subtly different than liveness checks. The liveness check will return |
| 40 |
`200 OK` if the server can serve requests. But because each application may have |
| 41 |
a different definition of what it means to be "healthy" (perhaps your |
| 42 |
application has a dependency on a back end service), you will need to define a |
| 43 |
concrete type to implement the `health.Checker` interface and define a |
| 44 |
`CheckHealth` method specific to your application for readiness checks. |
| 45 |
|
| 46 |
```go |
| 47 |
// customHealthCheck is an example health check. It implements the |
| 48 |
// health.Checker interface and reports the server is healthy when the healthy |
| 49 |
// field is set to true. |
| 50 |
type customHealthCheck struct { |
| 51 |
mu sync.RWMutex |
| 52 |
healthy bool |
| 53 |
} |
| 54 |
|
| 55 |
// customHealthCheck implements the health.Checker interface because it has a |
| 56 |
// CheckHealth method. |
| 57 |
func (h *customHealthCheck) CheckHealth() error { |
| 58 |
h.mu.RLock() |
| 59 |
defer h.mu.RUnlock() |
| 60 |
if !h.healthy { |
| 61 |
return errors.New("not ready yet!") |
| 62 |
} |
| 63 |
return nil |
| 64 |
} |
| 65 |
``` |
| 66 |
|
| 67 |
{{< goexample src="gocloud.dev/server.ExampleServer_withHealthChecks" >}} |
| 68 |
|
| 69 |
## Other Usage Samples |
| 70 |
|
| 71 |
* [Minimal server sample](https://github.com/google/go-cloud/tree/master/samples/server) |
| 72 |
* [Guestbook sample](https://gocloud.dev/tutorials/guestbook/) |