diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..4eeae68 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,2 @@ + +app diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 0000000..e66ecd4 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD app /app +ENTRYPOINT [ "/app" ] diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..0845b43 --- /dev/null +++ b/app/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/app.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/app.proto + +.PHONY: build +build: + go build -o app *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t app:latest diff --git a/app/README.md b/app/README.md new file mode 100644 index 0000000..097e258 --- /dev/null +++ b/app/README.md @@ -0,0 +1,6 @@ +Global app deployment (Coming soon) + +# App Service + +Deploy apps and services quickly and easily from a source url or container image. +Get a globally unique URL ([name].m3o.app) and share with others. Reserve your app name now. diff --git a/app/generate.go b/app/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/app/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/app/handler/app.go b/app/handler/app.go new file mode 100644 index 0000000..9a3c590 --- /dev/null +++ b/app/handler/app.go @@ -0,0 +1,119 @@ +package handler + +import ( + "context" + "crypto/sha1" + "fmt" + "io" + "regexp" + "sync" + "time" + + "github.com/micro/micro/v3/service/errors" + "github.com/micro/micro/v3/service/store" + pb "github.com/micro/services/app/proto" + "github.com/micro/services/pkg/tenant" +) + +type App struct{} + +var ( + mtx sync.Mutex + + ReservationKey = "reservedApp/" + NameFormat = regexp.MustCompilePOSIX("[a-z0-9]+") +) + +type Reservation struct { + // The app name + Name string `json:"name"` + // The owner e.g tenant id + Owner string `json:"owner"` + // Uniq associated token + Token string `json:"token"` + // Time of creation + Created time.Time `json:"created"` + // The expiry time + Expires time.Time `json:"expires"` +} + +func genToken(name, owner string) string { + h := sha1.New() + io.WriteString(h, name+owner) + return fmt.Sprintf("%x", h.Sum(nil)) +} + +// Call is a single request handler called via client.Call or the generated client code +func (a *App) Reserve(ctx context.Context, req *pb.ReserveRequest, rsp *pb.ReserveResponse) error { + id, ok := tenant.FromContext(ctx) + if !ok { + id = "micro" + } + + if len(req.Name) == 0 { + return errors.BadRequest("app.reserve", "missing app name") + } + + if len(req.Name) < 3 || len(req.Name) > 256 { + return errors.BadRequest("app.reserve", "name must be longer than 3-256 chars in length") + } + + if !NameFormat.MatchString(req.Name) { + return errors.BadRequest("app.reserve", "invalidate name format") + } + + // to prevent race conditions in reservation lets global lock + mtx.Lock() + defer mtx.Unlock() + + // check the store for reservation + recs, err := store.Read(ReservationKey + req.Name) + if err != nil && err != store.ErrNotFound { + return errors.InternalServerError("app.reserve", "failed to reserve name") + } + + var rsrv *Reservation + + // check if the record exists + if len(recs) > 0 { + // existing reservation exists + rec := recs[0] + + if err := rec.Decode(&rsrv); err != nil { + return errors.BadRequest("app.reserve", "name already reserved") + } + + // check the owner matches + if rsrv.Owner != id { + return errors.BadRequest("app.reserve", "name already reserved") + } + + // update the reservation + rsrv.Expires = time.Now().AddDate(1, 0, 0) + } else { + // not reserved + rsrv = &Reservation{ + Name: req.Name, + Owner: id, + Created: time.Now(), + Expires: time.Now().AddDate(1, 0, 0), + Token: genToken(req.Name, id), + } + } + + rec := store.NewRecord(ReservationKey+req.Name, rsrv) + + if err := store.Write(rec); err != nil { + return errors.InternalServerError("app.reserve", "error while reserving name") + } + + rsp.Reservation = &pb.Reservation{ + Name: rsrv.Name, + Owner: rsrv.Owner, + Created: rsrv.Created.Format(time.RFC3339Nano), + Expires: rsrv.Expires.Format(time.RFC3339Nano), + Token: rsrv.Token, + } + + return nil +} diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..17c1931 --- /dev/null +++ b/app/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" + "github.com/micro/services/app/handler" + pb "github.com/micro/services/app/proto" +) + +func main() { + // Create service + srv := service.New( + service.Name("app"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterAppHandler(srv.Server(), new(handler.App)) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/app/micro.mu b/app/micro.mu new file mode 100644 index 0000000..f38b779 --- /dev/null +++ b/app/micro.mu @@ -0,0 +1 @@ +service app diff --git a/app/proto/app.pb.go b/app/proto/app.pb.go new file mode 100644 index 0000000..cdd2db0 --- /dev/null +++ b/app/proto/app.pb.go @@ -0,0 +1,320 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.15.6 +// source: proto/app.proto + +package app + +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 Reservation struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // name of the app + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + // owner id + Owner string `protobuf:"bytes,2,opt,name=owner,proto3" json:"owner,omitempty"` + // associated token + Token string `protobuf:"bytes,3,opt,name=token,proto3" json:"token,omitempty"` + // time of reservation + Created string `protobuf:"bytes,4,opt,name=created,proto3" json:"created,omitempty"` + // time reservation expires + Expires string `protobuf:"bytes,5,opt,name=expires,proto3" json:"expires,omitempty"` +} + +func (x *Reservation) Reset() { + *x = Reservation{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_app_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Reservation) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Reservation) ProtoMessage() {} + +func (x *Reservation) ProtoReflect() protoreflect.Message { + mi := &file_proto_app_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 Reservation.ProtoReflect.Descriptor instead. +func (*Reservation) Descriptor() ([]byte, []int) { + return file_proto_app_proto_rawDescGZIP(), []int{0} +} + +func (x *Reservation) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Reservation) GetOwner() string { + if x != nil { + return x.Owner + } + return "" +} + +func (x *Reservation) GetToken() string { + if x != nil { + return x.Token + } + return "" +} + +func (x *Reservation) GetCreated() string { + if x != nil { + return x.Created + } + return "" +} + +func (x *Reservation) GetExpires() string { + if x != nil { + return x.Expires + } + return "" +} + +// Reserve your app name +type ReserveRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // name of your app e.g helloworld + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *ReserveRequest) Reset() { + *x = ReserveRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_app_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ReserveRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ReserveRequest) ProtoMessage() {} + +func (x *ReserveRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_app_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 ReserveRequest.ProtoReflect.Descriptor instead. +func (*ReserveRequest) Descriptor() ([]byte, []int) { + return file_proto_app_proto_rawDescGZIP(), []int{1} +} + +func (x *ReserveRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type ReserveResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Reservation *Reservation `protobuf:"bytes,1,opt,name=reservation,proto3" json:"reservation,omitempty"` +} + +func (x *ReserveResponse) Reset() { + *x = ReserveResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_app_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ReserveResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ReserveResponse) ProtoMessage() {} + +func (x *ReserveResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_app_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 ReserveResponse.ProtoReflect.Descriptor instead. +func (*ReserveResponse) Descriptor() ([]byte, []int) { + return file_proto_app_proto_rawDescGZIP(), []int{2} +} + +func (x *ReserveResponse) GetReservation() *Reservation { + if x != nil { + return x.Reservation + } + return nil +} + +var File_proto_app_proto protoreflect.FileDescriptor + +var file_proto_app_proto_rawDesc = []byte{ + 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x70, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x03, 0x61, 0x70, 0x70, 0x22, 0x81, 0x01, 0x0a, 0x0b, 0x52, 0x65, 0x73, 0x65, 0x72, + 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x77, + 0x6e, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, + 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, + 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, + 0x12, 0x18, 0x0a, 0x07, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x07, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x73, 0x22, 0x24, 0x0a, 0x0e, 0x52, 0x65, + 0x73, 0x65, 0x72, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, + 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, + 0x22, 0x45, 0x0a, 0x0f, 0x52, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x32, 0x0a, 0x0b, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x52, + 0x65, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0b, 0x72, 0x65, 0x73, 0x65, + 0x72, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x32, 0x3d, 0x0a, 0x03, 0x41, 0x70, 0x70, 0x12, 0x36, + 0x0a, 0x07, 0x52, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x12, 0x13, 0x2e, 0x61, 0x70, 0x70, 0x2e, + 0x52, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, + 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x52, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0d, 0x5a, 0x0b, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x3b, 0x61, 0x70, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_app_proto_rawDescOnce sync.Once + file_proto_app_proto_rawDescData = file_proto_app_proto_rawDesc +) + +func file_proto_app_proto_rawDescGZIP() []byte { + file_proto_app_proto_rawDescOnce.Do(func() { + file_proto_app_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_app_proto_rawDescData) + }) + return file_proto_app_proto_rawDescData +} + +var file_proto_app_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_app_proto_goTypes = []interface{}{ + (*Reservation)(nil), // 0: app.Reservation + (*ReserveRequest)(nil), // 1: app.ReserveRequest + (*ReserveResponse)(nil), // 2: app.ReserveResponse +} +var file_proto_app_proto_depIdxs = []int32{ + 0, // 0: app.ReserveResponse.reservation:type_name -> app.Reservation + 1, // 1: app.App.Reserve:input_type -> app.ReserveRequest + 2, // 2: app.App.Reserve:output_type -> app.ReserveResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_proto_app_proto_init() } +func file_proto_app_proto_init() { + if File_proto_app_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_app_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Reservation); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_app_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ReserveRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_app_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ReserveResponse); 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_app_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_app_proto_goTypes, + DependencyIndexes: file_proto_app_proto_depIdxs, + MessageInfos: file_proto_app_proto_msgTypes, + }.Build() + File_proto_app_proto = out.File + file_proto_app_proto_rawDesc = nil + file_proto_app_proto_goTypes = nil + file_proto_app_proto_depIdxs = nil +} diff --git a/app/proto/app.pb.micro.go b/app/proto/app.pb.micro.go new file mode 100644 index 0000000..ffbc03d --- /dev/null +++ b/app/proto/app.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/app.proto + +package app + +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 App service + +func NewAppEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for App service + +type AppService interface { + Reserve(ctx context.Context, in *ReserveRequest, opts ...client.CallOption) (*ReserveResponse, error) +} + +type appService struct { + c client.Client + name string +} + +func NewAppService(name string, c client.Client) AppService { + return &appService{ + c: c, + name: name, + } +} + +func (c *appService) Reserve(ctx context.Context, in *ReserveRequest, opts ...client.CallOption) (*ReserveResponse, error) { + req := c.c.NewRequest(c.name, "App.Reserve", in) + out := new(ReserveResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for App service + +type AppHandler interface { + Reserve(context.Context, *ReserveRequest, *ReserveResponse) error +} + +func RegisterAppHandler(s server.Server, hdlr AppHandler, opts ...server.HandlerOption) error { + type app interface { + Reserve(ctx context.Context, in *ReserveRequest, out *ReserveResponse) error + } + type App struct { + app + } + h := &appHandler{hdlr} + return s.Handle(s.NewHandler(&App{h}, opts...)) +} + +type appHandler struct { + AppHandler +} + +func (h *appHandler) Reserve(ctx context.Context, in *ReserveRequest, out *ReserveResponse) error { + return h.AppHandler.Reserve(ctx, in, out) +} diff --git a/app/proto/app.proto b/app/proto/app.proto new file mode 100644 index 0000000..b94f104 --- /dev/null +++ b/app/proto/app.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +package app; + +option go_package = "./proto;app"; + +service App { + rpc Reserve(ReserveRequest) returns (ReserveResponse) {} +} + +message Reservation { + // name of the app + string name = 1; + // owner id + string owner = 2; + // associated token + string token = 3; + // time of reservation + string created = 4; + // time reservation expires + string expires = 5; +} + +// Reserve your app name +message ReserveRequest { + // name of your app e.g helloworld + string name = 1; +} + +message ReserveResponse { + Reservation reservation = 1; +} diff --git a/app/publicapi.json b/app/publicapi.json new file mode 100644 index 0000000..736f4d4 --- /dev/null +++ b/app/publicapi.json @@ -0,0 +1,9 @@ +{ + "name": "app", + "icon": "💭", + "category": "hosting", + "display_name": "Apps", + "pricing": { + "App.Reserve": 1000000 + } +}