2018-01-16 20:57:04 -08:00
|
|
|
package hwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
fmt "fmt"
|
2018-01-26 17:15:05 -08:00
|
|
|
"go/src/math/rand"
|
|
|
|
"time"
|
2018-01-20 08:38:58 -08:00
|
|
|
|
2018-01-20 09:01:46 -08:00
|
|
|
"github.com/twitchtv/twirp"
|
2018-01-20 08:38:58 -08:00
|
|
|
pb "mcquay.me/hwt/rpc/hwt"
|
2018-01-16 20:57:04 -08:00
|
|
|
)
|
|
|
|
|
2018-01-20 09:07:57 -08:00
|
|
|
type Server struct {
|
|
|
|
Hostname string
|
|
|
|
}
|
2018-01-16 20:57:04 -08:00
|
|
|
|
2018-01-20 08:38:58 -08:00
|
|
|
func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
|
2018-01-20 09:01:46 -08:00
|
|
|
if req.Subject == "" {
|
|
|
|
return nil, twirp.RequiredArgumentError("subject")
|
|
|
|
}
|
2018-01-20 09:07:57 -08:00
|
|
|
|
2018-01-21 20:18:28 -08:00
|
|
|
u, err := getUser(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, twirp.InternalErrorWith(err)
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:15:05 -08:00
|
|
|
if rand.Int()%100 == 0 {
|
|
|
|
rest := time.Duration(rand.Intn(50)) * time.Millisecond
|
|
|
|
time.Sleep(rest)
|
|
|
|
}
|
|
|
|
|
2018-01-20 09:07:57 -08:00
|
|
|
r := &pb.HelloResp{
|
2018-01-21 20:18:28 -08:00
|
|
|
Text: fmt.Sprintf("%s said: %v", u, req.Subject),
|
2018-01-20 09:07:57 -08:00
|
|
|
Hostname: s.Hostname,
|
|
|
|
}
|
|
|
|
return r, nil
|
2018-01-16 20:57:04 -08:00
|
|
|
}
|