URL shortening service (#104)

* URL shortening service

* Url shortener -> url

* Readme edit
This commit is contained in:
Janos Dobronszki
2021-05-17 14:29:01 +01:00
committed by GitHub
parent 5232140591
commit a2dc68b385
12 changed files with 872 additions and 0 deletions

1
go.mod
View File

@@ -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

2
url/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
url-shortener

3
url/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD url-shortener /url-shortener
ENTRYPOINT [ "/url-shortener" ]

22
url/Makefile Normal file
View File

@@ -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.proto
.PHONY: build
build:
go build -o url *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t url:latest

1
url/README.md Normal file
View File

@@ -0,0 +1 @@
URL shortening, sharing and analytics

2
url/generate.go Normal file
View File

@@ -0,0 +1,2 @@
package main
//go:generate make proto

93
url/handler/url.go Normal file
View File

@@ -0,0 +1,93 @@
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"
url "github.com/micro/services/url/proto"
)
const hostPrefix = "https://m3o.one/u"
type Url struct {
pairs model.Model
ownerIndex model.Index
hostPrefix string
}
func NewUrl() *Url {
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 &Url{
pairs: model.NewModel(
model.WithKey("ShortURL"),
model.WithIndexes(ownerIndex),
),
ownerIndex: ownerIndex,
}
}
func (e *Url) Shorten(ctx context.Context, req *url.ShortenRequest, rsp *url.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(&url.URLPair{
DestinationURL: req.DestinationURL,
ShortURL: id,
Owner: tenantID,
})
}
func (e *Url) List(ctx context.Context, req *url.ListRequest, rsp *url.ListResponse) error {
tenantID, ok := tenant.FromContext(ctx)
if !ok {
return errors.New("Not authorized")
}
rsp.UrlPairs = []*url.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 *Url) Proxy(ctx context.Context, req *url.ProxyRequest, rsp *url.ProxyResponse) error {
var pair url.URLPair
err := e.pairs.Read(e.ownerIndex.ToQuery(model.QueryEquals("ShortURL", e.hostPrefix+"/"+req.ShortURL)), pair)
if err != nil {
return err
}
rsp.DestinationURL = pair.DestinationURL
return nil
}

25
url/main.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import (
"github.com/micro/services/url/handler"
pb "github.com/micro/services/url/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.NewUrl())
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
url/micro.mu Normal file
View File

@@ -0,0 +1 @@
service url-shortener

555
url/proto/url.pb.go Normal file
View File

@@ -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.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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_proto_rawDescGZIP(), []int{4}
}
func (x *ListResponse) GetUrlPairs() []*URLPair {
if x != nil {
return x.UrlPairs
}
return nil
}
type ProxyRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ShortURL string `protobuf:"bytes,1,opt,name=shortURL,proto3" json:"shortURL,omitempty"`
}
func (x *ProxyRequest) Reset() {
*x = ProxyRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_url_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ProxyRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ProxyRequest) ProtoMessage() {}
func (x *ProxyRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_url_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 ProxyRequest.ProtoReflect.Descriptor instead.
func (*ProxyRequest) Descriptor() ([]byte, []int) {
return file_proto_url_proto_rawDescGZIP(), []int{5}
}
func (x *ProxyRequest) GetShortURL() string {
if x != nil {
return x.ShortURL
}
return ""
}
type ProxyResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
DestinationURL string `protobuf:"bytes,1,opt,name=destinationURL,proto3" json:"destinationURL,omitempty"`
}
func (x *ProxyResponse) Reset() {
*x = ProxyResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_url_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ProxyResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ProxyResponse) ProtoMessage() {}
func (x *ProxyResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_url_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 ProxyResponse.ProtoReflect.Descriptor instead.
func (*ProxyResponse) Descriptor() ([]byte, []int) {
return file_proto_url_proto_rawDescGZIP(), []int{6}
}
func (x *ProxyResponse) GetDestinationURL() string {
if x != nil {
return x.DestinationURL
}
return ""
}
var File_proto_url_proto protoreflect.FileDescriptor
var file_proto_url_proto_rawDesc = []byte{
0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x75, 0x72, 0x6c, 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,
0x2a, 0x0a, 0x0c, 0x50, 0x72, 0x6f, 0x78, 0x79, 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, 0x37, 0x0a, 0x0d, 0x50,
0x72, 0x6f, 0x78, 0x79, 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, 0xdd, 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, 0x42, 0x0a, 0x05, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x12, 0x1a, 0x2e, 0x75, 0x72, 0x6c, 0x73,
0x68, 0x6f, 0x72, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x75, 0x72, 0x6c, 0x73, 0x68, 0x6f, 0x72, 0x74,
0x65, 0x6e, 0x65, 0x72, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 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_proto_rawDescOnce sync.Once
file_proto_url_proto_rawDescData = file_proto_url_proto_rawDesc
)
func file_proto_url_proto_rawDescGZIP() []byte {
file_proto_url_proto_rawDescOnce.Do(func() {
file_proto_url_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_url_proto_rawDescData)
})
return file_proto_url_proto_rawDescData
}
var file_proto_url_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_proto_url_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
(*ProxyRequest)(nil), // 5: urlshortener.ProxyRequest
(*ProxyResponse)(nil), // 6: urlshortener.ProxyResponse
}
var file_proto_url_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.Proxy:input_type -> urlshortener.ProxyRequest
1, // 4: urlshortener.UrlShortener.Shorten:output_type -> urlshortener.ShortenResponse
4, // 5: urlshortener.UrlShortener.List:output_type -> urlshortener.ListResponse
6, // 6: urlshortener.UrlShortener.Proxy:output_type -> urlshortener.ProxyResponse
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_proto_init() }
func file_proto_url_proto_init() {
if File_proto_url_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_url_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_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_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_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_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_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ProxyRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_url_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ProxyResponse); 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_proto_rawDesc,
NumEnums: 0,
NumMessages: 7,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_url_proto_goTypes,
DependencyIndexes: file_proto_url_proto_depIdxs,
MessageInfos: file_proto_url_proto_msgTypes,
}.Build()
File_proto_url_proto = out.File
file_proto_url_proto_rawDesc = nil
file_proto_url_proto_goTypes = nil
file_proto_url_proto_depIdxs = nil
}

127
url/proto/url.pb.micro.go Normal file
View File

@@ -0,0 +1,127 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/url.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)
Proxy(ctx context.Context, in *ProxyRequest, opts ...client.CallOption) (*ProxyResponse, 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) Proxy(ctx context.Context, in *ProxyRequest, opts ...client.CallOption) (*ProxyResponse, error) {
req := c.c.NewRequest(c.name, "UrlShortener.Proxy", in)
out := new(ProxyResponse)
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
Proxy(context.Context, *ProxyRequest, *ProxyResponse) 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
Proxy(ctx context.Context, in *ProxyRequest, out *ProxyResponse) 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) Proxy(ctx context.Context, in *ProxyRequest, out *ProxyResponse) error {
return h.UrlShortenerHandler.Proxy(ctx, in, out)
}

40
url/proto/url.proto Normal file
View File

@@ -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 Proxy(ProxyRequest) returns (ProxyResponse) {}
}
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 ProxyRequest {
string shortURL = 1;
}
message ProxyResponse {
string destinationURL = 1;
}