forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (53 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()
}