Add hostname to response

This commit is contained in:
Stephen McQuay 2018-01-20 09:07:57 -08:00
parent 19c1adde6f
commit 2f0c01df7a
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
3 changed files with 16 additions and 3 deletions

View File

@ -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
View File

@ -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
} }

View File

@ -14,4 +14,5 @@ message HelloReq {
message HelloResp { message HelloResp {
string text = 1; // required string text = 1; // required
string hostname = 2;
} }