diff --git a/nft/.gitignore b/nft/.gitignore new file mode 100644 index 0000000..42a5f25 --- /dev/null +++ b/nft/.gitignore @@ -0,0 +1,2 @@ + +nft diff --git a/nft/Dockerfile b/nft/Dockerfile new file mode 100644 index 0000000..eecf069 --- /dev/null +++ b/nft/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD nft /nft +ENTRYPOINT [ "/nft" ] diff --git a/nft/Makefile b/nft/Makefile new file mode 100644 index 0000000..c2ef85a --- /dev/null +++ b/nft/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/nft.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/nft.proto + +.PHONY: build +build: + go build -o nft *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t nft:latest diff --git a/nft/README.md b/nft/README.md new file mode 100644 index 0000000..c8ffa79 --- /dev/null +++ b/nft/README.md @@ -0,0 +1,7 @@ +Explore, buy and sell NFTs (coming soon) + +# NFT Service + +Explore, buy and sell NFTs with a simple API. Powered by OpenSea. +Create your own unique NFT now and launch it when the API is ready. + diff --git a/nft/examples.json b/nft/examples.json new file mode 100644 index 0000000..cb597bc --- /dev/null +++ b/nft/examples.json @@ -0,0 +1,15 @@ +{ + "vote": [ + { + "title": "Vote for the API", + "run_check": false, + "request": { + "message": "Launch it!" + }, + "response": { + "message": "Thanks for the vote!" + } + } + } + ] +} diff --git a/nft/generate.go b/nft/generate.go new file mode 100644 index 0000000..96f431a --- /dev/null +++ b/nft/generate.go @@ -0,0 +1,2 @@ +package main +//go:generate make proto diff --git a/nft/handler/nft.go b/nft/handler/nft.go new file mode 100644 index 0000000..26efcfc --- /dev/null +++ b/nft/handler/nft.go @@ -0,0 +1,50 @@ +package handler + +import ( + "context" + "sync" + "time" + + + "github.com/micro/services/pkg/tenant" + "github.com/micro/micro/v3/service/store" + pb "github.com/micro/services/nft/proto" +) + +type Nft struct{} + +var ( + mtx sync.RWMutex + + voteKey = "votes/" +) + +type Vote struct { + Id string `json:"id"` + Message string `json:"message"` + VotedAt time.Time `json:"voted_at"` +} + +func (n *Nft) Vote(ctx context.Context, req *pb.VoteRequest, rsp *pb.VoteResponse) error { + mtx.Lock() + defer mtx.Unlock() + + id, ok := tenant.FromContext(ctx) + if !ok { + id = "micro" + } + + rec := store.NewRecord(voteKey + id, &Vote{ + Id: id, + Message: req.Message, + VotedAt: time.Now(), + }) + + // we don't need to check the error + store.Write(rec) + + rsp.Message = "Thanks for the vote!" + + return nil +} + diff --git a/nft/main.go b/nft/main.go new file mode 100644 index 0000000..c8843fd --- /dev/null +++ b/nft/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/micro/services/nft/handler" + pb "github.com/micro/services/nft/proto" + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" +) + +func main() { + // Create service + srv := service.New( + service.Name("nft"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterNftHandler(srv.Server(), new(handler.Nft)) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/nft/micro.mu b/nft/micro.mu new file mode 100644 index 0000000..216a78e --- /dev/null +++ b/nft/micro.mu @@ -0,0 +1 @@ +service nft diff --git a/nft/proto/nft.pb.go b/nft/proto/nft.pb.go new file mode 100644 index 0000000..4bb0bd1 --- /dev/null +++ b/nft/proto/nft.pb.go @@ -0,0 +1,213 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.15.6 +// source: proto/nft.proto + +package nft + +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) +) + +// Vote to have the NFT api launched faster! +type VoteRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // optional message + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *VoteRequest) Reset() { + *x = VoteRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_nft_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *VoteRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*VoteRequest) ProtoMessage() {} + +func (x *VoteRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_nft_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 VoteRequest.ProtoReflect.Descriptor instead. +func (*VoteRequest) Descriptor() ([]byte, []int) { + return file_proto_nft_proto_rawDescGZIP(), []int{0} +} + +func (x *VoteRequest) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +type VoteResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // response message + Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *VoteResponse) Reset() { + *x = VoteResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_nft_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *VoteResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*VoteResponse) ProtoMessage() {} + +func (x *VoteResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_nft_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 VoteResponse.ProtoReflect.Descriptor instead. +func (*VoteResponse) Descriptor() ([]byte, []int) { + return file_proto_nft_proto_rawDescGZIP(), []int{1} +} + +func (x *VoteResponse) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_proto_nft_proto protoreflect.FileDescriptor + +var file_proto_nft_proto_rawDesc = []byte{ + 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6e, 0x66, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x03, 0x6e, 0x66, 0x74, 0x22, 0x27, 0x0a, 0x0b, 0x56, 0x6f, 0x74, 0x65, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, + 0x28, 0x0a, 0x0c, 0x56, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, + 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x34, 0x0a, 0x03, 0x4e, 0x66, 0x74, + 0x12, 0x2d, 0x0a, 0x04, 0x56, 0x6f, 0x74, 0x65, 0x12, 0x10, 0x2e, 0x6e, 0x66, 0x74, 0x2e, 0x56, + 0x6f, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x6e, 0x66, 0x74, + 0x2e, 0x56, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, + 0x0d, 0x5a, 0x0b, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x6e, 0x66, 0x74, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_nft_proto_rawDescOnce sync.Once + file_proto_nft_proto_rawDescData = file_proto_nft_proto_rawDesc +) + +func file_proto_nft_proto_rawDescGZIP() []byte { + file_proto_nft_proto_rawDescOnce.Do(func() { + file_proto_nft_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_nft_proto_rawDescData) + }) + return file_proto_nft_proto_rawDescData +} + +var file_proto_nft_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_nft_proto_goTypes = []interface{}{ + (*VoteRequest)(nil), // 0: nft.VoteRequest + (*VoteResponse)(nil), // 1: nft.VoteResponse +} +var file_proto_nft_proto_depIdxs = []int32{ + 0, // 0: nft.Nft.Vote:input_type -> nft.VoteRequest + 1, // 1: nft.Nft.Vote:output_type -> nft.VoteResponse + 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_nft_proto_init() } +func file_proto_nft_proto_init() { + if File_proto_nft_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_nft_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*VoteRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_nft_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*VoteResponse); 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_nft_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_nft_proto_goTypes, + DependencyIndexes: file_proto_nft_proto_depIdxs, + MessageInfos: file_proto_nft_proto_msgTypes, + }.Build() + File_proto_nft_proto = out.File + file_proto_nft_proto_rawDesc = nil + file_proto_nft_proto_goTypes = nil + file_proto_nft_proto_depIdxs = nil +} diff --git a/nft/proto/nft.pb.micro.go b/nft/proto/nft.pb.micro.go new file mode 100644 index 0000000..8075ddc --- /dev/null +++ b/nft/proto/nft.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/nft.proto + +package nft + +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 Nft service + +func NewNftEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Nft service + +type NftService interface { + Vote(ctx context.Context, in *VoteRequest, opts ...client.CallOption) (*VoteResponse, error) +} + +type nftService struct { + c client.Client + name string +} + +func NewNftService(name string, c client.Client) NftService { + return &nftService{ + c: c, + name: name, + } +} + +func (c *nftService) Vote(ctx context.Context, in *VoteRequest, opts ...client.CallOption) (*VoteResponse, error) { + req := c.c.NewRequest(c.name, "Nft.Vote", in) + out := new(VoteResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Nft service + +type NftHandler interface { + Vote(context.Context, *VoteRequest, *VoteResponse) error +} + +func RegisterNftHandler(s server.Server, hdlr NftHandler, opts ...server.HandlerOption) error { + type nft interface { + Vote(ctx context.Context, in *VoteRequest, out *VoteResponse) error + } + type Nft struct { + nft + } + h := &nftHandler{hdlr} + return s.Handle(s.NewHandler(&Nft{h}, opts...)) +} + +type nftHandler struct { + NftHandler +} + +func (h *nftHandler) Vote(ctx context.Context, in *VoteRequest, out *VoteResponse) error { + return h.NftHandler.Vote(ctx, in, out) +} diff --git a/nft/proto/nft.proto b/nft/proto/nft.proto new file mode 100644 index 0000000..39fb5c7 --- /dev/null +++ b/nft/proto/nft.proto @@ -0,0 +1,20 @@ +syntax = "proto3"; + +package nft; + +option go_package = "./proto;nft"; + +service Nft { + rpc Vote(VoteRequest) returns (VoteResponse) {} +} + +// Vote to have the NFT api launched faster! +message VoteRequest { + // optional message + string message = 1; +} + +message VoteResponse { + // response message + string message = 2; +} diff --git a/nft/publicapi.json b/nft/publicapi.json new file mode 100644 index 0000000..236145d --- /dev/null +++ b/nft/publicapi.json @@ -0,0 +1,6 @@ +{ + "name": "nft", + "icon": "🪙", + "category": "coming soon", + "display_name": "NFT (Coming Soon)" +}