另客网go项目公用的代码库

context.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package linker
  2. import (
  3. "bytes"
  4. "context"
  5. "strings"
  6. "github.com/wpajqz/linker/codec"
  7. )
  8. type (
  9. Context interface {
  10. WithValue(key interface{}, value interface{}) Context
  11. Value(key interface{}) interface{}
  12. ParseParam(data interface{}) error
  13. Success(body interface{})
  14. Error(code int, message string)
  15. Write(operator string, body interface{}) (int, error)
  16. SetRequestProperty(key, value string)
  17. GetRequestProperty(key string) string
  18. SetResponseProperty(key, value string)
  19. GetResponseProperty(key string) string
  20. LocalAddr() string
  21. RemoteAddr() string
  22. }
  23. defaultContext struct {
  24. config Config
  25. operateType uint32
  26. sequence int64
  27. body []byte
  28. Context context.Context
  29. Request, Response struct {
  30. Header, Body []byte
  31. }
  32. }
  33. )
  34. func (dc *defaultContext) WithValue(key interface{}, value interface{}) Context {
  35. dc.Context = context.WithValue(dc.Context, key, value)
  36. return dc
  37. }
  38. func (dc *defaultContext) Value(key interface{}) interface{} {
  39. return dc.Context.Value(key)
  40. }
  41. func (dc *defaultContext) ParseParam(data interface{}) error {
  42. r, err := codec.NewCoder(dc.config.ContentType)
  43. if err != nil {
  44. return err
  45. }
  46. return r.Decoder(dc.body, data)
  47. }
  48. func (dc *defaultContext) SetRequestProperty(key, value string) {
  49. v := dc.GetRequestProperty(key)
  50. if v != "" {
  51. dc.Request.Header = bytes.Trim(dc.Request.Header, key+"="+value+";")
  52. }
  53. dc.Request.Header = append(dc.Request.Header, []byte(key+"="+value+";")...)
  54. }
  55. func (dc *defaultContext) GetRequestProperty(key string) string {
  56. values := strings.Split(string(dc.Request.Header), ";")
  57. for _, value := range values {
  58. kv := strings.Split(value, "=")
  59. if kv[0] == key {
  60. return kv[1]
  61. }
  62. }
  63. return ""
  64. }
  65. func (dc *defaultContext) SetResponseProperty(key, value string) {
  66. v := dc.GetResponseProperty(key)
  67. if v != "" {
  68. dc.Response.Header = bytes.Trim(dc.Response.Header, key+"="+value+";")
  69. }
  70. dc.Response.Header = append(dc.Response.Header, []byte(key+"="+value+";")...)
  71. }
  72. func (dc *defaultContext) GetResponseProperty(key string) string {
  73. values := strings.Split(string(dc.Response.Header), ";")
  74. for _, value := range values {
  75. kv := strings.Split(value, "=")
  76. if kv[0] == key {
  77. return kv[1]
  78. }
  79. }
  80. return ""
  81. }
  82. func (dc *defaultContext) Success(body interface{}) {}
  83. func (dc *defaultContext) Error(code int, message string) {}
  84. func (dc *defaultContext) Write(operator string, body interface{}) (int, error) { return 0, nil }
  85. func (dc *defaultContext) LocalAddr() string { return "" }
  86. func (dc *defaultContext) RemoteAddr() string { return "" }