diff --git a/go.mod b/go.mod index e773abd..8bfb475 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/pquerna/otp v1.3.0 github.com/stoewer/go-strcase v1.2.0 github.com/stretchr/testify v1.7.0 + github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf github.com/ulikunitz/xz v0.5.8 // indirect golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 golang.org/x/net v0.0.0-20201021035429-f5854403a974 diff --git a/url-shortener/.gitignore b/url-shortener/.gitignore new file mode 100644 index 0000000..9e7ed84 --- /dev/null +++ b/url-shortener/.gitignore @@ -0,0 +1,2 @@ + +url-shortener diff --git a/url-shortener/Dockerfile b/url-shortener/Dockerfile new file mode 100644 index 0000000..084c0c8 --- /dev/null +++ b/url-shortener/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD url-shortener /url-shortener +ENTRYPOINT [ "/url-shortener" ] diff --git a/url-shortener/Makefile b/url-shortener/Makefile new file mode 100644 index 0000000..5f2ab58 --- /dev/null +++ b/url-shortener/Makefile @@ -0,0 +1,22 @@ + +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 +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/url-shortener.proto + +.PHONY: build +build: + go build -o url-shortener *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t url-shortener:latest diff --git a/url-shortener/README.md b/url-shortener/README.md new file mode 100644 index 0000000..e32b308 --- /dev/null +++ b/url-shortener/README.md @@ -0,0 +1,23 @@ +# UrlShortener Service + +This is the UrlShortener service + +Generated with + +``` +micro new url-shortener +``` + +## Usage + +Generate the proto code + +``` +make proto +``` + +Run the service + +``` +micro run . +``` \ No newline at end of file diff --git a/url-shortener/generate.go b/url-shortener/generate.go new file mode 100644 index 0000000..96f431a --- /dev/null +++ b/url-shortener/generate.go @@ -0,0 +1,2 @@ +package main +//go:generate make proto diff --git a/url-shortener/handler/url-shortener.go b/url-shortener/handler/url-shortener.go new file mode 100644 index 0000000..31ccf89 --- /dev/null +++ b/url-shortener/handler/url-shortener.go @@ -0,0 +1,101 @@ +package handler + +import ( + "context" + "errors" + "strings" + + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/model" + "github.com/teris-io/shortid" + + "github.com/micro/services/pkg/tenant" + urlshortener "github.com/micro/services/url-shortener/proto" +) + +const hostPrefix = "https://cdn.m3ocontent.com" + +type UrlShortener struct { + pairs model.Model + ownerIndex model.Index + hostPrefix string +} + +func NewUrlShortener() *UrlShortener { + var hp string + cfg, err := config.Get("micro.url_shortener.host_prefix") + if err != nil { + hp = cfg.String(hostPrefix) + } + if len(strings.TrimSpace(hp)) == 0 { + hp = hostPrefix + } + + ownerIndex := model.ByEquality("Owner") + ownerIndex.Order.Type = model.OrderTypeUnordered + + return &UrlShortener{ + pairs: model.NewModel( + model.WithKey("ShortURL"), + model.WithIndexes(ownerIndex), + ), + ownerIndex: ownerIndex, + } +} + +func (e *UrlShortener) Shorten(ctx context.Context, req *urlshortener.ShortenRequest, rsp *urlshortener.ShortenResponse) error { + tenantID, ok := tenant.FromContext(ctx) + if !ok { + return errors.New("Not authorized") + } + sid, err := shortid.New(1, shortid.DefaultABC, 2342) + if err != nil { + return err + } + + id, err := sid.Generate() + if err != nil { + return err + } + return e.pairs.Create(&urlshortener.URLPair{ + DestinationURL: req.DestinationURL, + ShortURL: id, + Owner: tenantID, + }) +} + +func (e *UrlShortener) List(ctx context.Context, req *urlshortener.ListRequest, rsp *urlshortener.ListResponse) error { + tenantID, ok := tenant.FromContext(ctx) + if !ok { + return errors.New("Not authorized") + } + + rsp.UrlPairs = []*urlshortener.URLPair{} + err := e.pairs.Read(e.ownerIndex.ToQuery(e.ownerIndex.ToQuery(tenantID)), &rsp.UrlPairs) + if err != nil { + return err + } + for _, v := range rsp.UrlPairs { + v.ShortURL = e.hostPrefix + "/" + v.ShortURL + } + return nil +} + +func (e *UrlShortener) Get(ctx context.Context, req *urlshortener.GetRequest, rsp *urlshortener.GetResponse) error { + tenantID, ok := tenant.FromContext(ctx) + if !ok { + return errors.New("Not authorized") + } + + var pair urlshortener.URLPair + err := e.pairs.Read(e.ownerIndex.ToQuery(model.QueryEquals("ShortURL", e.hostPrefix+"/"+req.ShortURL)), pair) + if err != nil { + return err + } + if pair.Owner != tenantID { + return errors.New("not authorized") + } + + rsp.DestinationURL = pair.DestinationURL + return nil +} diff --git a/url-shortener/main.go b/url-shortener/main.go new file mode 100644 index 0000000..79eda37 --- /dev/null +++ b/url-shortener/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/micro/services/url-shortener/handler" + pb "github.com/micro/services/url-shortener/proto" + + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" +) + +func main() { + // Create service + srv := service.New( + service.Name("url-shortener"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterUrlShortenerHandler(srv.Server(), handler.NewUrlShortener()) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/url-shortener/micro.mu b/url-shortener/micro.mu new file mode 100644 index 0000000..0078f49 --- /dev/null +++ b/url-shortener/micro.mu @@ -0,0 +1 @@ +service url-shortener diff --git a/url-shortener/proto/url-shortener.pb.go b/url-shortener/proto/url-shortener.pb.go new file mode 100644 index 0000000..0aa60a7 --- /dev/null +++ b/url-shortener/proto/url-shortener.pb.go @@ -0,0 +1,555 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.6.1 +// source: proto/url-shortener.proto + +package urlshortener + +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 ShortenRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + DestinationURL string `protobuf:"bytes,1,opt,name=destinationURL,proto3" json:"destinationURL,omitempty"` +} + +func (x *ShortenRequest) Reset() { + *x = ShortenRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ShortenRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ShortenRequest) ProtoMessage() {} + +func (x *ShortenRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_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 ShortenRequest.ProtoReflect.Descriptor instead. +func (*ShortenRequest) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{0} +} + +func (x *ShortenRequest) GetDestinationURL() string { + if x != nil { + return x.DestinationURL + } + return "" +} + +type ShortenResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ShortURL string `protobuf:"bytes,1,opt,name=shortURL,proto3" json:"shortURL,omitempty"` +} + +func (x *ShortenResponse) Reset() { + *x = ShortenResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ShortenResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ShortenResponse) ProtoMessage() {} + +func (x *ShortenResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_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 ShortenResponse.ProtoReflect.Descriptor instead. +func (*ShortenResponse) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{1} +} + +func (x *ShortenResponse) GetShortURL() string { + if x != nil { + return x.ShortURL + } + return "" +} + +type URLPair struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + DestinationURL string `protobuf:"bytes,1,opt,name=destinationURL,proto3" json:"destinationURL,omitempty"` + ShortURL string `protobuf:"bytes,2,opt,name=shortURL,proto3" json:"shortURL,omitempty"` + Owner string `protobuf:"bytes,3,opt,name=owner,proto3" json:"owner,omitempty"` +} + +func (x *URLPair) Reset() { + *x = URLPair{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *URLPair) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*URLPair) ProtoMessage() {} + +func (x *URLPair) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_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 URLPair.ProtoReflect.Descriptor instead. +func (*URLPair) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{2} +} + +func (x *URLPair) GetDestinationURL() string { + if x != nil { + return x.DestinationURL + } + return "" +} + +func (x *URLPair) GetShortURL() string { + if x != nil { + return x.ShortURL + } + return "" +} + +func (x *URLPair) GetOwner() string { + if x != nil { + return x.Owner + } + return "" +} + +type ListRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *ListRequest) Reset() { + *x = ListRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListRequest) ProtoMessage() {} + +func (x *ListRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_proto_msgTypes[3] + 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 ListRequest.ProtoReflect.Descriptor instead. +func (*ListRequest) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{3} +} + +type ListResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UrlPairs []*URLPair `protobuf:"bytes,1,rep,name=urlPairs,proto3" json:"urlPairs,omitempty"` +} + +func (x *ListResponse) Reset() { + *x = ListResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListResponse) ProtoMessage() {} + +func (x *ListResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_proto_msgTypes[4] + 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 ListResponse.ProtoReflect.Descriptor instead. +func (*ListResponse) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{4} +} + +func (x *ListResponse) GetUrlPairs() []*URLPair { + if x != nil { + return x.UrlPairs + } + return nil +} + +type GetRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ShortURL string `protobuf:"bytes,1,opt,name=shortURL,proto3" json:"shortURL,omitempty"` +} + +func (x *GetRequest) Reset() { + *x = GetRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetRequest) ProtoMessage() {} + +func (x *GetRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_proto_msgTypes[5] + 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 GetRequest.ProtoReflect.Descriptor instead. +func (*GetRequest) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{5} +} + +func (x *GetRequest) GetShortURL() string { + if x != nil { + return x.ShortURL + } + return "" +} + +type GetResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + DestinationURL string `protobuf:"bytes,1,opt,name=destinationURL,proto3" json:"destinationURL,omitempty"` +} + +func (x *GetResponse) Reset() { + *x = GetResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_url_shortener_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetResponse) ProtoMessage() {} + +func (x *GetResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_url_shortener_proto_msgTypes[6] + 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 GetResponse.ProtoReflect.Descriptor instead. +func (*GetResponse) Descriptor() ([]byte, []int) { + return file_proto_url_shortener_proto_rawDescGZIP(), []int{6} +} + +func (x *GetResponse) GetDestinationURL() string { + if x != nil { + return x.DestinationURL + } + return "" +} + +var File_proto_url_shortener_proto protoreflect.FileDescriptor + +var file_proto_url_shortener_proto_rawDesc = []byte{ + 0x0a, 0x19, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x75, 0x72, 0x6c, 0x2d, 0x73, 0x68, 0x6f, 0x72, + 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0c, 0x75, 0x72, 0x6c, + 0x73, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x22, 0x38, 0x0a, 0x0e, 0x53, 0x68, 0x6f, + 0x72, 0x74, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x26, 0x0a, 0x0e, 0x64, + 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x55, 0x52, 0x4c, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x0e, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x55, 0x52, 0x4c, 0x22, 0x2d, 0x0a, 0x0f, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x55, + 0x52, 0x4c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x55, + 0x52, 0x4c, 0x22, 0x63, 0x0a, 0x07, 0x55, 0x52, 0x4c, 0x50, 0x61, 0x69, 0x72, 0x12, 0x26, 0x0a, + 0x0e, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x55, 0x52, 0x4c, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x55, 0x52, 0x4c, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x55, 0x52, + 0x4c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x55, 0x52, + 0x4c, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x22, 0x0d, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x41, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x75, 0x72, 0x6c, 0x50, 0x61, 0x69, + 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x75, 0x72, 0x6c, 0x73, 0x68, + 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x55, 0x52, 0x4c, 0x50, 0x61, 0x69, 0x72, 0x52, + 0x08, 0x75, 0x72, 0x6c, 0x50, 0x61, 0x69, 0x72, 0x73, 0x22, 0x28, 0x0a, 0x0a, 0x47, 0x65, 0x74, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, + 0x55, 0x52, 0x4c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x73, 0x68, 0x6f, 0x72, 0x74, + 0x55, 0x52, 0x4c, 0x22, 0x35, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x26, 0x0a, 0x0e, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x55, 0x52, 0x4c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x64, 0x65, 0x73, 0x74, + 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x55, 0x52, 0x4c, 0x32, 0xd7, 0x01, 0x0a, 0x0c, 0x55, + 0x72, 0x6c, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x12, 0x48, 0x0a, 0x07, 0x53, + 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x12, 0x1c, 0x2e, 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, 0x72, + 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x65, + 0x6e, 0x65, 0x72, 0x2e, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3f, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x19, 0x2e, + 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x4c, 0x69, 0x73, + 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x75, 0x72, 0x6c, 0x73, 0x68, + 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3c, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x18, 0x2e, + 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x74, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, + 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x42, 0x3c, 0x5a, 0x3a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x73, 0x2f, 0x75, 0x72, 0x6c, 0x2d, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, + 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_url_shortener_proto_rawDescOnce sync.Once + file_proto_url_shortener_proto_rawDescData = file_proto_url_shortener_proto_rawDesc +) + +func file_proto_url_shortener_proto_rawDescGZIP() []byte { + file_proto_url_shortener_proto_rawDescOnce.Do(func() { + file_proto_url_shortener_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_url_shortener_proto_rawDescData) + }) + return file_proto_url_shortener_proto_rawDescData +} + +var file_proto_url_shortener_proto_msgTypes = make([]protoimpl.MessageInfo, 7) +var file_proto_url_shortener_proto_goTypes = []interface{}{ + (*ShortenRequest)(nil), // 0: urlshortener.ShortenRequest + (*ShortenResponse)(nil), // 1: urlshortener.ShortenResponse + (*URLPair)(nil), // 2: urlshortener.URLPair + (*ListRequest)(nil), // 3: urlshortener.ListRequest + (*ListResponse)(nil), // 4: urlshortener.ListResponse + (*GetRequest)(nil), // 5: urlshortener.GetRequest + (*GetResponse)(nil), // 6: urlshortener.GetResponse +} +var file_proto_url_shortener_proto_depIdxs = []int32{ + 2, // 0: urlshortener.ListResponse.urlPairs:type_name -> urlshortener.URLPair + 0, // 1: urlshortener.UrlShortener.Shorten:input_type -> urlshortener.ShortenRequest + 3, // 2: urlshortener.UrlShortener.List:input_type -> urlshortener.ListRequest + 5, // 3: urlshortener.UrlShortener.Get:input_type -> urlshortener.GetRequest + 1, // 4: urlshortener.UrlShortener.Shorten:output_type -> urlshortener.ShortenResponse + 4, // 5: urlshortener.UrlShortener.List:output_type -> urlshortener.ListResponse + 6, // 6: urlshortener.UrlShortener.Get:output_type -> urlshortener.GetResponse + 4, // [4:7] is the sub-list for method output_type + 1, // [1:4] 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_url_shortener_proto_init() } +func file_proto_url_shortener_proto_init() { + if File_proto_url_shortener_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_url_shortener_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ShortenRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_url_shortener_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ShortenResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_url_shortener_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*URLPair); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_url_shortener_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_url_shortener_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_url_shortener_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_url_shortener_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetResponse); 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_url_shortener_proto_rawDesc, + NumEnums: 0, + NumMessages: 7, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_url_shortener_proto_goTypes, + DependencyIndexes: file_proto_url_shortener_proto_depIdxs, + MessageInfos: file_proto_url_shortener_proto_msgTypes, + }.Build() + File_proto_url_shortener_proto = out.File + file_proto_url_shortener_proto_rawDesc = nil + file_proto_url_shortener_proto_goTypes = nil + file_proto_url_shortener_proto_depIdxs = nil +} diff --git a/url-shortener/proto/url-shortener.pb.micro.go b/url-shortener/proto/url-shortener.pb.micro.go new file mode 100644 index 0000000..cc12eae --- /dev/null +++ b/url-shortener/proto/url-shortener.pb.micro.go @@ -0,0 +1,127 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/url-shortener.proto + +package urlshortener + +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 UrlShortener service + +func NewUrlShortenerEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for UrlShortener service + +type UrlShortenerService interface { + Shorten(ctx context.Context, in *ShortenRequest, opts ...client.CallOption) (*ShortenResponse, error) + List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error) + Get(ctx context.Context, in *GetRequest, opts ...client.CallOption) (*GetResponse, error) +} + +type urlShortenerService struct { + c client.Client + name string +} + +func NewUrlShortenerService(name string, c client.Client) UrlShortenerService { + return &urlShortenerService{ + c: c, + name: name, + } +} + +func (c *urlShortenerService) Shorten(ctx context.Context, in *ShortenRequest, opts ...client.CallOption) (*ShortenResponse, error) { + req := c.c.NewRequest(c.name, "UrlShortener.Shorten", in) + out := new(ShortenResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *urlShortenerService) List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error) { + req := c.c.NewRequest(c.name, "UrlShortener.List", in) + out := new(ListResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *urlShortenerService) Get(ctx context.Context, in *GetRequest, opts ...client.CallOption) (*GetResponse, error) { + req := c.c.NewRequest(c.name, "UrlShortener.Get", in) + out := new(GetResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for UrlShortener service + +type UrlShortenerHandler interface { + Shorten(context.Context, *ShortenRequest, *ShortenResponse) error + List(context.Context, *ListRequest, *ListResponse) error + Get(context.Context, *GetRequest, *GetResponse) error +} + +func RegisterUrlShortenerHandler(s server.Server, hdlr UrlShortenerHandler, opts ...server.HandlerOption) error { + type urlShortener interface { + Shorten(ctx context.Context, in *ShortenRequest, out *ShortenResponse) error + List(ctx context.Context, in *ListRequest, out *ListResponse) error + Get(ctx context.Context, in *GetRequest, out *GetResponse) error + } + type UrlShortener struct { + urlShortener + } + h := &urlShortenerHandler{hdlr} + return s.Handle(s.NewHandler(&UrlShortener{h}, opts...)) +} + +type urlShortenerHandler struct { + UrlShortenerHandler +} + +func (h *urlShortenerHandler) Shorten(ctx context.Context, in *ShortenRequest, out *ShortenResponse) error { + return h.UrlShortenerHandler.Shorten(ctx, in, out) +} + +func (h *urlShortenerHandler) List(ctx context.Context, in *ListRequest, out *ListResponse) error { + return h.UrlShortenerHandler.List(ctx, in, out) +} + +func (h *urlShortenerHandler) Get(ctx context.Context, in *GetRequest, out *GetResponse) error { + return h.UrlShortenerHandler.Get(ctx, in, out) +} diff --git a/url-shortener/proto/url-shortener.proto b/url-shortener/proto/url-shortener.proto new file mode 100644 index 0000000..1215e6d --- /dev/null +++ b/url-shortener/proto/url-shortener.proto @@ -0,0 +1,40 @@ +syntax = "proto3"; + +package urlshortener; + +option go_package = "github.com/micro/services/url-shortener/proto;urlshortener"; + +service UrlShortener { + rpc Shorten(ShortenRequest) returns (ShortenResponse) {} + rpc List(ListRequest) returns (ListResponse) {} + rpc Get(GetRequest) returns (GetResponse) {} +} + +message ShortenRequest { + string destinationURL = 1; +} + +message ShortenResponse { + string shortURL = 1; +} + +message URLPair { + string destinationURL = 1; + string shortURL = 2; + string owner = 3; +} + +message ListRequest { +} + +message ListResponse { + repeated URLPair urlPairs = 1; +} + +message GetRequest { + string shortURL = 1; +} + +message GetResponse { + string destinationURL = 1; +}