diff --git a/doc/software b/doc/software index f6033b0..f8355a5 100644 --- a/doc/software +++ b/doc/software @@ -141,25 +141,26 @@ onedrive on linux: insync with aur. Works but slow List of frequently used commands and flags -| command | Description | -|--------------------------------------------------|-----------------------------------------------------------------------| -| docker run -d -p 80:80 --name my_container nginx | run container from image, -d: in background mode, --name: assign name | -| docker pull ubuntu | pull an image | -| docker build -t my_image:latest -f Dockerfile . | build an image from Dockerfile. -t: tag, -f: name of dockerfile, | -| docker images | list images | -| docker ps -a | list containers, -a to show all containers (including inactive) | -| docker stop my_container | stop a container | -| docker start my_container | start a container | -| docker rm my_contianer | remove a container (won't in ps -a any more), -f: force | -| docker rmi my_image:latest | remove a image, -f: force | -| docker save my_image -o my_image.tar | save a image to a tar (which can be loaded afterwards) | -| docker load -i my_image.tar | load a tar to a image | -| docker exec my_container ls | run a command in running container | -| docker exec -it my_container /bin/bash | -it: interactive terminal mode, so you can run bash cmds in docker | -| docker logs my_container | show current logs of a container | -| docker logs -f my_contiainer | -f: follow. new logs will be printed as well | -| docker system/container/network/image prune | remove unused container/network/image | -| docker system/container/network/image | show a list of commands related to system/container/network/image | +| command | Description | +|-------------------------------------------------------------------|-----------------------------------------------------------------------| +| docker run -d -p 80:80 --name my_container nginx | run container from image, -d: in background mode, --name: assign name | +| docker run -d -p 80:80 -p 81:81 -v /local/path:/app/data/data.csv | -v: mount local path | +| docker pull ubuntu | pull an image | +| docker build -t my_image:latest -f Dockerfile . | build an image from Dockerfile. -t: tag, -f: name of dockerfile, | +| docker images | list images | +| docker ps -a | list containers, -a to show all containers (including inactive) | +| docker stop my_container | stop a container | +| docker start my_container | start a container | +| docker rm my_contianer | remove a container (won't in ps -a any more), -f: force | +| docker rmi my_image:latest | remove a image, -f: force | +| docker save my_image -o my_image.tar | save a image to a tar (which can be loaded afterwards) | +| docker load -i my_image.tar | load a tar to a image | +| docker exec my_container ls | run a command in running container | +| docker exec -it my_container /bin/bash | -it: interactive terminal mode, so you can run bash cmds in docker | +| docker logs my_container | show current logs of a container | +| docker logs -f my_contiainer | -f: follow. new logs will be printed as well | +| docker system/container/network/image prune | remove unused container/network/image | +| docker system/container/network/image | show a list of commands related to system/container/network/image | Steps for docker push 1. docker login diff --git a/go/grpc/client/main.go b/go/grpc/client/main.go new file mode 100644 index 0000000..0f1fe34 --- /dev/null +++ b/go/grpc/client/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "grpc/pb" + + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +func main() { + conn, _ := grpc.NewClient("127.0.0.1:8000", grpc.WithTransportCredentials(insecure.NewCredentials())) + defer conn.Close() + client := pb.NewSearchServiceClient(conn) + + req := &pb.SearchRequest{Name: "John"} + // Search is the rpc defined in proto service, which receives SearchRequest and returns SearchResponse + resp, _ := client.Search(context.Background(), req) + + fmt.Println(resp) + +} diff --git a/go/grpc/go.mod b/go/grpc/go.mod new file mode 100644 index 0000000..d4be9ae --- /dev/null +++ b/go/grpc/go.mod @@ -0,0 +1,15 @@ +module grpc + +go 1.22.5 + +require ( + google.golang.org/grpc v1.65.0 + google.golang.org/protobuf v1.34.2 +) + +require ( + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect +) diff --git a/go/grpc/go.sum b/go/grpc/go.sum new file mode 100644 index 0000000..2a4541c --- /dev/null +++ b/go/grpc/go.sum @@ -0,0 +1,14 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0= +google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= +google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= diff --git a/go/grpc/pb/person.pb.go b/go/grpc/pb/person.pb.go new file mode 100644 index 0000000..34671fd --- /dev/null +++ b/go/grpc/pb/person.pb.go @@ -0,0 +1,284 @@ +// compile using + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v5.27.2 +// source: person.proto + +package pb + +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 Person struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Age int32 `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"` +} + +func (x *Person) Reset() { + *x = Person{} + if protoimpl.UnsafeEnabled { + mi := &file_person_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Person) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Person) ProtoMessage() {} + +func (x *Person) ProtoReflect() protoreflect.Message { + mi := &file_person_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 Person.ProtoReflect.Descriptor instead. +func (*Person) Descriptor() ([]byte, []int) { + return file_person_proto_rawDescGZIP(), []int{0} +} + +func (x *Person) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Person) GetAge() int32 { + if x != nil { + return x.Age + } + return 0 +} + +type SearchRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *SearchRequest) Reset() { + *x = SearchRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_person_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SearchRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SearchRequest) ProtoMessage() {} + +func (x *SearchRequest) ProtoReflect() protoreflect.Message { + mi := &file_person_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 SearchRequest.ProtoReflect.Descriptor instead. +func (*SearchRequest) Descriptor() ([]byte, []int) { + return file_person_proto_rawDescGZIP(), []int{1} +} + +func (x *SearchRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type SearchResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Person *Person `protobuf:"bytes,1,opt,name=person,proto3" json:"person,omitempty"` +} + +func (x *SearchResponse) Reset() { + *x = SearchResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_person_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SearchResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SearchResponse) ProtoMessage() {} + +func (x *SearchResponse) ProtoReflect() protoreflect.Message { + mi := &file_person_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 SearchResponse.ProtoReflect.Descriptor instead. +func (*SearchResponse) Descriptor() ([]byte, []int) { + return file_person_proto_rawDescGZIP(), []int{2} +} + +func (x *SearchResponse) GetPerson() *Person { + if x != nil { + return x.Person + } + return nil +} + +var File_person_proto protoreflect.FileDescriptor + +var file_person_proto_rawDesc = []byte{ + 0x0a, 0x0c, 0x70, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, + 0x70, 0x62, 0x22, 0x2e, 0x0a, 0x06, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, + 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, + 0x12, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x61, + 0x67, 0x65, 0x22, 0x23, 0x0a, 0x0d, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 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, 0x34, 0x0a, 0x0e, 0x53, 0x65, 0x61, 0x72, 0x63, + 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x06, 0x70, 0x65, 0x72, + 0x73, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x70, 0x62, 0x2e, 0x50, + 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x52, 0x06, 0x70, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x32, 0x40, 0x0a, + 0x0d, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x2f, + 0x0a, 0x06, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x65, + 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x62, + 0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, + 0x03, 0x5a, 0x01, 0x2e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_person_proto_rawDescOnce sync.Once + file_person_proto_rawDescData = file_person_proto_rawDesc +) + +func file_person_proto_rawDescGZIP() []byte { + file_person_proto_rawDescOnce.Do(func() { + file_person_proto_rawDescData = protoimpl.X.CompressGZIP(file_person_proto_rawDescData) + }) + return file_person_proto_rawDescData +} + +var file_person_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_person_proto_goTypes = []interface{}{ + (*Person)(nil), // 0: pb.Person + (*SearchRequest)(nil), // 1: pb.SearchRequest + (*SearchResponse)(nil), // 2: pb.SearchResponse +} +var file_person_proto_depIdxs = []int32{ + 0, // 0: pb.SearchResponse.person:type_name -> pb.Person + 1, // 1: pb.SearchService.Search:input_type -> pb.SearchRequest + 2, // 2: pb.SearchService.Search:output_type -> pb.SearchResponse + 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_person_proto_init() } +func file_person_proto_init() { + if File_person_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_person_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Person); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_person_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SearchRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_person_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SearchResponse); 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_person_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_person_proto_goTypes, + DependencyIndexes: file_person_proto_depIdxs, + MessageInfos: file_person_proto_msgTypes, + }.Build() + File_person_proto = out.File + file_person_proto_rawDesc = nil + file_person_proto_goTypes = nil + file_person_proto_depIdxs = nil +} diff --git a/go/grpc/pb/person.proto b/go/grpc/pb/person.proto new file mode 100644 index 0000000..75b59d7 --- /dev/null +++ b/go/grpc/pb/person.proto @@ -0,0 +1,24 @@ + +// compile using command +// protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative person.proto +syntax = "proto3"; +package pb; + +option go_package = "."; +message Person { + string name = 1; + int32 age = 2; + +} + +message SearchRequest { + string name = 1; +} + +message SearchResponse { + Person person = 1; +} + +service SearchService { + rpc Search (SearchRequest) returns (SearchResponse); +} diff --git a/go/grpc/pb/person_grpc.pb.go b/go/grpc/pb/person_grpc.pb.go new file mode 100644 index 0000000..877c8a9 --- /dev/null +++ b/go/grpc/pb/person_grpc.pb.go @@ -0,0 +1,105 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v5.27.2 +// source: person.proto + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// SearchServiceClient is the client API for SearchService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type SearchServiceClient interface { + Search(ctx context.Context, in *SearchRequest, opts ...grpc.CallOption) (*SearchResponse, error) +} + +type searchServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewSearchServiceClient(cc grpc.ClientConnInterface) SearchServiceClient { + return &searchServiceClient{cc} +} + +func (c *searchServiceClient) Search(ctx context.Context, in *SearchRequest, opts ...grpc.CallOption) (*SearchResponse, error) { + out := new(SearchResponse) + err := c.cc.Invoke(ctx, "/pb.SearchService/Search", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// SearchServiceServer is the server API for SearchService service. +// All implementations must embed UnimplementedSearchServiceServer +// for forward compatibility +type SearchServiceServer interface { + Search(context.Context, *SearchRequest) (*SearchResponse, error) + mustEmbedUnimplementedSearchServiceServer() +} + +// UnimplementedSearchServiceServer must be embedded to have forward compatible implementations. +type UnimplementedSearchServiceServer struct { +} + +func (UnimplementedSearchServiceServer) Search(context.Context, *SearchRequest) (*SearchResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Search not implemented") +} +func (UnimplementedSearchServiceServer) mustEmbedUnimplementedSearchServiceServer() {} + +// UnsafeSearchServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to SearchServiceServer will +// result in compilation errors. +type UnsafeSearchServiceServer interface { + mustEmbedUnimplementedSearchServiceServer() +} + +func RegisterSearchServiceServer(s grpc.ServiceRegistrar, srv SearchServiceServer) { + s.RegisterService(&SearchService_ServiceDesc, srv) +} + +func _SearchService_Search_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SearchRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SearchServiceServer).Search(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/pb.SearchService/Search", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SearchServiceServer).Search(ctx, req.(*SearchRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// SearchService_ServiceDesc is the grpc.ServiceDesc for SearchService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var SearchService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "pb.SearchService", + HandlerType: (*SearchServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Search", + Handler: _SearchService_Search_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "person.proto", +} diff --git a/go/grpc/server/main.go b/go/grpc/server/main.go new file mode 100644 index 0000000..c55a899 --- /dev/null +++ b/go/grpc/server/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + "fmt" + "grpc/pb" + "net" + + "google.golang.org/grpc" +) + +// Your implemented server +type server struct { + pb.UnimplementedSearchServiceServer +} + +// Should be the same name as defined in proto service +func (s *server) Search(ctx context.Context, in *pb.SearchRequest) (*pb.SearchResponse, error) { + name := in.Name + persons := []pb.Person{ + {Name: "Mike", Age: 15}, + {Name: "John", Age: 13}, + {Name: "Sarah", Age: 14}, + } + + fmt.Println("Get Req", *in) + var resp pb.SearchResponse + for _, person := range persons { + if person.Name == name { + resp.Person = &person + } + } + + return &resp, nil +} + +func main() { + listen, _ := net.Listen("tcp", ":8000") + + s := grpc.NewServer() + pb.RegisterSearchServiceServer(s, &server{}) + + s.Serve(listen) + +}