/
main.go
52 lines (43 loc) · 1.13 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
package main
import (
"context"
"log"
"math/rand"
"net/http"
"time"
"protoc-gen-swiftwirp/example/cmd/haberdasher"
"github.com/golang/protobuf/ptypes"
"github.com/twitchtv/twirp"
)
type randomHaberdasher struct{}
func (h *randomHaberdasher) BuyHat(ctx context.Context, hat *haberdasher.Hat) (*haberdasher.Hat, error) {
return hat, nil
}
func (h *randomHaberdasher) MakeHat(ctx context.Context, size *haberdasher.Size) (*haberdasher.Hat, error) {
if int(size.Inches) <= 0 {
return nil, twirp.InvalidArgumentError("Inches", "must be a positive number greater than zero")
}
ts, err := ptypes.TimestampProto(time.Now())
if err != nil {
return nil, err
}
return &haberdasher.Hat{
Size: size.Inches,
Color: []string{"white", "black", "brown", "red", "blue"}[rand.Intn(4)],
Name: []string{"bowler", "baseball cap", "top hat", "derby"}[rand.Intn(3)],
AvailableSizes: []*haberdasher.Size{
{Inches: 10},
{Inches: 20},
},
Roles: []int32{
1,
2,
3,
},
CreatedOn: ts,
}, nil
}
func main() {
server := haberdasher.NewHaberdasherServer(&randomHaberdasher{}, nil)
log.Fatal(http.ListenAndServe(":9000", server))
}