-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
81 lines (69 loc) · 1.83 KB
/
handler.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package sample
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"runtime/debug"
"time"
"github.com/pkg/errors"
)
// Client has the information needed to work as a httpway plugin.
type Client struct {
url *url.URL
}
// Close the client.
func (Client) Close(ctx context.Context) error {
return nil
}
// Default is a sample HTTP handler that track the time of a request.
func (c Client) Default(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
r.Host = c.url.Host
r.URL.Host = c.url.Host
r.URL.Scheme = c.url.Scheme
t := time.Now()
next.ServeHTTP(w, r)
fmt.Printf("request duration: %s\n", time.Since(t).String())
}
return http.HandlerFunc(fn)
}
// Panic is used to recover from panics to avoid the server shuting down.
func (Client) Panic(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rv := recover(); rv != nil {
fmt.Fprintf(os.Stderr, "panic: %+v\n", rv)
debug.PrintStack()
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// NotFound is invoked to handle not found errors.
func (Client) NotFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
func (c *Client) init(config map[string]interface{}) error {
rawHost, ok := config["host"].(string)
if !ok {
return errors.New("casting host to string error")
}
var err error
c.url, err = url.Parse(rawHost)
if err != nil {
return errors.Wrapf(err, "parse url '%s' error", rawHost)
}
return nil
}
// NewClient return a initialized client.
func NewClient(config map[string]interface{}) (Client, error) {
var c Client
if err := c.init(config); err != nil {
return c, errors.Wrap(err, "initialization error")
}
return c, nil
}