You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/example/tracing/edge/main.go

62 lines
1.3 KiB
Go

4 years ago
package main
import (
"flag"
"net/http"
"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"
4 years ago
)
var (
configFile = flag.String("f", "config.json", "the config file")
client rpcx.Client
4 years ago
)
type Config struct {
rest.RestConf
Portal rpcx.RpcClientConf
}
4 years ago
func handle(w http.ResponseWriter, r *http.Request) {
conn := client.Conn()
4 years ago
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()
var c Config
4 years ago
conf.MustLoad(*configFile, &c)
client = rpcx.MustNewClient(c.Portal)
engine := rest.MustNewServer(rest.RestConf{
4 years ago
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Mode: "console",
},
},
Port: c.Port,
4 years ago
})
defer engine.Stop()
engine.AddRoute(rest.Route{
4 years ago
Method: http.MethodGet,
Path: "/",
Handler: handle,
})
engine.Start()
}