-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_create_endpoint_test.go
48 lines (43 loc) · 1.55 KB
/
example_create_endpoint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package npoint_test
import (
"database/sql"
"net/http"
"github.com/muir/nject"
"github.com/muir/npoint"
)
// CreateEndpoint is the simplest way to start using the npoint framework. It
// generates an http.HandlerFunc from a list of handlers. The handlers will be called
// in order. In the example below, first WriteErrorResponse() will be called. It
// has an inner() func that it uses to invoke the rest of the chain. When
// WriteErrorResponse() calls its inner() function, the db injector returned by
// InjectDB is called. If that does not return error, then the inline function below
// to handle the endpint is called.
func ExampleCreateEndpoint() {
mux := http.NewServeMux()
mux.HandleFunc("/my/endpoint", npoint.CreateEndpoint(
WriteErrorResponse,
InjectDB("postgres", "postgres://..."),
func(r *http.Request, db *sql.DB, w http.ResponseWriter) error {
// Write response to w or return error...
return nil
}))
}
// WriteErrorResponse invokes the remainder of the handler chain by calling inner().
func WriteErrorResponse(inner func() nject.TerminalError, w http.ResponseWriter) {
err := inner()
if err != nil {
_, _ = w.Write([]byte(err.Error()))
w.WriteHeader(500)
}
}
// InjectDB returns a handler function that opens a database connection. If the open
// fails, executation of the handler chain is terminated.
func InjectDB(driver, uri string) func() (nject.TerminalError, *sql.DB) {
return func() (nject.TerminalError, *sql.DB) {
db, err := sql.Open(driver, uri)
if err != nil {
return err, nil
}
return nil, db
}
}