From 6186bf717bfcb79c49a726aa9db498ceffae88f2 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 24 Nov 2021 10:43:34 +0000 Subject: [PATCH] add news service (#282) --- go.mod | 14 +- news/.gitignore | 2 + news/Dockerfile | 3 + news/Makefile | 28 +++ news/README.md | 23 ++ news/examples.json | 32 +++ news/generate.go | 3 + news/handler/news.go | 105 +++++++++ news/main.go | 37 ++++ news/micro.mu | 1 + news/proto/news.pb.go | 416 ++++++++++++++++++++++++++++++++++++ news/proto/news.pb.micro.go | 93 ++++++++ news/proto/news.proto | 49 +++++ news/publicapi.json | 9 + 14 files changed, 808 insertions(+), 7 deletions(-) create mode 100644 news/.gitignore create mode 100644 news/Dockerfile create mode 100644 news/Makefile create mode 100644 news/README.md create mode 100644 news/examples.json create mode 100644 news/generate.go create mode 100644 news/handler/news.go create mode 100644 news/main.go create mode 100644 news/micro.mu create mode 100644 news/proto/news.pb.go create mode 100644 news/proto/news.pb.micro.go create mode 100644 news/proto/news.proto create mode 100644 news/publicapi.json diff --git a/go.mod b/go.mod index 8f8128c..b52654b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/micro/services go 1.15 require ( - github.com/Masterminds/semver/v3 v3.1.1 + github.com/Masterminds/semver/v3 v3.1.1 // indirect github.com/PuerkitoBio/goquery v1.6.1 github.com/SlyMarbo/rss v1.0.1 github.com/Teamwork/spamc v0.0.0-20200109085853-a4e0c5c3f7a0 @@ -11,12 +11,12 @@ require ( github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 // indirect github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 github.com/crufter/lexer v0.0.0-20120907053443-23fe8c7add01 - github.com/crufter/nested v0.0.0-20210903145606-dea42c476b37 + github.com/crufter/nested v0.0.0-20210903145606-dea42c476b37 // indirect github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e github.com/disintegration/imaging v1.6.2 github.com/enescakir/emoji v1.0.0 - github.com/fatih/camelcase v1.0.0 - github.com/getkin/kin-openapi v0.26.0 + github.com/fatih/camelcase v1.0.0 // indirect + github.com/getkin/kin-openapi v0.26.0 // indirect github.com/gojuno/go.osrm v0.1.1-0.20200217151037-435fc3e1d3d4 github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e github.com/golang/protobuf v1.5.2 @@ -33,10 +33,10 @@ require ( github.com/m3o/goduckgo v0.0.0-20210630141545-c760fe67b945 github.com/mattheath/base62 v0.0.0-20150408093626-b80cdc656a7a // indirect github.com/mattheath/kala v0.0.0-20171219141654-d6276794bf0e - github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163 + github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163 // indirect github.com/micro/micro/v3 v3.7.1-0.20211111170433-1ebb8328e280 github.com/miekg/dns v1.1.31 // indirect - github.com/onsi/gomega v1.10.5 + github.com/onsi/gomega v1.10.5 // indirect github.com/oschwald/geoip2-golang v1.5.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 @@ -45,7 +45,7 @@ require ( github.com/sendgrid/rest v2.6.4+incompatible // indirect github.com/sendgrid/sendgrid-go v3.10.0+incompatible github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e - github.com/stoewer/go-strcase v1.2.0 + github.com/stoewer/go-strcase v1.2.0 // indirect github.com/stretchr/testify v1.7.0 github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad // indirect github.com/teamwork/utils v0.0.0-20211103135549-f7e7a68ba696 // indirect diff --git a/news/.gitignore b/news/.gitignore new file mode 100644 index 0000000..245dc16 --- /dev/null +++ b/news/.gitignore @@ -0,0 +1,2 @@ + +news diff --git a/news/Dockerfile b/news/Dockerfile new file mode 100644 index 0000000..7cde1a4 --- /dev/null +++ b/news/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD news /news +ENTRYPOINT [ "/news" ] diff --git a/news/Makefile b/news/Makefile new file mode 100644 index 0000000..8776be3 --- /dev/null +++ b/news/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/news.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/news.proto + +.PHONY: build +build: + go build -o news *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t news:latest diff --git a/news/README.md b/news/README.md new file mode 100644 index 0000000..dfb54bf --- /dev/null +++ b/news/README.md @@ -0,0 +1,23 @@ +# News Service + +This is the News service + +Generated with + +``` +micro new news +``` + +## Usage + +Generate the proto code + +``` +make proto +``` + +Run the service + +``` +micro run . +``` \ No newline at end of file diff --git a/news/examples.json b/news/examples.json new file mode 100644 index 0000000..ad24516 --- /dev/null +++ b/news/examples.json @@ -0,0 +1,32 @@ +{ + "headlines": [ + { + "title": "Get news headlines", + "description": "Retrieve the latest headlines for the US", + "run_check": false, + "request": { + "language": "en", + "locale": "us", + "date": "2021-11-24" + }, + "response": { + "articles": [{ + "title": "Will Trump run again? Against Biden? The shadow-boxing begins", + "description": "With the Democratic Party in a bit of a tizzy over whether Joe Biden will run again, there’s at least as much uncertainty about whether Donald Trump will be on the ballot.", + "keywords": "", + "snippet": "With the Democratic Party in a bit of a tizzy over whether Joe Biden will run again, there’s at least as much uncertainty about whether Donald Trump will be o...", + "url": "https://www.foxnews.com/media/will-trump-biden-shadow-boxing-begins-kurtz-media-buzz", + "image_url": "https://static.foxnews.com/foxnews.com/content/uploads/2021/11/USA-TRUMP-SOCIALMEDIA-CHINA.jpg", + "published_at": "2021-11-24T08:58:18.000000Z", + "source": "foxnews.com", + "categories": [ + "general", + "politics" + ], + "language": "en", + "locale": "us" + }] + } + } + ] +} diff --git a/news/generate.go b/news/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/news/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/news/handler/news.go b/news/handler/news.go new file mode 100644 index 0000000..57b8ad8 --- /dev/null +++ b/news/handler/news.go @@ -0,0 +1,105 @@ +package handler + +import ( + "context" + "fmt" + "net/url" + "time" + + "github.com/micro/micro/v3/service/errors" + pb "github.com/micro/services/news/proto" + "github.com/micro/services/pkg/api" +) + +type News struct { + apiKey string +} + +type Article struct { + UUID string `json:"uuid,omitempty"` + Title string `json:"title,omitempty"` + Description string `json:"description,omitempty"` + Keywords string `json:"keywords,omitempty"` + Snippet string `json:"snippet,omitempty"` + Url string `json:"url,omitempty"` + ImageUrl string `json:"image_url,omitempty"` + PublishedAt string `json:"published_at,omitempty"` + Source string `json:"source,omitempty"` + Categories []string `json:"categories,omitempty"` + Language string `json:"language,omitempty"` + Locale string `json:"locale,omitempty"` + Similar []*Article `json:"similar,omitempty"` +} + +type Headlines struct { + Data map[string][]*Article `json:"data"` +} + +var ( + apiURL = "https://api.thenewsapi.com" +) + +func New(apiKey string) *News { + return &News{ + apiKey: apiKey, + } +} + +func toProto(v *Article) *pb.Article { + return &pb.Article{ + Id: v.UUID, + Title: v.Title, + Description: v.Description, + Keywords: v.Keywords, + Snippet: v.Snippet, + Url: v.Url, + ImageUrl: v.ImageUrl, + PublishedAt: v.PublishedAt, + Language: v.Language, + Source: v.Source, + Categories: v.Categories, + Locale: v.Locale, + } +} + +func (n *News) Headlines(ctx context.Context, req *pb.HeadlinesRequest, rsp *pb.HeadlinesResponse) error { + path := "/v1/news/headlines" + locale := "us" + language := "en" + date := time.Now().Format("2006-01-02") + + if len(req.Locale) > 0 { + locale = req.Locale + } + if len(req.Language) > 0 { + language = req.Language + } + + if len(req.Date) > 0 { + date = req.Date + } + + vals := url.Values{} + vals.Set("api_token", n.apiKey) + vals.Set("locale", locale) + vals.Set("published_on", date) + vals.Set("language", language) + + uri := fmt.Sprintf("%s%s?%s", apiURL, path, vals.Encode()) + var resp *Headlines + if err := api.Get(uri, &resp); err != nil { + return errors.InternalServerError("news.headlines", err.Error()) + } + + for _, articles := range resp.Data { + for _, v := range articles { + rsp.Articles = append(rsp.Articles, toProto(v)) + + for _, a := range v.Similar { + rsp.Articles = append(rsp.Articles, toProto(a)) + } + } + } + + return nil +} diff --git a/news/main.go b/news/main.go new file mode 100644 index 0000000..57cfbb9 --- /dev/null +++ b/news/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "github.com/micro/services/news/handler" + pb "github.com/micro/services/news/proto" + + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/logger" +) + +func main() { + // Create service + srv := service.New( + service.Name("news"), + service.Version("latest"), + ) + + // Setup google maps + c, err := config.Get("news.apikey") + if err != nil { + logger.Fatalf("Error loading config: %v", err) + } + + apiKey := c.String("") + if len(apiKey) == 0 { + logger.Fatalf("Missing required config: news.apikey") + } + + // Register handler + pb.RegisterNewsHandler(srv.Server(), handler.New(apiKey)) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/news/micro.mu b/news/micro.mu new file mode 100644 index 0000000..25957a4 --- /dev/null +++ b/news/micro.mu @@ -0,0 +1 @@ +service news diff --git a/news/proto/news.pb.go b/news/proto/news.pb.go new file mode 100644 index 0000000..468422f --- /dev/null +++ b/news/proto/news.pb.go @@ -0,0 +1,416 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.15.6 +// source: proto/news.proto + +package news + +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 Article struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // article id + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + // article title + Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"` + // article description + Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"` + // related keywords + Keywords string `protobuf:"bytes,4,opt,name=keywords,proto3" json:"keywords,omitempty"` + // first 60 characters of article body + Snippet string `protobuf:"bytes,5,opt,name=snippet,proto3" json:"snippet,omitempty"` + // url of the article + Url string `protobuf:"bytes,6,opt,name=url,proto3" json:"url,omitempty"` + // image url + ImageUrl string `protobuf:"bytes,7,opt,name=image_url,json=imageUrl,proto3" json:"image_url,omitempty"` + // time it was published + PublishedAt string `protobuf:"bytes,8,opt,name=published_at,json=publishedAt,proto3" json:"published_at,omitempty"` + // source of news + Source string `protobuf:"bytes,9,opt,name=source,proto3" json:"source,omitempty"` + // categories + Categories []string `protobuf:"bytes,10,rep,name=categories,proto3" json:"categories,omitempty"` + // the article language + Language string `protobuf:"bytes,11,opt,name=language,proto3" json:"language,omitempty"` + // the locale + Locale string `protobuf:"bytes,12,opt,name=locale,proto3" json:"locale,omitempty"` +} + +func (x *Article) Reset() { + *x = Article{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_news_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Article) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Article) ProtoMessage() {} + +func (x *Article) ProtoReflect() protoreflect.Message { + mi := &file_proto_news_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 Article.ProtoReflect.Descriptor instead. +func (*Article) Descriptor() ([]byte, []int) { + return file_proto_news_proto_rawDescGZIP(), []int{0} +} + +func (x *Article) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +func (x *Article) GetTitle() string { + if x != nil { + return x.Title + } + return "" +} + +func (x *Article) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + +func (x *Article) GetKeywords() string { + if x != nil { + return x.Keywords + } + return "" +} + +func (x *Article) GetSnippet() string { + if x != nil { + return x.Snippet + } + return "" +} + +func (x *Article) GetUrl() string { + if x != nil { + return x.Url + } + return "" +} + +func (x *Article) GetImageUrl() string { + if x != nil { + return x.ImageUrl + } + return "" +} + +func (x *Article) GetPublishedAt() string { + if x != nil { + return x.PublishedAt + } + return "" +} + +func (x *Article) GetSource() string { + if x != nil { + return x.Source + } + return "" +} + +func (x *Article) GetCategories() []string { + if x != nil { + return x.Categories + } + return nil +} + +func (x *Article) GetLanguage() string { + if x != nil { + return x.Language + } + return "" +} + +func (x *Article) GetLocale() string { + if x != nil { + return x.Locale + } + return "" +} + +type HeadlinesRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // comma separated list of languages to retrieve in e.g en,es + Language string `protobuf:"bytes,1,opt,name=language,proto3" json:"language,omitempty"` + // comma separated list of countries to include e.g us,ca + Locale string `protobuf:"bytes,2,opt,name=locale,proto3" json:"locale,omitempty"` + // date published on in YYYY-MM-DD format + Date string `protobuf:"bytes,3,opt,name=date,proto3" json:"date,omitempty"` +} + +func (x *HeadlinesRequest) Reset() { + *x = HeadlinesRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_news_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HeadlinesRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HeadlinesRequest) ProtoMessage() {} + +func (x *HeadlinesRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_news_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 HeadlinesRequest.ProtoReflect.Descriptor instead. +func (*HeadlinesRequest) Descriptor() ([]byte, []int) { + return file_proto_news_proto_rawDescGZIP(), []int{1} +} + +func (x *HeadlinesRequest) GetLanguage() string { + if x != nil { + return x.Language + } + return "" +} + +func (x *HeadlinesRequest) GetLocale() string { + if x != nil { + return x.Locale + } + return "" +} + +func (x *HeadlinesRequest) GetDate() string { + if x != nil { + return x.Date + } + return "" +} + +type HeadlinesResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Articles []*Article `protobuf:"bytes,1,rep,name=articles,proto3" json:"articles,omitempty"` +} + +func (x *HeadlinesResponse) Reset() { + *x = HeadlinesResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_news_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HeadlinesResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HeadlinesResponse) ProtoMessage() {} + +func (x *HeadlinesResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_news_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 HeadlinesResponse.ProtoReflect.Descriptor instead. +func (*HeadlinesResponse) Descriptor() ([]byte, []int) { + return file_proto_news_proto_rawDescGZIP(), []int{2} +} + +func (x *HeadlinesResponse) GetArticles() []*Article { + if x != nil { + return x.Articles + } + return nil +} + +var File_proto_news_proto protoreflect.FileDescriptor + +var file_proto_news_proto_rawDesc = []byte{ + 0x0a, 0x10, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6e, 0x65, 0x77, 0x73, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x04, 0x6e, 0x65, 0x77, 0x73, 0x22, 0xc5, 0x02, 0x0a, 0x07, 0x41, 0x72, 0x74, + 0x69, 0x63, 0x6c, 0x65, 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, 0x20, 0x0a, 0x0b, 0x64, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1a, 0x0a, 0x08, + 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, + 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x6e, 0x69, 0x70, + 0x70, 0x65, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x6e, 0x69, 0x70, 0x70, + 0x65, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x03, 0x75, 0x72, 0x6c, 0x12, 0x1b, 0x0a, 0x09, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x5f, 0x75, 0x72, + 0x6c, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x55, 0x72, + 0x6c, 0x12, 0x21, 0x0a, 0x0c, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64, 0x5f, 0x61, + 0x74, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, + 0x65, 0x64, 0x41, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x09, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x1e, 0x0a, 0x0a, + 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x69, 0x65, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x09, + 0x52, 0x0a, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x69, 0x65, 0x73, 0x12, 0x1a, 0x0a, 0x08, + 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, + 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x6f, 0x63, 0x61, + 0x6c, 0x65, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x65, + 0x22, 0x5a, 0x0a, 0x10, 0x48, 0x65, 0x61, 0x64, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, + 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x06, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x65, 0x22, 0x3e, 0x0a, 0x11, + 0x48, 0x65, 0x61, 0x64, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x29, 0x0a, 0x08, 0x61, 0x72, 0x74, 0x69, 0x63, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x6e, 0x65, 0x77, 0x73, 0x2e, 0x41, 0x72, 0x74, 0x69, 0x63, + 0x6c, 0x65, 0x52, 0x08, 0x61, 0x72, 0x74, 0x69, 0x63, 0x6c, 0x65, 0x73, 0x32, 0x46, 0x0a, 0x04, + 0x4e, 0x65, 0x77, 0x73, 0x12, 0x3e, 0x0a, 0x09, 0x48, 0x65, 0x61, 0x64, 0x6c, 0x69, 0x6e, 0x65, + 0x73, 0x12, 0x16, 0x2e, 0x6e, 0x65, 0x77, 0x73, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x6c, 0x69, 0x6e, + 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6e, 0x65, 0x77, 0x73, + 0x2e, 0x48, 0x65, 0x61, 0x64, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, + 0x6e, 0x65, 0x77, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_news_proto_rawDescOnce sync.Once + file_proto_news_proto_rawDescData = file_proto_news_proto_rawDesc +) + +func file_proto_news_proto_rawDescGZIP() []byte { + file_proto_news_proto_rawDescOnce.Do(func() { + file_proto_news_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_news_proto_rawDescData) + }) + return file_proto_news_proto_rawDescData +} + +var file_proto_news_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_news_proto_goTypes = []interface{}{ + (*Article)(nil), // 0: news.Article + (*HeadlinesRequest)(nil), // 1: news.HeadlinesRequest + (*HeadlinesResponse)(nil), // 2: news.HeadlinesResponse +} +var file_proto_news_proto_depIdxs = []int32{ + 0, // 0: news.HeadlinesResponse.articles:type_name -> news.Article + 1, // 1: news.News.Headlines:input_type -> news.HeadlinesRequest + 2, // 2: news.News.Headlines:output_type -> news.HeadlinesResponse + 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_news_proto_init() } +func file_proto_news_proto_init() { + if File_proto_news_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_news_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Article); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_news_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HeadlinesRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_news_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HeadlinesResponse); 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_news_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_news_proto_goTypes, + DependencyIndexes: file_proto_news_proto_depIdxs, + MessageInfos: file_proto_news_proto_msgTypes, + }.Build() + File_proto_news_proto = out.File + file_proto_news_proto_rawDesc = nil + file_proto_news_proto_goTypes = nil + file_proto_news_proto_depIdxs = nil +} diff --git a/news/proto/news.pb.micro.go b/news/proto/news.pb.micro.go new file mode 100644 index 0000000..1da9326 --- /dev/null +++ b/news/proto/news.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/news.proto + +package news + +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 News service + +func NewNewsEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for News service + +type NewsService interface { + Headlines(ctx context.Context, in *HeadlinesRequest, opts ...client.CallOption) (*HeadlinesResponse, error) +} + +type newsService struct { + c client.Client + name string +} + +func NewNewsService(name string, c client.Client) NewsService { + return &newsService{ + c: c, + name: name, + } +} + +func (c *newsService) Headlines(ctx context.Context, in *HeadlinesRequest, opts ...client.CallOption) (*HeadlinesResponse, error) { + req := c.c.NewRequest(c.name, "News.Headlines", in) + out := new(HeadlinesResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for News service + +type NewsHandler interface { + Headlines(context.Context, *HeadlinesRequest, *HeadlinesResponse) error +} + +func RegisterNewsHandler(s server.Server, hdlr NewsHandler, opts ...server.HandlerOption) error { + type news interface { + Headlines(ctx context.Context, in *HeadlinesRequest, out *HeadlinesResponse) error + } + type News struct { + news + } + h := &newsHandler{hdlr} + return s.Handle(s.NewHandler(&News{h}, opts...)) +} + +type newsHandler struct { + NewsHandler +} + +func (h *newsHandler) Headlines(ctx context.Context, in *HeadlinesRequest, out *HeadlinesResponse) error { + return h.NewsHandler.Headlines(ctx, in, out) +} diff --git a/news/proto/news.proto b/news/proto/news.proto new file mode 100644 index 0000000..9a3d6a5 --- /dev/null +++ b/news/proto/news.proto @@ -0,0 +1,49 @@ +syntax = "proto3"; + +package news; + +option go_package = "./proto;news"; + +service News { + rpc Headlines(HeadlinesRequest) returns (HeadlinesResponse) {} +} + +message Article { + // article id + string id = 1; + // article title + string title = 2; + // article description + string description = 3; + // related keywords + string keywords = 4; + // first 60 characters of article body + string snippet = 5; + // url of the article + string url = 6; + // image url + string image_url = 7; + // time it was published + string published_at = 8; + // source of news + string source = 9; + // categories + repeated string categories = 10; + // the article language + string language = 11; + // the locale + string locale = 12; +} + +message HeadlinesRequest { + // comma separated list of languages to retrieve in e.g en,es + string language = 1; + // comma separated list of countries to include e.g us,ca + string locale = 2; + // date published on in YYYY-MM-DD format + string date = 3; +} + +message HeadlinesResponse { + repeated Article articles = 1; +} diff --git a/news/publicapi.json b/news/publicapi.json new file mode 100644 index 0000000..2eeb79f --- /dev/null +++ b/news/publicapi.json @@ -0,0 +1,9 @@ +{ + "name": "news", + "icon": "📰", + "category": "web", + "display_name": "News", + "pricing": { + "News.Headlines": 10000 + } +}