From 72827704d2e470b4ccd379e4a10dec271e629a82 Mon Sep 17 00:00:00 2001 From: zhaoyang Date: Wed, 8 Dec 2021 18:38:03 +0800 Subject: [PATCH] feat: add joke api (#294) * feat: add joke api close #124 * chore: count boundary * fix: bug fix chore: update README.md * feat: add publicapi.json --- go.mod | 1 + joke/.gitignore | 2 + joke/Dockerfile | 3 + joke/Makefile | 28 ++++ joke/README.md | 8 + joke/examples.json | 37 +++++ joke/generate.go | 2 + joke/handler/joke.go | 43 +++++ joke/main.go | 87 ++++++++++ joke/micro.mu | 1 + joke/model/joke.go | 132 +++++++++++++++ joke/proto/joke.pb.go | 314 ++++++++++++++++++++++++++++++++++++ joke/proto/joke.pb.micro.go | 95 +++++++++++ joke/proto/joke.proto | 28 ++++ joke/publicapi.json | 6 + 15 files changed, 787 insertions(+) create mode 100644 joke/.gitignore create mode 100644 joke/Dockerfile create mode 100644 joke/Makefile create mode 100644 joke/README.md create mode 100644 joke/examples.json create mode 100644 joke/generate.go create mode 100644 joke/handler/joke.go create mode 100644 joke/main.go create mode 100644 joke/micro.mu create mode 100644 joke/model/joke.go create mode 100644 joke/proto/joke.pb.go create mode 100644 joke/proto/joke.pb.micro.go create mode 100644 joke/proto/joke.proto create mode 100644 joke/publicapi.json diff --git a/go.mod b/go.mod index 3fcc777..e81b6fe 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/patrickmn/go-cache v2.1.0+incompatible github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 github.com/peterbourgon/diskv/v3 v3.0.1 + github.com/pkg/errors v0.9.1 github.com/pquerna/otp v1.3.0 github.com/robfig/cron/v3 v3.0.1 github.com/sendgrid/rest v2.6.4+incompatible // indirect diff --git a/joke/.gitignore b/joke/.gitignore new file mode 100644 index 0000000..cb9f94e --- /dev/null +++ b/joke/.gitignore @@ -0,0 +1,2 @@ + +joke diff --git a/joke/Dockerfile b/joke/Dockerfile new file mode 100644 index 0000000..a3f18ae --- /dev/null +++ b/joke/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD joke /joke +ENTRYPOINT [ "/joke" ] diff --git a/joke/Makefile b/joke/Makefile new file mode 100644 index 0000000..93f1664 --- /dev/null +++ b/joke/Makefile @@ -0,0 +1,28 @@ + +GOPATH:=$(shell go env GOPATH) +.PHONY: init +init: + go get -u github.com/golang/protobuf/proto + go get -u github.com/golang/protobuf/protoc-gen-go + go get github.com/micro/micro/v3/cmd/protoc-gen-micro + go get github.com/micro/micro/v3/cmd/protoc-gen-openapi + +.PHONY: api +api: + protoc --openapi_out=. --proto_path=. proto/joke.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/joke.proto + +.PHONY: build +build: + go build -o joke *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t joke:latest diff --git a/joke/README.md b/joke/README.md new file mode 100644 index 0000000..fef8838 --- /dev/null +++ b/joke/README.md @@ -0,0 +1,8 @@ +Funny Jokes + +# Joke Service + +This is the Joke service + +Leveraged by https://github.com/taivop/joke-dataset + diff --git a/joke/examples.json b/joke/examples.json new file mode 100644 index 0000000..9a2ed9c --- /dev/null +++ b/joke/examples.json @@ -0,0 +1,37 @@ +{ + "random": [ + { + "title": "Get random n jokes", + "description": "Leveraged by https://github.com/taivop/joke-dataset", + "run_check": false, + "request": { + "count": 3 + }, + "response": { + "jokes": [ + { + "id": "3rpt4l", + "title": "In five-card poker, six-high beats a pair...", + "body": "", + "category": "", + "source": "https://www.reddit.com/r/jokes" + }, + { + "id": "5sbykr", + "title": "Who choked harder the Golden State Warriors or the Atlanta Falcons?", + "body": "Hillary Clinton", + "category": "", + "source": "https://www.reddit.com/r/jokes" + }, + { + "id": "52uuuz", + "title": "I was wondering why the ball was getting bigger.", + "body": "Then it struck me.", + "category": "", + "source": "https://www.reddit.com/r/jokes" + } + ] + } + } + ] +} diff --git a/joke/generate.go b/joke/generate.go new file mode 100644 index 0000000..96f431a --- /dev/null +++ b/joke/generate.go @@ -0,0 +1,2 @@ +package main +//go:generate make proto diff --git a/joke/handler/joke.go b/joke/handler/joke.go new file mode 100644 index 0000000..53728e2 --- /dev/null +++ b/joke/handler/joke.go @@ -0,0 +1,43 @@ +package handler + +import ( + "context" + "math/rand" + + "github.com/micro/services/joke/model" + pb "github.com/micro/services/joke/proto" +) + +type Joke struct{} + +// Random is used to get random jokes +func (e *Joke) Random(_ context.Context, req *pb.RandomRequest, rsp *pb.RandomResponse) error { + jokes := model.GetAllJokes() + count := req.Count + + if count <= 0 { + count = 1 + } else if count > 10 { + count = 10 + } + + if count > int32(len(jokes)) { + count = int32(len(jokes)) + } + + for i := int32(0); i < count; i++ { + random := jokes[rand.Intn(len(jokes))] + + info := &pb.JokeInfo{ + Id: random.Id, + Title: random.Title, + Body: random.Body, + Category: random.Category, + Source: random.Source, + } + + rsp.Jokes = append(rsp.Jokes, info) + } + + return nil +} diff --git a/joke/main.go b/joke/main.go new file mode 100644 index 0000000..6559fae --- /dev/null +++ b/joke/main.go @@ -0,0 +1,87 @@ +package main + +import ( + "errors" + "math/rand" + "strings" + "sync" + "time" + + "github.com/micro/services/joke/handler" + "github.com/micro/services/joke/model" + pb "github.com/micro/services/joke/proto" + + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" +) + +var ( + sources = []model.JokeSource{ + { + Source: "https://www.reddit.com/r/jokes", + Api: "https://raw.githubusercontent.com/taivop/joke-dataset/master/reddit_jokes.json", + }, + { + Source: "http://wocka.com/", + Api: "https://raw.githubusercontent.com/taivop/joke-dataset/master/wocka.json", + }, + { + Source: "http://stupidstuff.org/jokes/", + Api: "https://raw.githubusercontent.com/taivop/joke-dataset/master/stupidstuff.json", + }, + } +) + +// loadJokes is used to load jokes in store +func loadJokes() error { + wg := sync.WaitGroup{} + errs := make([]string, 0) + for _, v := range sources { + wg.Add(1) + + go func(s model.JokeSource) { + defer wg.Done() + + err := model.NewJoke(s).Load() + + if err != nil { + logger.Errorf("load jokes error: %s", err) + errs = append(errs, err.Error()) + return + } + + }(v) + + } + + wg.Wait() + + if len(errs) != 0 { + return errors.New("load jokes error: " + strings.Join(errs, ";")) + } + + logger.Infof("loaded jokes: %d\n", len(model.GetAllJokes())) + + return nil +} + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func main() { + // Create service + srv := service.New( + service.Name("joke"), + service.Version("latest"), + service.BeforeStart(loadJokes), + ) + + // Register handler + pb.RegisterJokeHandler(srv.Server(), new(handler.Joke)) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/joke/micro.mu b/joke/micro.mu new file mode 100644 index 0000000..f5b533d --- /dev/null +++ b/joke/micro.mu @@ -0,0 +1 @@ +service joke diff --git a/joke/model/joke.go b/joke/model/joke.go new file mode 100644 index 0000000..51dc8fe --- /dev/null +++ b/joke/model/joke.go @@ -0,0 +1,132 @@ +package model + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" + + "github.com/micro/micro/v3/service/logger" + "github.com/pkg/errors" +) + +var ( + // jokes is a memory store to save all jokes + jokes []Joke +) + +// GetAllJokes return all jokes +func GetAllJokes() []Joke { + return jokes +} + +// JokeSource is the source of joke, contains source name and source api +// The Api response content must be an array, and every object has contains `title` or `body` field +// eg: +// [ +// { +// "title": "joke's title" +// "body": "joke's body" +// } +// ] +type JokeSource struct { + Source string + Api string +} + +type Joke struct { + Id string + Title string + Body string + Category string + Source string +} + +// GetKey is used to get jokes uniq key +func (i Joke) GetKey() string { + return fmt.Sprintf("%s-%s", i.Source, i.Id) +} + +type joke struct { + source JokeSource +} + +func NewJoke(s JokeSource) *joke { + return &joke{ + source: s, + } +} + +// get is used to get jokes from api +func (j *joke) get() (jokes []Joke, err error) { + + client := http.Client{ + Timeout: 30 * time.Second, + } + resp, err := client.Get(j.source.Api) + if err != nil { + return nil, errors.Wrap(err, "request jokes api error: "+err.Error()) + } + + if resp.StatusCode != http.StatusOK { + return nil, errors.Wrap(err, "request jokes api status is not ok: "+resp.Status) + } + + defer func() { + if err := resp.Body.Close(); err != nil { + logger.Errorf("close response error, %v", err) + return + } + }() + + b, _ := ioutil.ReadAll(resp.Body) + + results := make([]map[string]interface{}, 0) + if err := json.Unmarshal(b, &results); err != nil { + return nil, errors.Wrap(err, "json unmarshal jokes api error: "+err.Error()) + } + + for _, r := range results { + info := Joke{} + + info.Id = func() string { + if id, ok := r["id"].(string); ok { + return id + } else if id, ok := r["id"].(float64); ok { + return fmt.Sprintf("%d", int32(id)) + } else { + return "unknown" + } + }() + + info.Source = j.source.Source + info.Title, _ = r["title"].(string) + info.Body, _ = r["body"].(string) + info.Category, _ = r["category"].(string) + + if info.Title == "" && info.Body == "" { + continue + } + + jokes = append(jokes, info) + } + + return jokes, nil +} + +// Load is used to save jokes in memory +func (j *joke) Load() (err error) { + if j.source.Source == "" || j.source.Api == "" { + return errors.New("source or api can not be empty") + } + + js, err := j.get() + if err != nil { + return err + } + + jokes = append(jokes, js...) + + return nil +} diff --git a/joke/proto/joke.pb.go b/joke/proto/joke.pb.go new file mode 100644 index 0000000..50c748a --- /dev/null +++ b/joke/proto/joke.pb.go @@ -0,0 +1,314 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.19.1 +// source: proto/joke.proto + +package joke + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type RandomRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the count of random jokes want, maximum: 10 + Count int32 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"` +} + +func (x *RandomRequest) Reset() { + *x = RandomRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_joke_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RandomRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RandomRequest) ProtoMessage() {} + +func (x *RandomRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_joke_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RandomRequest.ProtoReflect.Descriptor instead. +func (*RandomRequest) Descriptor() ([]byte, []int) { + return file_proto_joke_proto_rawDescGZIP(), []int{0} +} + +func (x *RandomRequest) GetCount() int32 { + if x != nil { + return x.Count + } + return 0 +} + +type JokeInfo struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"` + Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"` + Category string `protobuf:"bytes,4,opt,name=category,proto3" json:"category,omitempty"` + // the source of joke + Source string `protobuf:"bytes,5,opt,name=source,proto3" json:"source,omitempty"` +} + +func (x *JokeInfo) Reset() { + *x = JokeInfo{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_joke_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *JokeInfo) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*JokeInfo) ProtoMessage() {} + +func (x *JokeInfo) ProtoReflect() protoreflect.Message { + mi := &file_proto_joke_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use JokeInfo.ProtoReflect.Descriptor instead. +func (*JokeInfo) Descriptor() ([]byte, []int) { + return file_proto_joke_proto_rawDescGZIP(), []int{1} +} + +func (x *JokeInfo) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +func (x *JokeInfo) GetTitle() string { + if x != nil { + return x.Title + } + return "" +} + +func (x *JokeInfo) GetBody() string { + if x != nil { + return x.Body + } + return "" +} + +func (x *JokeInfo) GetCategory() string { + if x != nil { + return x.Category + } + return "" +} + +func (x *JokeInfo) GetSource() string { + if x != nil { + return x.Source + } + return "" +} + +type RandomResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Jokes []*JokeInfo `protobuf:"bytes,1,rep,name=jokes,proto3" json:"jokes,omitempty"` +} + +func (x *RandomResponse) Reset() { + *x = RandomResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_joke_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RandomResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RandomResponse) ProtoMessage() {} + +func (x *RandomResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_joke_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RandomResponse.ProtoReflect.Descriptor instead. +func (*RandomResponse) Descriptor() ([]byte, []int) { + return file_proto_joke_proto_rawDescGZIP(), []int{2} +} + +func (x *RandomResponse) GetJokes() []*JokeInfo { + if x != nil { + return x.Jokes + } + return nil +} + +var File_proto_joke_proto protoreflect.FileDescriptor + +var file_proto_joke_proto_rawDesc = []byte{ + 0x0a, 0x10, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6a, 0x6f, 0x6b, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x04, 0x6a, 0x6f, 0x6b, 0x65, 0x22, 0x25, 0x0a, 0x0d, 0x52, 0x61, 0x6e, 0x64, + 0x6f, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, + 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, + 0x78, 0x0a, 0x08, 0x4a, 0x6f, 0x6b, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x0e, 0x0a, 0x02, 0x69, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, + 0x69, 0x74, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, + 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, + 0x79, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x22, 0x36, 0x0a, 0x0e, 0x52, 0x61, 0x6e, + 0x64, 0x6f, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x05, 0x6a, + 0x6f, 0x6b, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x6a, 0x6f, 0x6b, + 0x65, 0x2e, 0x4a, 0x6f, 0x6b, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x05, 0x6a, 0x6f, 0x6b, 0x65, + 0x73, 0x32, 0x3d, 0x0a, 0x04, 0x4a, 0x6f, 0x6b, 0x65, 0x12, 0x35, 0x0a, 0x06, 0x52, 0x61, 0x6e, + 0x64, 0x6f, 0x6d, 0x12, 0x13, 0x2e, 0x6a, 0x6f, 0x6b, 0x65, 0x2e, 0x52, 0x61, 0x6e, 0x64, 0x6f, + 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x6a, 0x6f, 0x6b, 0x65, 0x2e, + 0x52, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, + 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x6a, 0x6f, 0x6b, 0x65, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_joke_proto_rawDescOnce sync.Once + file_proto_joke_proto_rawDescData = file_proto_joke_proto_rawDesc +) + +func file_proto_joke_proto_rawDescGZIP() []byte { + file_proto_joke_proto_rawDescOnce.Do(func() { + file_proto_joke_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_joke_proto_rawDescData) + }) + return file_proto_joke_proto_rawDescData +} + +var file_proto_joke_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_joke_proto_goTypes = []interface{}{ + (*RandomRequest)(nil), // 0: joke.RandomRequest + (*JokeInfo)(nil), // 1: joke.JokeInfo + (*RandomResponse)(nil), // 2: joke.RandomResponse +} +var file_proto_joke_proto_depIdxs = []int32{ + 1, // 0: joke.RandomResponse.jokes:type_name -> joke.JokeInfo + 0, // 1: joke.Joke.Random:input_type -> joke.RandomRequest + 2, // 2: joke.Joke.Random:output_type -> joke.RandomResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_proto_joke_proto_init() } +func file_proto_joke_proto_init() { + if File_proto_joke_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_joke_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RandomRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_joke_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*JokeInfo); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_joke_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RandomResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_joke_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_joke_proto_goTypes, + DependencyIndexes: file_proto_joke_proto_depIdxs, + MessageInfos: file_proto_joke_proto_msgTypes, + }.Build() + File_proto_joke_proto = out.File + file_proto_joke_proto_rawDesc = nil + file_proto_joke_proto_goTypes = nil + file_proto_joke_proto_depIdxs = nil +} diff --git a/joke/proto/joke.pb.micro.go b/joke/proto/joke.pb.micro.go new file mode 100644 index 0000000..4d9a0b7 --- /dev/null +++ b/joke/proto/joke.pb.micro.go @@ -0,0 +1,95 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/joke.proto + +package joke + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +import ( + context "context" + api "github.com/micro/micro/v3/service/api" + client "github.com/micro/micro/v3/service/client" + server "github.com/micro/micro/v3/service/server" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Reference imports to suppress errors if they are not otherwise used. +var _ api.Endpoint +var _ context.Context +var _ client.Option +var _ server.Option + +// Api Endpoints for Joke service + +func NewJokeEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Joke service + +type JokeService interface { + // get n random jokes + Random(ctx context.Context, in *RandomRequest, opts ...client.CallOption) (*RandomResponse, error) +} + +type jokeService struct { + c client.Client + name string +} + +func NewJokeService(name string, c client.Client) JokeService { + return &jokeService{ + c: c, + name: name, + } +} + +func (c *jokeService) Random(ctx context.Context, in *RandomRequest, opts ...client.CallOption) (*RandomResponse, error) { + req := c.c.NewRequest(c.name, "Joke.Random", in) + out := new(RandomResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Joke service + +type JokeHandler interface { + // get n random jokes + Random(context.Context, *RandomRequest, *RandomResponse) error +} + +func RegisterJokeHandler(s server.Server, hdlr JokeHandler, opts ...server.HandlerOption) error { + type joke interface { + Random(ctx context.Context, in *RandomRequest, out *RandomResponse) error + } + type Joke struct { + joke + } + h := &jokeHandler{hdlr} + return s.Handle(s.NewHandler(&Joke{h}, opts...)) +} + +type jokeHandler struct { + JokeHandler +} + +func (h *jokeHandler) Random(ctx context.Context, in *RandomRequest, out *RandomResponse) error { + return h.JokeHandler.Random(ctx, in, out) +} diff --git a/joke/proto/joke.proto b/joke/proto/joke.proto new file mode 100644 index 0000000..2e41558 --- /dev/null +++ b/joke/proto/joke.proto @@ -0,0 +1,28 @@ +syntax = "proto3"; + +package joke; + +option go_package = "./proto;joke"; + +service Joke { + // get n random jokes + rpc Random(RandomRequest) returns (RandomResponse) {} +} + +message RandomRequest { + // the count of random jokes want, maximum: 10 + int32 count = 1; +} + +message JokeInfo { + string id = 1; + string title = 2; + string body = 3; + string category = 4; + // the source of joke + string source = 5; +} + +message RandomResponse { + repeated JokeInfo jokes = 1; +} diff --git a/joke/publicapi.json b/joke/publicapi.json new file mode 100644 index 0000000..370f8df --- /dev/null +++ b/joke/publicapi.json @@ -0,0 +1,6 @@ +{ + "name": "joke", + "icon": "🃏", + "category": "for fun", + "display_name": "Jokes" +}