go-zero/example/tracing/edge/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package main
import (
"flag"
"net/http"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/service"
"github.com/tal-tech/go-zero/example/tracing/remote/portal"
"github.com/tal-tech/go-zero/rest"
"github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rpcx"
2020-07-26 17:09:05 +08:00
)
var (
configFile = flag.String("f", "config.json", "the config file")
client rpcx.Client
2020-07-26 17:09:05 +08:00
)
2020-09-18 08:53:06 +08:00
type Config struct {
rest.RestConf
Portal rpcx.RpcClientConf
}
2020-07-26 17:09:05 +08:00
func handle(w http.ResponseWriter, r *http.Request) {
2020-08-06 20:55:38 +08:00
conn := client.Conn()
2020-07-26 17:09:05 +08:00
greet := portal.NewPortalClient(conn)
resp, err := greet.Portal(r.Context(), &portal.PortalRequest{
Name: "kevin",
})
if err != nil {
httpx.WriteJson(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
} else {
httpx.OkJson(w, resp.Response)
}
}
func main() {
flag.Parse()
2020-09-18 08:53:06 +08:00
var c Config
2020-07-26 17:09:05 +08:00
conf.MustLoad(*configFile, &c)
2020-09-18 08:53:06 +08:00
client = rpcx.MustNewClient(c.Portal)
2020-07-31 11:45:16 +08:00
engine := rest.MustNewServer(rest.RestConf{
2020-07-26 17:09:05 +08:00
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Mode: "console",
},
},
2020-09-18 08:53:06 +08:00
Port: c.Port,
2020-07-26 17:09:05 +08:00
})
defer engine.Stop()
2020-07-31 11:14:48 +08:00
engine.AddRoute(rest.Route{
2020-07-26 17:09:05 +08:00
Method: http.MethodGet,
Path: "/",
Handler: handle,
})
engine.Start()
}