commit 60a30d6385c85bb3b269db146bbef4de18cd5abd Author: huangsimin Date: Wed May 13 14:57:57 2020 +0800 Simple v0.0.1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51f866a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.log +.rocksdb +data +workshop diff --git a/base.go b/base.go new file mode 100644 index 0000000..0b565ff --- /dev/null +++ b/base.go @@ -0,0 +1,97 @@ +package main + +import ( + "bytes" + "compress/flate" + "encoding/gob" + "log" + "math/rand" + "os" + + "github.com/tecbot/gorocksdb" +) + +// CheckErrorPanic 查错误 存在就panic操作 +func CheckErrorPanic(err error) { + if err != nil { + log.Panic(err) + } +} + +// OpenDB 打开rocksdb +func OpenDB() *gorocksdb.DB { + bbto := gorocksdb.NewDefaultBlockBasedTableOptions() + + bbto.SetBlockCache(gorocksdb.NewLRUCache(2 << 30)) + bbto.SetCacheIndexAndFilterBlocksWithHighPriority(true) + + opts := gorocksdb.NewDefaultOptions() + + bbto.SetFilterPolicy(gorocksdb.NewBloomFilter(16)) + + opts.SetBlockBasedTableFactory(bbto) + opts.SetCreateIfMissing(true) + opts.SetCreateIfMissingColumnFamilies(true) + opts.SetCompression(gorocksdb.LZ4Compression) + + db, err := gorocksdb.OpenDb(opts, ".rocksdb") + CheckErrorPanic(err) + return db +} + +var db = OpenDB() +var wopts = gorocksdb.NewDefaultWriteOptions() +var ropts = gorocksdb.NewDefaultReadOptions() + +// IKeyList key list interface +type IKeyList interface { + AppendKey(key []byte) + GetKeyList() [][]byte + GetLength() int +} + +// SaveGobFromRocksdb save data from rocksdb keyseek +func SaveGobFromRocksdb(fname string, seekKey string, v IKeyList) { + i := 0 + iter := db.NewIterator(ropts) + iter.Seek([]byte(seekKey)) + for ; iter.Valid(); iter.Next() { + i++ + // if i%1000 == 0 { + // t.Error(string(iter.Key().Data())) + // t.Error(string(iter.Value().Data())) + + // } + v.AppendKey(iter.Value().Data()) + } + log.Println(i) + + var keybuf = &bytes.Buffer{} + + enc := gob.NewEncoder(keybuf) + enc.Encode(v) + log.Println((len(keybuf.Bytes()))) + + f, err := os.OpenFile(fname, os.O_CREATE|os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm) + CheckErrorPanic(err) + + gw, err := flate.NewWriter(f, 5) + CheckErrorPanic(err) + defer gw.Close() + + gw.Write(keybuf.Bytes()) +} + +// LoadGob load gob from file +func LoadGob(fname string, v IKeyList) { + f, err := os.Open(fname) + CheckErrorPanic(err) + reader := flate.NewReader(f) + dec := gob.NewDecoder(reader) + dec.Decode(v) +} + +// GetRandomKey get ikeylist key by random +func GetRandomKey(v IKeyList) []byte { + return v.GetKeyList()[rand.Intn(v.GetLength())] +} diff --git a/case_test.go b/case_test.go new file mode 100644 index 0000000..3052246 --- /dev/null +++ b/case_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" +) + +func TestCheck(t *testing.T) { + // firstname lastname + fn := &FirstNameList{} + SaveGobFromRocksdb("./data/firstname.gob", "firstname-", fn) +} + +func TestReadData(t *testing.T) { + + ln := &FirstNameList{} + LoadGob("./data/firstname.gob", ln) + t.Error(len(ln.KeyList)) +} diff --git a/generate.sh b/generate.sh new file mode 100644 index 0000000..ccb0cf9 --- /dev/null +++ b/generate.sh @@ -0,0 +1,12 @@ +cd proto + protoc -I. -I/usr/include -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ + --go_out=plugins=grpc:.. \ + *.proto + + protoc -I/usr/include -I. \ + -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ + --grpc-gateway_out=logtostderr=true:.. \ + *.proto + + # protoc --go_out=plugins=grpc:.. *.proto +cd .. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f81c6d2 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module workshop + +go 1.14 + +require ( + github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect + github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect + github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b + github.com/golang/protobuf v1.4.1 + github.com/grpc-ecosystem/grpc-gateway v1.14.5 + github.com/stretchr/testify v1.5.1 // indirect + github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c + google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380 + google.golang.org/grpc v1.29.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6cb314a --- /dev/null +++ b/go.sum @@ -0,0 +1,102 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0= +github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= +github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A= +github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg= +github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk= +github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/grpc-ecosystem/grpc-gateway v1.14.5 h1:aiLxiiVzAXb7wb3lAmubA69IokWOoUNe+E7TdGKh8yw= +github.com/grpc-ecosystem/grpc-gateway v1.14.5/go.mod h1:UJ0EZAp832vCd54Wev9N1BMKEyvcZ5+IM0AwDrnlkEc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok= +github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20191002035440-2ec189313ef0 h1:2mqDk8w/o6UmeUCu5Qiq2y7iMf6anbx+YA8d1JFoFrs= +golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190927181202-20e1ac93f88c/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= +google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380 h1:xriR1EgvKfkKxIoU2uUvrMVl+H26359loFFUleSMXFo= +google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.24.0/go.mod h1:XDChyiUovWa60DnaeDeZmSW86xtLtjtZbwvSiRnRtcA= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4= +google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0 h1:cJv5/xdbk1NnMPR1VP9+HU6gupuG9MLBoH1r6RHZ2MY= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6b73adc --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "context" + "log" + "math/rand" + "net/http" + "os" + "time" + + "github.com/grpc-ecosystem/grpc-gateway/runtime" +) + +var fnl *FirstNameList = &FirstNameList{} +var lnl *LastNameList = &LastNameList{} + +func init() { + f, err := os.OpenFile("./my.log", os.O_CREATE|os.O_RDWR, os.ModePerm) + CheckErrorPanic(err) + log.SetOutput(f) + + rand.Seed(time.Now().UnixNano()) + LoadGob("./data/firstname.gob", fnl) + + LoadGob("./data/lastname.gob", lnl) + +} + +func main() { + + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + // gserver := grpc.NewServer() + mux := runtime.NewServeMux() + + s := &nameserver{} + // RegisterNameServer(gserver, s) + RegisterNameHandlerServer(ctx, mux, s) + log.Fatal(http.ListenAndServe(":4433", mux)) +} diff --git a/name_service.go b/name_service.go new file mode 100644 index 0000000..58e8440 --- /dev/null +++ b/name_service.go @@ -0,0 +1,75 @@ +package main + +import ( + "context" +) + +type LastNameList struct { + KeyList [][]byte +} + +func (ln *LastNameList) GetKeyList() [][]byte { + return ln.KeyList +} + +func (ln *LastNameList) AppendKey(key []byte) { + ln.KeyList = append(ln.KeyList, key) +} + +func (ln *LastNameList) GetLength() int { + return len(ln.KeyList) +} + +type FirstNameList struct { + KeyList [][]byte +} + +func (fn *FirstNameList) GetKeyList() [][]byte { + return fn.KeyList +} + +func (fn *FirstNameList) AppendKey(key []byte) { + fn.KeyList = append(fn.KeyList, key) +} + +func (fn *FirstNameList) GetLength() int { + return len(fn.KeyList) +} + +type nameserver struct { +} + +func (s *nameserver) FirstName(cxt context.Context, request *NameRequest) (*NameReply, error) { + + reply := &NameReply{} + reply.Message = string(GetRandomKey(fnl)) + + // switch request.GetNametype() { + // case NameType_FullName: + // var fullname []byte + // fullname = append(fullname, GetRandomKey(fnl)...) + // fullname = append(fullname, GetRandomKey(lnl)...) + // reply.Message = string(fullname) + + // case NameType_FirstName: + // reply.Message = string(GetRandomKey(fnl)) + // case NameType_LastName: + // reply.Message = string(GetRandomKey(lnl)) + // } + return reply, nil +} + +func (s *nameserver) LastName(cxt context.Context, request *NameRequest) (*NameReply, error) { + reply := &NameReply{} + reply.Message = string(GetRandomKey(lnl)) + return reply, nil +} + +func (s *nameserver) FullName(cxt context.Context, request *NameRequest) (*NameReply, error) { + reply := &NameReply{} + var fullname []byte + fullname = append(fullname, GetRandomKey(fnl)...) + fullname = append(fullname, GetRandomKey(lnl)...) + reply.Message = string(fullname) + return reply, nil +} diff --git a/people.pb.go b/people.pb.go new file mode 100644 index 0000000..5f20678 --- /dev/null +++ b/people.pb.go @@ -0,0 +1,279 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: people.proto + +package main + +import ( + context "context" + fmt "fmt" + proto "github.com/golang/protobuf/proto" + _ "google.golang.org/genproto/googleapis/api/annotations" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + math "math" +) + +// 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 + +// The request message containing the user's name. +type NameRequest struct { + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *NameRequest) Reset() { *m = NameRequest{} } +func (m *NameRequest) String() string { return proto.CompactTextString(m) } +func (*NameRequest) ProtoMessage() {} +func (*NameRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_09461903b56db210, []int{0} +} + +func (m *NameRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_NameRequest.Unmarshal(m, b) +} +func (m *NameRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_NameRequest.Marshal(b, m, deterministic) +} +func (m *NameRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_NameRequest.Merge(m, src) +} +func (m *NameRequest) XXX_Size() int { + return xxx_messageInfo_NameRequest.Size(m) +} +func (m *NameRequest) XXX_DiscardUnknown() { + xxx_messageInfo_NameRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_NameRequest proto.InternalMessageInfo + +// The response message containing the greetings +type NameReply struct { + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *NameReply) Reset() { *m = NameReply{} } +func (m *NameReply) String() string { return proto.CompactTextString(m) } +func (*NameReply) ProtoMessage() {} +func (*NameReply) Descriptor() ([]byte, []int) { + return fileDescriptor_09461903b56db210, []int{1} +} + +func (m *NameReply) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_NameReply.Unmarshal(m, b) +} +func (m *NameReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_NameReply.Marshal(b, m, deterministic) +} +func (m *NameReply) XXX_Merge(src proto.Message) { + xxx_messageInfo_NameReply.Merge(m, src) +} +func (m *NameReply) XXX_Size() int { + return xxx_messageInfo_NameReply.Size(m) +} +func (m *NameReply) XXX_DiscardUnknown() { + xxx_messageInfo_NameReply.DiscardUnknown(m) +} + +var xxx_messageInfo_NameReply proto.InternalMessageInfo + +func (m *NameReply) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func init() { + proto.RegisterType((*NameRequest)(nil), "main.NameRequest") + proto.RegisterType((*NameReply)(nil), "main.NameReply") +} + +func init() { + proto.RegisterFile("people.proto", fileDescriptor_09461903b56db210) +} + +var fileDescriptor_09461903b56db210 = []byte{ + // 220 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x48, 0xcd, 0x2f, + 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x93, 0x92, + 0x49, 0xcf, 0xcf, 0x4f, 0xcf, 0x49, 0xd5, 0x4f, 0x2c, 0xc8, 0xd4, 0x4f, 0xcc, 0xcb, 0xcb, 0x2f, + 0x49, 0x2c, 0xc9, 0xcc, 0xcf, 0x2b, 0x86, 0xa8, 0x51, 0xe2, 0xe5, 0xe2, 0xf6, 0x4b, 0xcc, 0x4d, + 0x0d, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x51, 0x52, 0xe5, 0xe2, 0x84, 0x70, 0x0b, 0x72, 0x2a, + 0x85, 0x24, 0xb8, 0xd8, 0x73, 0x53, 0x8b, 0x8b, 0x13, 0xd3, 0x53, 0x25, 0x18, 0x15, 0x18, 0x35, + 0x38, 0x83, 0x60, 0x5c, 0xa3, 0x4f, 0x8c, 0x5c, 0x2c, 0x20, 0x75, 0x42, 0x7e, 0x5c, 0x9c, 0x6e, + 0x99, 0x45, 0xc5, 0x25, 0x60, 0x8e, 0xa0, 0x1e, 0xc8, 0x42, 0x3d, 0x24, 0xf3, 0xa4, 0xf8, 0x91, + 0x85, 0x0a, 0x72, 0x2a, 0x95, 0x64, 0x9b, 0x2e, 0x3f, 0x99, 0xcc, 0x24, 0xae, 0x24, 0xa4, 0x5f, + 0x66, 0xa8, 0x9f, 0x97, 0x98, 0x9b, 0xaa, 0x9f, 0x06, 0xd2, 0x0f, 0x62, 0x59, 0x31, 0x6a, 0x09, + 0xf9, 0x70, 0x71, 0xf8, 0x24, 0x92, 0x60, 0x9c, 0x0c, 0xd8, 0x38, 0x31, 0x25, 0x41, 0xb8, 0x71, + 0x39, 0x89, 0x28, 0xa6, 0xb9, 0x95, 0xe6, 0xe4, 0x50, 0x60, 0x5a, 0x5a, 0x69, 0x4e, 0x0e, 0xd4, + 0x34, 0x27, 0x8e, 0x28, 0x36, 0x3d, 0x6b, 0x90, 0x8e, 0x24, 0x36, 0x70, 0xd8, 0x19, 0x03, 0x02, + 0x00, 0x00, 0xff, 0xff, 0x59, 0xe5, 0x6d, 0xa1, 0x6f, 0x01, 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// NameClient is the client API for Name service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type NameClient interface { + // Sends a greeting + FirstName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) + LastName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) + FullName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) +} + +type nameClient struct { + cc grpc.ClientConnInterface +} + +func NewNameClient(cc grpc.ClientConnInterface) NameClient { + return &nameClient{cc} +} + +func (c *nameClient) FirstName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) { + out := new(NameReply) + err := c.cc.Invoke(ctx, "/main.Name/FirstName", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nameClient) LastName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) { + out := new(NameReply) + err := c.cc.Invoke(ctx, "/main.Name/LastName", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nameClient) FullName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) { + out := new(NameReply) + err := c.cc.Invoke(ctx, "/main.Name/FullName", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// NameServer is the server API for Name service. +type NameServer interface { + // Sends a greeting + FirstName(context.Context, *NameRequest) (*NameReply, error) + LastName(context.Context, *NameRequest) (*NameReply, error) + FullName(context.Context, *NameRequest) (*NameReply, error) +} + +// UnimplementedNameServer can be embedded to have forward compatible implementations. +type UnimplementedNameServer struct { +} + +func (*UnimplementedNameServer) FirstName(ctx context.Context, req *NameRequest) (*NameReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method FirstName not implemented") +} +func (*UnimplementedNameServer) LastName(ctx context.Context, req *NameRequest) (*NameReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method LastName not implemented") +} +func (*UnimplementedNameServer) FullName(ctx context.Context, req *NameRequest) (*NameReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method FullName not implemented") +} + +func RegisterNameServer(s *grpc.Server, srv NameServer) { + s.RegisterService(&_Name_serviceDesc, srv) +} + +func _Name_FirstName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(NameRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NameServer).FirstName(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/main.Name/FirstName", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NameServer).FirstName(ctx, req.(*NameRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Name_LastName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(NameRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NameServer).LastName(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/main.Name/LastName", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NameServer).LastName(ctx, req.(*NameRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Name_FullName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(NameRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NameServer).FullName(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/main.Name/FullName", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NameServer).FullName(ctx, req.(*NameRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Name_serviceDesc = grpc.ServiceDesc{ + ServiceName: "main.Name", + HandlerType: (*NameServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "FirstName", + Handler: _Name_FirstName_Handler, + }, + { + MethodName: "LastName", + Handler: _Name_LastName_Handler, + }, + { + MethodName: "FullName", + Handler: _Name_FullName_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "people.proto", +} diff --git a/people.pb.gw.go b/people.pb.gw.go new file mode 100644 index 0000000..00760bf --- /dev/null +++ b/people.pb.gw.go @@ -0,0 +1,319 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: people.proto + +/* +Package main is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package main + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/descriptor" + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +// Suppress "imported and not used" errors +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray +var _ = descriptor.ForMessage + +func request_Name_FirstName_0(ctx context.Context, marshaler runtime.Marshaler, client NameClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq NameRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.FirstName(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Name_FirstName_0(ctx context.Context, marshaler runtime.Marshaler, server NameServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq NameRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.FirstName(ctx, &protoReq) + return msg, metadata, err + +} + +func request_Name_LastName_0(ctx context.Context, marshaler runtime.Marshaler, client NameClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq NameRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.LastName(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Name_LastName_0(ctx context.Context, marshaler runtime.Marshaler, server NameServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq NameRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.LastName(ctx, &protoReq) + return msg, metadata, err + +} + +func request_Name_FullName_0(ctx context.Context, marshaler runtime.Marshaler, client NameClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq NameRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.FullName(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Name_FullName_0(ctx context.Context, marshaler runtime.Marshaler, server NameServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq NameRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.FullName(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterNameHandlerServer registers the http handlers for service Name to "mux". +// UnaryRPC :call NameServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +func RegisterNameHandlerServer(ctx context.Context, mux *runtime.ServeMux, server NameServer) error { + + mux.Handle("POST", pattern_Name_FirstName_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Name_FirstName_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Name_FirstName_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("POST", pattern_Name_LastName_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Name_LastName_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Name_LastName_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("POST", pattern_Name_FullName_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Name_FullName_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Name_FullName_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterNameHandlerFromEndpoint is same as RegisterNameHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterNameHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterNameHandler(ctx, mux, conn) +} + +// RegisterNameHandler registers the http handlers for service Name to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterNameHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterNameHandlerClient(ctx, mux, NewNameClient(conn)) +} + +// RegisterNameHandlerClient registers the http handlers for service Name +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "NameClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "NameClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "NameClient" to call the correct interceptors. +func RegisterNameHandlerClient(ctx context.Context, mux *runtime.ServeMux, client NameClient) error { + + mux.Handle("POST", pattern_Name_FirstName_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Name_FirstName_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Name_FirstName_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("POST", pattern_Name_LastName_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Name_LastName_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Name_LastName_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("POST", pattern_Name_FullName_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Name_FullName_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Name_FullName_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Name_FirstName_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "name", "firstname"}, "", runtime.AssumeColonVerbOpt(true))) + + pattern_Name_LastName_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "name", "lastname"}, "", runtime.AssumeColonVerbOpt(true))) + + pattern_Name_FullName_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "name", "fullname"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_Name_FirstName_0 = runtime.ForwardResponseMessage + + forward_Name_LastName_0 = runtime.ForwardResponseMessage + + forward_Name_FullName_0 = runtime.ForwardResponseMessage +) diff --git a/proto/people.proto b/proto/people.proto new file mode 100644 index 0000000..7b7f7e0 --- /dev/null +++ b/proto/people.proto @@ -0,0 +1,48 @@ +syntax = "proto3"; + +package main; + +option go_package = ".;main"; + +import "google/api/annotations.proto"; + +// The greeting service definition. +service Name { + // Sends a greeting + rpc FirstName (NameRequest) returns (NameReply){ + option (google.api.http) = { + post: "/v1/name/firstname" + body: "*" + }; + } + + rpc LastName (NameRequest) returns (NameReply){ + option (google.api.http) = { + post: "/v1/name/lastname" + body: "*" + }; + } + + rpc FullName (NameRequest) returns (NameReply){ + option (google.api.http) = { + post: "/v1/name/fullname" + body: "*" + }; + } +} + +// The request message containing the user's name. +message NameRequest { + // NameType nametype = 1; +} + +// The response message containing the greetings +message NameReply { + string message = 1; +} + +// enum NameType { +// FullName = 0; +// FirstName = 1; +// LastName = 2 ; +// } \ No newline at end of file