gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/runtime/service/handler/handler.go (about) 1 package handler 2 3 import ( 4 "context" 5 6 "gitee.com/liuxuezhan/go-micro-v1.18.0/errors" 7 "gitee.com/liuxuezhan/go-micro-v1.18.0/runtime" 8 pb "gitee.com/liuxuezhan/go-micro-v1.18.0/runtime/service/proto" 9 ) 10 11 type Runtime struct { 12 Runtime runtime.Runtime 13 } 14 15 func toProto(s *runtime.Service) *pb.Service { 16 return &pb.Service{ 17 Name: s.Name, 18 Version: s.Version, 19 Source: s.Source, 20 Metadata: s.Metadata, 21 } 22 } 23 24 func toService(s *pb.Service) *runtime.Service { 25 return &runtime.Service{ 26 Name: s.Name, 27 Version: s.Version, 28 Source: s.Source, 29 Metadata: s.Metadata, 30 } 31 } 32 33 func toCreateOptions(opts *pb.CreateOptions) []runtime.CreateOption { 34 options := []runtime.CreateOption{} 35 // command options 36 if len(opts.Command) > 0 { 37 options = append(options, runtime.WithCommand(opts.Command...)) 38 } 39 // env options 40 if len(opts.Env) > 0 { 41 options = append(options, runtime.WithEnv(opts.Env)) 42 } 43 44 // TODO: output options 45 46 return options 47 } 48 49 func toReadOptions(opts *pb.ReadOptions) []runtime.ReadOption { 50 options := []runtime.ReadOption{} 51 if len(opts.Service) > 0 { 52 options = append(options, runtime.ReadService(opts.Service)) 53 } 54 if len(opts.Version) > 0 { 55 options = append(options, runtime.ReadVersion(opts.Version)) 56 } 57 if len(opts.Type) > 0 { 58 options = append(options, runtime.ReadType(opts.Type)) 59 } 60 61 return options 62 } 63 64 func (r *Runtime) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error { 65 if req.Service == nil { 66 return errors.BadRequest("go.micro.runtime", "blank service") 67 } 68 69 var options []runtime.CreateOption 70 if req.Options != nil { 71 options = toCreateOptions(req.Options) 72 } 73 74 service := toService(req.Service) 75 err := r.Runtime.Create(service, options...) 76 if err != nil { 77 return errors.InternalServerError("go.micro.runtime", err.Error()) 78 } 79 80 return nil 81 } 82 83 func (r *Runtime) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error { 84 var options []runtime.ReadOption 85 if req.Options != nil { 86 options = toReadOptions(req.Options) 87 } 88 89 services, err := r.Runtime.Read(options...) 90 if err != nil { 91 return errors.InternalServerError("go.micro.runtime", err.Error()) 92 } 93 94 for _, service := range services { 95 rsp.Services = append(rsp.Services, toProto(service)) 96 } 97 98 return nil 99 } 100 101 func (r *Runtime) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error { 102 if req.Service == nil { 103 return errors.BadRequest("go.micro.runtime", "blank service") 104 } 105 106 // TODO: add opts 107 service := toService(req.Service) 108 err := r.Runtime.Update(service) 109 if err != nil { 110 return errors.InternalServerError("go.micro.runtime", err.Error()) 111 } 112 113 return nil 114 } 115 116 func (r *Runtime) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error { 117 if req.Service == nil { 118 return errors.BadRequest("go.micro.runtime", "blank service") 119 } 120 121 // TODO: add opts 122 service := toService(req.Service) 123 err := r.Runtime.Delete(service) 124 if err != nil { 125 return errors.InternalServerError("go.micro.runtime", err.Error()) 126 } 127 128 return nil 129 } 130 131 func (r *Runtime) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error { 132 services, err := r.Runtime.List() 133 if err != nil { 134 return errors.InternalServerError("go.micro.runtime", err.Error()) 135 } 136 137 for _, service := range services { 138 rsp.Services = append(rsp.Services, toProto(service)) 139 } 140 141 return nil 142 }