main.go 1.2 KB

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