MCPcopy Create free account
hub / github.com/Shopify/goose / ExampleNewServer

Function ExampleNewServer

srvutil/server_test.go:22–47  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20)
21
22func ExampleNewServer() {
23 tb := &tomb.Tomb{}
24 sl := FuncServlet("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
25 name := mux.Vars(r)["name"]
26 fmt.Fprintf(w, "hello %s", name)
27 })
28
29 sl = UseServlet(sl,
30 // Should be first to properly add tags and logging fields to the context
31 RequestContextMiddleware,
32 NewRequestMetricsMiddleware(&RequestMetricsMiddlewareConfig{BodyLogPredicate: LogErrorBody}),
33 safely.Middleware,
34 )
35
36 s := NewServer(tb, "127.0.0.1:0", sl)
37 defer s.Tomb().Kill(nil)
38 safely.Run(s)
39
40 u := httpScheme + s.Addr().String() + "/hello/world"
41
42 res, _ := http.Get(u)
43 io.Copy(os.Stdout, res.Body)
44
45 // Output:
46 // hello world
47}
48
49func TestNewServer(t *testing.T) {
50 logOutput := &syncio.Buffer{}

Callers

nothing calls this directly

Calls 10

AddrMethod · 0.95
RunFunction · 0.92
FuncServletFunction · 0.85
UseServletFunction · 0.85
NewServerFunction · 0.85
KillMethod · 0.65
TombMethod · 0.65
GetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected