greeter_server.go 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "go-common/library/net/rpc/warden"
  7. pb "go-common/library/net/rpc/warden/proto/testproto"
  8. xtime "go-common/library/time"
  9. "github.com/prometheus/client_golang/prometheus/promhttp"
  10. )
  11. var (
  12. config = &warden.ServerConfig{Timeout: xtime.Duration(time.Second)}
  13. )
  14. func main() {
  15. newServer()
  16. }
  17. type hello struct {
  18. }
  19. func (s *hello) SayHello(c context.Context, in *pb.HelloRequest) (out *pb.HelloReply, err error) {
  20. out = new(pb.HelloReply)
  21. out.Message = in.Name
  22. return
  23. }
  24. func (s *hello) StreamHello(ss pb.Greeter_StreamHelloServer) error {
  25. return nil
  26. }
  27. func newServer() {
  28. server := warden.NewServer(config)
  29. pb.RegisterGreeterServer(server.Server(), &hello{})
  30. go func() {
  31. http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  32. h := promhttp.Handler()
  33. h.ServeHTTP(w, r)
  34. })
  35. http.ListenAndServe("0.0.0.0:9998", nil)
  36. }()
  37. err := server.Run(":9999")
  38. if err != nil {
  39. return
  40. }
  41. }