Add hostname to response
This commit is contained in:
parent
19c1adde6f
commit
2f0c01df7a
@ -3,13 +3,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"mcquay.me/hwt"
|
"mcquay.me/hwt"
|
||||||
pb "mcquay.me/hwt/rpc/hwt"
|
pb "mcquay.me/hwt/rpc/hwt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := &hwt.Server{}
|
hn, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("cannot get hostname: %v", err)
|
||||||
|
}
|
||||||
|
s := &hwt.Server{hn}
|
||||||
th := pb.NewHelloWorldServer(s, nil)
|
th := pb.NewHelloWorldServer(s, nil)
|
||||||
if err := http.ListenAndServe(":8080", th); err != nil {
|
if err := http.ListenAndServe(":8080", th); err != nil {
|
||||||
log.Fatalf("listen and serve: %v", err)
|
log.Fatalf("listen and serve: %v", err)
|
||||||
|
11
hwt.go
11
hwt.go
@ -8,11 +8,18 @@ import (
|
|||||||
pb "mcquay.me/hwt/rpc/hwt"
|
pb "mcquay.me/hwt/rpc/hwt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct{}
|
type Server struct {
|
||||||
|
Hostname string
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
|
func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
|
||||||
if req.Subject == "" {
|
if req.Subject == "" {
|
||||||
return nil, twirp.RequiredArgumentError("subject")
|
return nil, twirp.RequiredArgumentError("subject")
|
||||||
}
|
}
|
||||||
return &pb.HelloResp{Text: fmt.Sprintf("echo: %v", req.Subject)}, nil
|
|
||||||
|
r := &pb.HelloResp{
|
||||||
|
Text: fmt.Sprintf("echo: %v", req.Subject),
|
||||||
|
Hostname: s.Hostname,
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
}
|
}
|
||||||
|
@ -14,4 +14,5 @@ message HelloReq {
|
|||||||
|
|
||||||
message HelloResp {
|
message HelloResp {
|
||||||
string text = 1; // required
|
string text = 1; // required
|
||||||
|
string hostname = 2;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user