Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { RequestDecoder func(*http.Request, interface{}) error ResponseEncoder func(http.ResponseWriter, *http.Request, interface{}) ErrorEncoder func(http.ResponseWriter, *http.Request, error) Validate bool // set to true to validate request after decode using Validatable interface }
Config is the hrpc config
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager type
Example ¶
m := New(Config{ RequestDecoder: func(r *http.Request, dst interface{}) error { return json.NewDecoder(r.Body).Decode(dst) }, ResponseEncoder: func(w http.ResponseWriter, r *http.Request, res interface{}) { w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(res) }, ErrorEncoder: func(w http.ResponseWriter, r *http.Request, err error) { res := &struct { Error string `json:"error"` }{err.Error()} w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusInternalServerError) json.NewEncoder(w).Encode(res) }, Validate: true, }) http.Handle("/user.get", m.Handler(func(ctx context.Context, req *struct { ID string `json:"id"` }) (map[string]string, error) { return map[string]string{ "user_id": req.ID, "user_name": "User " + req.ID, }, nil })) // $ curl -X POST -d '{"id":"123"}' http://localhost:8080/user.get // {"user_id":"123","user_name":"User 123"} http.Handle("/upload", m.Handler(func(r *http.Request) error { buf := &bytes.Buffer{} _, err := io.Copy(buf, r.Body) if err != nil { return err } fmt.Printf("upload data: %s\n", buf.String()) return nil })) // $ echo "test data" | curl -X POST -d "@-" http://localhost:8080/upload // upload data: test data http.ListenAndServe(":8080", nil)
Output:
func (*Manager) Handler ¶
Handler func, f must be a function which have at least 2 inputs and 2 outputs. first input must be a context. second input can be anything which will pass to RequestDecoder function. first output must be the result which will pass to success handler. second output must be an error interface which will pass to error handler if not nil.
Click to show internal directories.
Click to hide internal directories.