user.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "git.chuangxin1.com/csacred/toolkit"
  8. "github.com/chuangxin1/httprouter"
  9. "github.com/go-kit/kit/log"
  10. users "git.chuangxin1.com/csacred/toolkit/example/cuser/service"
  11. )
  12. func genUserHandlers(
  13. ctx context.Context,
  14. router *httprouter.Router,
  15. options toolkit.ServiceOptions,
  16. logger log.Logger) {
  17. ver := "v1"
  18. tags := []string{ver}
  19. passingOnly := true
  20. name := users.ServiceName
  21. instancer, err := toolkit.NewConsulInstancer(
  22. options,
  23. name,
  24. tags,
  25. passingOnly,
  26. logger)
  27. if err != nil {
  28. fmt.Println("NewConsulInstancer", err.Error())
  29. os.Exit(0)
  30. }
  31. handers := users.GenGatewayHandlers(ctx, instancer, logger)
  32. prefix := "/api/" + name + "/" + ver
  33. //ctx = context.WithValue(ctx, contextKeyPrefix, prefix)
  34. for i := range handers {
  35. fmt.Println(handers[i].Method, prefix+handers[i].Router)
  36. hander := handers[i].Hander
  37. router.Handle(
  38. handers[i].Method,
  39. prefix+handers[i].Router,
  40. func(w http.ResponseWriter, req *http.Request) {
  41. req = req.WithContext(context.WithValue(
  42. req.Context(),
  43. toolkit.ContextKeyGateWayPrefix,
  44. prefix))
  45. hander.ServeHTTP(w, req)
  46. })
  47. //handers[i].Hander)
  48. }
  49. }