mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
Pr/avatar (#319)
* feat: add avatar api close #310 * chore: add description line * fix: typo * chore: reformat the json string to make it more readable and fix few bad indents * fix: minimum go version * feat: upload to Micro's CDN * chore: delete redundant blank line * chore: update README.md
This commit is contained in:
2
avatar/.gitignore
vendored
Normal file
2
avatar/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
avatar
|
||||
3
avatar/Dockerfile
Normal file
3
avatar/Dockerfile
Normal file
@@ -0,0 +1,3 @@
|
||||
FROM alpine
|
||||
ADD avatar /avatar
|
||||
ENTRYPOINT [ "/avatar" ]
|
||||
28
avatar/Makefile
Normal file
28
avatar/Makefile
Normal file
@@ -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/avatar.proto
|
||||
|
||||
.PHONY: proto
|
||||
proto:
|
||||
protoc --proto_path=. --micro_out=. --go_out=:. proto/avatar.proto
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go build -o avatar *.go
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
go test -v ./... -cover
|
||||
|
||||
.PHONY: docker
|
||||
docker:
|
||||
docker build . -t avatar:latest
|
||||
6
avatar/README.md
Normal file
6
avatar/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
Generate an avatar
|
||||
|
||||
# Avatar Service
|
||||
|
||||
Generate a unique avatar for your user profiles,
|
||||
NFT crypto punks and much more.
|
||||
34
avatar/examples.json
Normal file
34
avatar/examples.json
Normal file
File diff suppressed because one or more lines are too long
3
avatar/generate.go
Normal file
3
avatar/generate.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package main
|
||||
|
||||
//go:generate make proto
|
||||
98
avatar/handler/avatar.go
Normal file
98
avatar/handler/avatar.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/o1egl/govatar"
|
||||
|
||||
pb "github.com/micro/services/avatar/proto"
|
||||
imagePb "github.com/micro/services/image/proto"
|
||||
)
|
||||
|
||||
type avatar struct {
|
||||
imageSvc imagePb.ImageService
|
||||
}
|
||||
|
||||
func NewAvatar(service imagePb.ImageService) *avatar {
|
||||
return &avatar{
|
||||
imageSvc: service,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate is used to generate a avatar
|
||||
func (e *avatar) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.GenerateResponse) error {
|
||||
var gender govatar.Gender
|
||||
|
||||
// gender, default is `male`
|
||||
if req.Gender == "male" {
|
||||
gender = govatar.MALE
|
||||
} else if req.Gender == "female" {
|
||||
gender = govatar.FEMALE
|
||||
} else {
|
||||
gender = govatar.MALE
|
||||
}
|
||||
|
||||
// generate avatar
|
||||
var avatarImg image.Image
|
||||
var err error
|
||||
|
||||
if req.Username == "" {
|
||||
avatarImg, err = govatar.Generate(gender)
|
||||
} else {
|
||||
avatarImg, err = govatar.GenerateForUsername(gender, req.Username)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalServerError("avatar.generate", "generate avatarImg error: %v", err)
|
||||
}
|
||||
|
||||
// format avatar image, default is `jpeg`
|
||||
format := req.Format
|
||||
if format != "png" && format != "jpeg" {
|
||||
format = "jpeg"
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if format == "png" {
|
||||
err = png.Encode(buf, avatarImg)
|
||||
} else {
|
||||
err = jpeg.Encode(buf, avatarImg, nil)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalServerError("avatar.generate", "encode avatar image error: %v", err)
|
||||
}
|
||||
|
||||
base64String := fmt.Sprintf("data:image/%s;base64,%s", format, base64.StdEncoding.EncodeToString(buf.Bytes()))
|
||||
|
||||
if !req.Upload {
|
||||
rsp.Base64 = base64String
|
||||
return nil
|
||||
}
|
||||
|
||||
// upload to CDN
|
||||
name := req.Username
|
||||
if name == "" {
|
||||
uid, _ := uuid.NewUUID()
|
||||
name = uid.String()
|
||||
}
|
||||
|
||||
uploadResp, err := e.imageSvc.Upload(ctx, &imagePb.UploadRequest{
|
||||
Base64: base64String,
|
||||
Name: fmt.Sprintf("%s.%s", name, format),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return errors.InternalServerError("avatar.generate", "upload avatar image error: %v", err)
|
||||
}
|
||||
|
||||
rsp.Url = uploadResp.Url
|
||||
|
||||
return nil
|
||||
}
|
||||
27
avatar/main.go
Normal file
27
avatar/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/micro/services/avatar/handler"
|
||||
pb "github.com/micro/services/avatar/proto"
|
||||
imagePb "github.com/micro/services/image/proto"
|
||||
|
||||
"github.com/micro/micro/v3/service"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create service
|
||||
srv := service.New(
|
||||
service.Name("avatar"),
|
||||
service.Version("latest"),
|
||||
)
|
||||
|
||||
// Register handler
|
||||
hdlr := handler.NewAvatar(imagePb.NewImageService("image", srv.Client()))
|
||||
pb.RegisterAvatarHandler(srv.Server(), hdlr)
|
||||
|
||||
// Run service
|
||||
if err := srv.Run(); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
}
|
||||
1
avatar/micro.mu
Normal file
1
avatar/micro.mu
Normal file
@@ -0,0 +1 @@
|
||||
service avatar
|
||||
259
avatar/proto/avatar.pb.go
Normal file
259
avatar/proto/avatar.pb.go
Normal file
@@ -0,0 +1,259 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.27.1
|
||||
// protoc v3.19.1
|
||||
// source: proto/avatar.proto
|
||||
|
||||
package avatar
|
||||
|
||||
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 GenerateRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// avatar's gender, `male` or `female`, default is `male`
|
||||
Gender string `protobuf:"bytes,1,opt,name=gender,proto3" json:"gender,omitempty"`
|
||||
// avatar's username, unique username will generates the unique avatar;
|
||||
// if username == "", will generate a random avatar in every request
|
||||
// if upload == true, username will be used as CDN filename
|
||||
Username string `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"`
|
||||
// encode format of avatar image, `png` or `jpeg`, default is `jpeg`
|
||||
Format string `protobuf:"bytes,3,opt,name=format,proto3" json:"format,omitempty"`
|
||||
// if upload to m3o CDN, default is `false`
|
||||
// if update = true, then it'll return the CDN url
|
||||
Upload bool `protobuf:"varint,4,opt,name=upload,proto3" json:"upload,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GenerateRequest) Reset() {
|
||||
*x = GenerateRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_avatar_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GenerateRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GenerateRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GenerateRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_avatar_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 GenerateRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GenerateRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_avatar_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *GenerateRequest) GetGender() string {
|
||||
if x != nil {
|
||||
return x.Gender
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GenerateRequest) GetUsername() string {
|
||||
if x != nil {
|
||||
return x.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GenerateRequest) GetFormat() string {
|
||||
if x != nil {
|
||||
return x.Format
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GenerateRequest) GetUpload() bool {
|
||||
if x != nil {
|
||||
return x.Upload
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type GenerateResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// Micro's CDN url of the avatar image
|
||||
Url string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"`
|
||||
// base64encode string of the avatar image
|
||||
Base64 string `protobuf:"bytes,2,opt,name=base64,proto3" json:"base64,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GenerateResponse) Reset() {
|
||||
*x = GenerateResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_avatar_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GenerateResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GenerateResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GenerateResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_avatar_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 GenerateResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GenerateResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_avatar_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *GenerateResponse) GetUrl() string {
|
||||
if x != nil {
|
||||
return x.Url
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GenerateResponse) GetBase64() string {
|
||||
if x != nil {
|
||||
return x.Base64
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_proto_avatar_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_proto_avatar_proto_rawDesc = []byte{
|
||||
0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x2e, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x22, 0x75, 0x0a, 0x0f,
|
||||
0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
|
||||
0x16, 0x0a, 0x06, 0x67, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x06, 0x67, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e,
|
||||
0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e,
|
||||
0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x03, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x75,
|
||||
0x70, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x75, 0x70, 0x6c,
|
||||
0x6f, 0x61, 0x64, 0x22, 0x3c, 0x0a, 0x10, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52,
|
||||
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x62, 0x61, 0x73,
|
||||
0x65, 0x36, 0x34, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x62, 0x61, 0x73, 0x65, 0x36,
|
||||
0x34, 0x32, 0x49, 0x0a, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x12, 0x3f, 0x0a, 0x08, 0x47,
|
||||
0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x12, 0x17, 0x2e, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72,
|
||||
0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
|
||||
0x1a, 0x18, 0x2e, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61,
|
||||
0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x10, 0x5a, 0x0e,
|
||||
0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x62, 0x06,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_proto_avatar_proto_rawDescOnce sync.Once
|
||||
file_proto_avatar_proto_rawDescData = file_proto_avatar_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_proto_avatar_proto_rawDescGZIP() []byte {
|
||||
file_proto_avatar_proto_rawDescOnce.Do(func() {
|
||||
file_proto_avatar_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_avatar_proto_rawDescData)
|
||||
})
|
||||
return file_proto_avatar_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_proto_avatar_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_proto_avatar_proto_goTypes = []interface{}{
|
||||
(*GenerateRequest)(nil), // 0: avatar.GenerateRequest
|
||||
(*GenerateResponse)(nil), // 1: avatar.GenerateResponse
|
||||
}
|
||||
var file_proto_avatar_proto_depIdxs = []int32{
|
||||
0, // 0: avatar.Avatar.Generate:input_type -> avatar.GenerateRequest
|
||||
1, // 1: avatar.Avatar.Generate:output_type -> avatar.GenerateResponse
|
||||
1, // [1:2] is the sub-list for method output_type
|
||||
0, // [0:1] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_proto_avatar_proto_init() }
|
||||
func file_proto_avatar_proto_init() {
|
||||
if File_proto_avatar_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_proto_avatar_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GenerateRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_avatar_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GenerateResponse); 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_avatar_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_proto_avatar_proto_goTypes,
|
||||
DependencyIndexes: file_proto_avatar_proto_depIdxs,
|
||||
MessageInfos: file_proto_avatar_proto_msgTypes,
|
||||
}.Build()
|
||||
File_proto_avatar_proto = out.File
|
||||
file_proto_avatar_proto_rawDesc = nil
|
||||
file_proto_avatar_proto_goTypes = nil
|
||||
file_proto_avatar_proto_depIdxs = nil
|
||||
}
|
||||
93
avatar/proto/avatar.pb.micro.go
Normal file
93
avatar/proto/avatar.pb.micro.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: proto/avatar.proto
|
||||
|
||||
package avatar
|
||||
|
||||
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 Avatar service
|
||||
|
||||
func NewAvatarEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for Avatar service
|
||||
|
||||
type AvatarService interface {
|
||||
Generate(ctx context.Context, in *GenerateRequest, opts ...client.CallOption) (*GenerateResponse, error)
|
||||
}
|
||||
|
||||
type avatarService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewAvatarService(name string, c client.Client) AvatarService {
|
||||
return &avatarService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *avatarService) Generate(ctx context.Context, in *GenerateRequest, opts ...client.CallOption) (*GenerateResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Avatar.Generate", in)
|
||||
out := new(GenerateResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Avatar service
|
||||
|
||||
type AvatarHandler interface {
|
||||
Generate(context.Context, *GenerateRequest, *GenerateResponse) error
|
||||
}
|
||||
|
||||
func RegisterAvatarHandler(s server.Server, hdlr AvatarHandler, opts ...server.HandlerOption) error {
|
||||
type avatar interface {
|
||||
Generate(ctx context.Context, in *GenerateRequest, out *GenerateResponse) error
|
||||
}
|
||||
type Avatar struct {
|
||||
avatar
|
||||
}
|
||||
h := &avatarHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Avatar{h}, opts...))
|
||||
}
|
||||
|
||||
type avatarHandler struct {
|
||||
AvatarHandler
|
||||
}
|
||||
|
||||
func (h *avatarHandler) Generate(ctx context.Context, in *GenerateRequest, out *GenerateResponse) error {
|
||||
return h.AvatarHandler.Generate(ctx, in, out)
|
||||
}
|
||||
30
avatar/proto/avatar.proto
Normal file
30
avatar/proto/avatar.proto
Normal file
@@ -0,0 +1,30 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package avatar;
|
||||
|
||||
option go_package = "./proto;avatar";
|
||||
|
||||
service Avatar {
|
||||
rpc Generate(GenerateRequest) returns (GenerateResponse) {}
|
||||
}
|
||||
|
||||
message GenerateRequest {
|
||||
// avatar's gender, `male` or `female`, default is `male`
|
||||
string gender = 1;
|
||||
// avatar's username, unique username will generates the unique avatar;
|
||||
// if username == "", will generate a random avatar in every request
|
||||
// if upload == true, username will be used as CDN filename rather than a random uuid string
|
||||
string username = 2;
|
||||
// encode format of avatar image, `png` or `jpeg`, default is `jpeg`
|
||||
string format = 3;
|
||||
// if upload to m3o CDN, default is `false`
|
||||
// if update = true, then it'll return the CDN url
|
||||
bool upload = 4;
|
||||
}
|
||||
|
||||
message GenerateResponse {
|
||||
// Micro's CDN url of the avatar image
|
||||
string url = 1;
|
||||
// base64encode string of the avatar image
|
||||
string base64 = 2;
|
||||
}
|
||||
6
avatar/publicapi.json
Normal file
6
avatar/publicapi.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "avatar",
|
||||
"icon": "🤳🏻",
|
||||
"category": "for fun",
|
||||
"display_name": "Avatar"
|
||||
}
|
||||
2
go.mod
2
go.mod
@@ -34,6 +34,7 @@ require (
|
||||
github.com/micro/micro/v3 v3.8.0
|
||||
github.com/miekg/dns v1.1.31 // indirect
|
||||
github.com/minio/minio-go/v7 v7.0.16
|
||||
github.com/o1egl/govatar v0.3.0
|
||||
github.com/onsi/gomega v1.10.5
|
||||
github.com/oschwald/geoip2-golang v1.5.0
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
@@ -58,7 +59,6 @@ require (
|
||||
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1
|
||||
golang.org/x/text v0.3.6
|
||||
google.golang.org/api v0.59.0
|
||||
google.golang.org/genproto v0.0.0-20211008145708-270636b82663 // indirect
|
||||
google.golang.org/grpc/examples v0.0.0-20211103202053-3b94303f3754 // indirect
|
||||
google.golang.org/protobuf v1.27.1
|
||||
googlemaps.github.io/maps v1.3.1
|
||||
|
||||
3
go.sum
3
go.sum
@@ -525,6 +525,8 @@ github.com/nrdcg/goinwx v0.6.1/go.mod h1:XPiut7enlbEdntAqalBIqcYcTEVhpv/dKWgDCX2
|
||||
github.com/nrdcg/namesilo v0.2.1/go.mod h1:lwMvfQTyYq+BbjJd30ylEG4GPSS6PII0Tia4rRpRiyw=
|
||||
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
github.com/o1egl/govatar v0.3.0 h1:hGDsiJJs6qgQ6Ea4JiaukRsUKTY2Ai4dgMEdsYvlUa0=
|
||||
github.com/o1egl/govatar v0.3.0/go.mod h1:YeDGDII+2Ji1RcBKvb1KqaPhk4PmuZyBq+rPYc6b+cQ=
|
||||
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||
github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M=
|
||||
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
|
||||
@@ -661,6 +663,7 @@ github.com/uber/jaeger-client-go v2.29.1+incompatible h1:R9ec3zO3sGpzs0abd43Y+fB
|
||||
github.com/uber/jaeger-client-go v2.29.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
|
||||
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
|
||||
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
|
||||
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
|
||||
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
|
||||
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
{
|
||||
"add": [{
|
||||
"add": [
|
||||
{
|
||||
"title": "Add a new feed",
|
||||
"description": "Add a new rss feed to the crawler",
|
||||
"run_check": true,
|
||||
@@ -10,8 +10,10 @@
|
||||
"category": "news"
|
||||
},
|
||||
"response": {}
|
||||
}],
|
||||
"feed": [{
|
||||
}
|
||||
],
|
||||
"feed": [
|
||||
{
|
||||
"title": "Read a feed",
|
||||
"description": "Read an rss feed by name",
|
||||
"run_check": true,
|
||||
@@ -38,8 +40,10 @@
|
||||
}
|
||||
]
|
||||
}
|
||||
}],
|
||||
"list": [{
|
||||
}
|
||||
],
|
||||
"list": [
|
||||
{
|
||||
"title": "List rss feeds",
|
||||
"description": "List the saved rss feeds",
|
||||
"run_check": true,
|
||||
@@ -54,8 +58,10 @@
|
||||
}
|
||||
]
|
||||
}
|
||||
}],
|
||||
"remove": [{
|
||||
}
|
||||
],
|
||||
"remove": [
|
||||
{
|
||||
"title": "Remove a feed",
|
||||
"description": "Remove an rss feed from the crawler",
|
||||
"run_check": true,
|
||||
@@ -63,5 +69,6 @@
|
||||
"name": "bbc"
|
||||
},
|
||||
"response": {}
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user