代码拉取完成,页面将自动刷新
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/zrpc"
)
var (
configFile = flag.String("f", "config.json", "the config file")
client zrpc.Client
)
type Config struct {
rest.RestConf
Portal zrpc.RpcClientConf
}
func handle(w http.ResponseWriter, r *http.Request) {
conn := client.Conn()
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
conf.MustLoad(*configFile, &c)
client = zrpc.MustNewClient(c.Portal)
engine := rest.MustNewServer(rest.RestConf{
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Mode: "console",
},
},
Port: c.Port,
})
defer engine.Stop()
engine.AddRoute(rest.Route{
Method: http.MethodGet,
Path: "/",
Handler: handle,
})
engine.Start()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。