handler.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package context
  2. import (
  3. "reflect"
  4. "runtime"
  5. )
  6. // A Handler responds to an HTTP request.
  7. // It writes reply headers and data to the Context.ResponseWriter() and then return.
  8. // Returning signals that the request is finished;
  9. // it is not valid to use the Context after or concurrently with the completion of the Handler call.
  10. //
  11. // Depending on the HTTP client software, HTTP protocol version,
  12. // and any intermediaries between the client and the iris server,
  13. // it may not be possible to read from the Context.Request().Body after writing to the context.ResponseWriter().
  14. // Cautious handlers should read the Context.Request().Body first, and then reply.
  15. //
  16. // Except for reading the body, handlers should not modify the provided Context.
  17. //
  18. // If Handler panics, the server (the caller of Handler) assumes that the effect of the panic was isolated to the active request.
  19. // It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
  20. type Handler func(Context)
  21. // Handlers is just a type of slice of []Handler.
  22. //
  23. // See `Handler` for more.
  24. type Handlers []Handler
  25. // HandlerName returns the name, the handler function informations.
  26. // Same as `context.HandlerName`.
  27. func HandlerName(h Handler) string {
  28. pc := reflect.ValueOf(h).Pointer()
  29. // l, n := runtime.FuncForPC(pc).FileLine(pc)
  30. // return fmt.Sprintf("%s:%d", l, n)
  31. return runtime.FuncForPC(pc).Name()
  32. }