main.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "flag"
  4. "net/http"
  5. "github.com/tal-tech/go-zero/core/conf"
  6. "github.com/tal-tech/go-zero/core/logx"
  7. "github.com/tal-tech/go-zero/core/service"
  8. "github.com/tal-tech/go-zero/rest"
  9. "github.com/tal-tech/go-zero/rest/httpx"
  10. "github.com/tal-tech/go-zero/zrpc"
  11. "github.com/zeromicro/zero-examples/tracing/remote/portal"
  12. )
  13. var (
  14. configFile = flag.String("f", "config.json", "the config file")
  15. client zrpc.Client
  16. )
  17. type Config struct {
  18. rest.RestConf
  19. Portal zrpc.RpcClientConf
  20. }
  21. func handle(w http.ResponseWriter, r *http.Request) {
  22. conn := client.Conn()
  23. greet := portal.NewPortalClient(conn)
  24. resp, err := greet.Portal(r.Context(), &portal.PortalRequest{
  25. Name: "kevin",
  26. })
  27. if err != nil {
  28. httpx.WriteJson(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
  29. } else {
  30. httpx.OkJson(w, resp.Response)
  31. }
  32. }
  33. func main() {
  34. flag.Parse()
  35. var c Config
  36. conf.MustLoad(*configFile, &c)
  37. client = zrpc.MustNewClient(c.Portal)
  38. engine := rest.MustNewServer(rest.RestConf{
  39. ServiceConf: service.ServiceConf{
  40. Log: logx.LogConf{
  41. Mode: "console",
  42. },
  43. },
  44. Port: c.Port,
  45. })
  46. defer engine.Stop()
  47. engine.AddRoute(rest.Route{
  48. Method: http.MethodGet,
  49. Path: "/",
  50. Handler: handle,
  51. })
  52. engine.Start()
  53. }