add building way interface

This commit is contained in:
huangsimin 2020-05-22 15:39:43 +08:00
parent 68feb03024
commit 41fd202470
16 changed files with 802 additions and 31 deletions

21
building.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
context "context"
)
var buildinglist = &KeyList{}
func init() {
LoadGob("./data/building.gob", buildinglist)
}
// UnimplementedBuildingServer can be embedded to have forward compatible implementations.
type buildingserver struct {
}
func (bs *buildingserver) Name(ctx context.Context, req *Request) (*Reply, error) {
reply := &Reply{}
reply.Message = string(GetRandomKey(buildinglist).([]byte))
return reply, nil
}

126
building.pb.go Normal file
View File

@ -0,0 +1,126 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: building.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
func init() {
proto.RegisterFile("building.proto", fileDescriptor_05bb3d959b9f4d76)
}
var fileDescriptor_05bb3d959b9f4d76 = []byte{
// 152 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4b, 0x2a, 0xcd, 0xcc,
0x49, 0xc9, 0xcc, 0x4b, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc,
0x93, 0xe2, 0x4a, 0x4a, 0x2c, 0x4e, 0x85, 0x88, 0x48, 0xc9, 0xa4, 0xe7, 0xe7, 0xa7, 0xe7, 0xa4,
0xea, 0x27, 0x16, 0x64, 0xea, 0x27, 0xe6, 0xe5, 0xe5, 0x97, 0x24, 0x96, 0x64, 0xe6, 0xe7, 0x15,
0x43, 0x64, 0x8d, 0x7c, 0xb8, 0x38, 0x9c, 0xa0, 0x26, 0x08, 0x39, 0x70, 0xb1, 0xf8, 0x25, 0xe6,
0xa6, 0x0a, 0xf1, 0xea, 0x81, 0x0c, 0xd1, 0x0b, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x91, 0xe2,
0x86, 0x71, 0x0b, 0x72, 0x2a, 0x95, 0x64, 0x9a, 0x2e, 0x3f, 0x99, 0xcc, 0x24, 0xa6, 0x24, 0xa8,
0x5f, 0x66, 0xa8, 0x0f, 0xb3, 0x5b, 0x3f, 0x2f, 0x31, 0x37, 0xd5, 0x8a, 0x51, 0xcb, 0x89, 0x23,
0x8a, 0x4d, 0xcf, 0x1a, 0xa4, 0x3a, 0x89, 0x0d, 0x6c, 0xbc, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff,
0xb2, 0x4d, 0xb2, 0x8a, 0xa0, 0x00, 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
// BuildingClient is the client API for Building service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type BuildingClient interface {
// Sends a greeting
Name(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error)
}
type buildingClient struct {
cc grpc.ClientConnInterface
}
func NewBuildingClient(cc grpc.ClientConnInterface) BuildingClient {
return &buildingClient{cc}
}
func (c *buildingClient) Name(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) {
out := new(Reply)
err := c.cc.Invoke(ctx, "/main.Building/Name", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// BuildingServer is the server API for Building service.
type BuildingServer interface {
// Sends a greeting
Name(context.Context, *Request) (*Reply, error)
}
// UnimplementedBuildingServer can be embedded to have forward compatible implementations.
type UnimplementedBuildingServer struct {
}
func (*UnimplementedBuildingServer) Name(ctx context.Context, req *Request) (*Reply, error) {
return nil, status.Errorf(codes.Unimplemented, "method Name not implemented")
}
func RegisterBuildingServer(s *grpc.Server, srv BuildingServer) {
s.RegisterService(&_Building_serviceDesc, srv)
}
func _Building_Name_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Request)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(BuildingServer).Name(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/main.Building/Name",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(BuildingServer).Name(ctx, req.(*Request))
}
return interceptor(ctx, in, info, handler)
}
var _Building_serviceDesc = grpc.ServiceDesc{
ServiceName: "main.Building",
HandlerType: (*BuildingServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Name",
Handler: _Building_Name_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "building.proto",
}

163
building.pb.gw.go Normal file
View File

@ -0,0 +1,163 @@
// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT.
// source: building.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_Building_Name_0(ctx context.Context, marshaler runtime.Marshaler, client BuildingClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq Request
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.Name(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_Building_Name_0(ctx context.Context, marshaler runtime.Marshaler, server BuildingServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq Request
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.Name(ctx, &protoReq)
return msg, metadata, err
}
// RegisterBuildingHandlerServer registers the http handlers for service Building to "mux".
// UnaryRPC :call BuildingServer directly.
// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906.
func RegisterBuildingHandlerServer(ctx context.Context, mux *runtime.ServeMux, server BuildingServer) error {
mux.Handle("POST", pattern_Building_Name_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_Building_Name_0(rctx, inboundMarshaler, server, req, pathParams)
ctx = runtime.NewServerMetadataContext(ctx, md)
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
forward_Building_Name_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
// RegisterBuildingHandlerFromEndpoint is same as RegisterBuildingHandler but
// automatically dials to "endpoint" and closes the connection when "ctx" gets done.
func RegisterBuildingHandlerFromEndpoint(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 RegisterBuildingHandler(ctx, mux, conn)
}
// RegisterBuildingHandler registers the http handlers for service Building to "mux".
// The handlers forward requests to the grpc endpoint over "conn".
func RegisterBuildingHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return RegisterBuildingHandlerClient(ctx, mux, NewBuildingClient(conn))
}
// RegisterBuildingHandlerClient registers the http handlers for service Building
// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "BuildingClient".
// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "BuildingClient"
// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in
// "BuildingClient" to call the correct interceptors.
func RegisterBuildingHandlerClient(ctx context.Context, mux *runtime.ServeMux, client BuildingClient) error {
mux.Handle("POST", pattern_Building_Name_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_Building_Name_0(rctx, inboundMarshaler, client, req, pathParams)
ctx = runtime.NewServerMetadataContext(ctx, md)
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
forward_Building_Name_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
var (
pattern_Building_Name_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "building", "name"}, "", runtime.AssumeColonVerbOpt(true)))
)
var (
forward_Building_Name_0 = runtime.ForwardResponseMessage
)

43
building_test.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"io/ioutil"
"os"
"regexp"
"testing"
)
func TestMakeBuildingGob(t *testing.T) {
f, err := os.Open("/home/eson/test/spider_xingming/building_simple.txt")
if err != nil {
panic(err)
}
// reader := bufio.NewReader(f)
var alldict map[string]bool = make(map[string]bool)
all, _ := ioutil.ReadAll(f)
kl := &KeyList{}
for _, line := range regexp.MustCompile(`[^\s]+`).FindAll(all, -1) {
// kl.AppendKey(line)
alldict[string(line)] = true
}
for k := range alldict {
kl.AppendKey([]byte(k))
}
// i := 0
// for ; ; i++ {
// if err != nil {
// break
// }
// if len(line) <= 2 {
// continue
// }
// //t.Error(string(line))
// kl.AppendKey(line)
// }
// t.Error(i)
SaveData("./data/building.gob", kl)
}

View File

@ -4,7 +4,17 @@ import (
"testing"
)
func estRemakeGob(t *testing.T) {
province := &KeyList{}
LoadGob("./data/province.gob", province)
func testRemakeGob(t *testing.T) {
country := &KeyList{}
LoadGob("./data/country-fix.gob", country)
// for i, ikey := range country.Keys {
// c := ikey.(Country)
// name := []rune(string(c.Name))
// iname := name[0 : len(name)-2]
// t.Error(string(name), string(iname))
// c.Name = []byte(string(iname))
// country.Keys[i] = c
// }
// SaveData("./data/country-fix.gob", country)
t.Error(country.Keys...)
}

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
github.com/golang/protobuf v1.4.1
github.com/grpc-ecosystem/grpc-gateway v1.14.5
github.com/qedus/osmpbf v1.1.0
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

2
go.sum
View File

@ -82,6 +82,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
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/qedus/osmpbf v1.1.0 h1:1ewnhb7cX0VAp24M+ViDvLI9RKKgZOXFBLM5xGlB5TA=
github.com/qedus/osmpbf v1.1.0/go.mod h1:37EgzlwZC2inPP5/rY1MZIxE6kgDof7MIljJuELs0c0=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

12
main.go
View File

@ -37,13 +37,11 @@ func main() {
// gserver := grpc.NewServer()
mux := runtime.NewServeMux()
ns := &nameserver{}
cs := &countryserver{}
ps := &provinceserver{}
RegisterNameHandlerServer(ctx, mux, ns)
RegisterCountryHandlerServer(ctx, mux, cs)
RegisterProvinceAreaCityHandlerServer(ctx, mux, ps)
RegisterNameHandlerServer(ctx, mux, &nameserver{})
RegisterCountryHandlerServer(ctx, mux, &countryserver{})
RegisterProvinceAreaCityHandlerServer(ctx, mux, &provinceserver{})
RegisterWayHandlerServer(ctx, mux, &wayserver{})
RegisterBuildingHandlerServer(ctx, mux, &buildingserver{})
log.Fatal(http.ListenAndServe(":4433", mux))

20
proto/building.proto Normal file
View File

@ -0,0 +1,20 @@
syntax = "proto3";
package main;
option go_package = ".;main";
import "base.proto";
import "google/api/annotations.proto";
// The greeting service definition.
service Building {
// Sends a greeting
rpc Name (Request) returns (Reply){
option (google.api.http) = {
post: "/v1/building/name"
body: "*"
};
}
}

View File

@ -4,10 +4,8 @@ package main;
option go_package = ".;main";
import "base.proto";
import "google/api/annotations.proto";
import "base.proto";
// The greeting service definition.
service ProvinceAreaCity {

18
proto/way.proto Normal file
View File

@ -0,0 +1,18 @@
syntax = "proto3";
package main;
option go_package = ".;main";
import "google/api/annotations.proto";
import "base.proto";
// The greeting service definition.
service Way {
rpc Name (Request) returns (Reply){
option (google.api.http) = {
post: "/v1/way/name"
body: "*"
};
}
}

View File

@ -142,24 +142,24 @@ var fileDescriptor_e183bc545e5b5a19 = []byte{
// 313 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x28, 0xca, 0x2f,
0xcb, 0xcc, 0x4b, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc,
0x93, 0xe2, 0x4a, 0x4a, 0x2c, 0x86, 0x8a, 0x48, 0xc9, 0xa4, 0xe7, 0xe7, 0xa7, 0xe7, 0xa4, 0xea,
0x27, 0x16, 0x64, 0xea, 0x27, 0xe6, 0xe5, 0xe5, 0x97, 0x24, 0x96, 0x64, 0xe6, 0xe7, 0x15, 0x43,
0x64, 0x95, 0x0a, 0xb8, 0x84, 0xfd, 0x12, 0x73, 0x53, 0x9d, 0xf3, 0x53, 0x52, 0x03, 0x12, 0x8b,
0x52, 0xf3, 0x4a, 0x82, 0x52, 0x0b, 0x72, 0x2a, 0x85, 0x84, 0xb8, 0x58, 0xf2, 0x12, 0x73, 0x53,
0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xc0, 0x6c, 0x90, 0x58, 0x72, 0x7e, 0x4a, 0xaa, 0x04,
0x13, 0x44, 0x0c, 0xc4, 0x16, 0x32, 0xe4, 0x62, 0x2b, 0x00, 0x6b, 0x93, 0x60, 0x56, 0x60, 0xd4,
0xe0, 0x36, 0x92, 0xd4, 0x03, 0xd9, 0xaf, 0x87, 0xc5, 0xc8, 0x20, 0xa8, 0x42, 0x25, 0x73, 0x2e,
0x5e, 0x98, 0x34, 0x49, 0x76, 0x19, 0x6d, 0x62, 0xe6, 0x12, 0x08, 0x80, 0xfa, 0xd6, 0xb1, 0x28,
0x35, 0xd1, 0x39, 0xb3, 0xa4, 0x52, 0x28, 0x80, 0x8b, 0x03, 0x26, 0x26, 0xc4, 0x0b, 0xb1, 0x3c,
0x28, 0xb5, 0xb0, 0x34, 0xb5, 0xb8, 0x44, 0x4a, 0x18, 0xd5, 0x2d, 0x60, 0xcb, 0x94, 0x14, 0x9a,
0x2e, 0x3f, 0x99, 0xcc, 0x24, 0xa5, 0x24, 0xaa, 0x5f, 0x66, 0xa8, 0x0f, 0x0b, 0x3b, 0x7d, 0x90,
0x9d, 0x20, 0x3b, 0xac, 0x18, 0xb5, 0x84, 0x3c, 0xb8, 0x58, 0x40, 0xa6, 0x13, 0x65, 0x9a, 0x0c,
0xd8, 0x34, 0x31, 0x25, 0x41, 0x90, 0x69, 0x89, 0x45, 0xa9, 0x89, 0x28, 0x26, 0x45, 0x73, 0x71,
0x81, 0x4c, 0x82, 0x04, 0x02, 0xba, 0x79, 0xb8, 0x43, 0x4a, 0x49, 0x19, 0x6c, 0xaa, 0xac, 0x92,
0x04, 0x86, 0xa9, 0xfa, 0x90, 0x30, 0x84, 0x3a, 0x13, 0x1c, 0x00, 0x24, 0x3b, 0x33, 0x39, 0xb3,
0xa4, 0x12, 0xdd, 0x99, 0x20, 0x93, 0x28, 0x73, 0x26, 0x8a, 0xa9, 0x08, 0x67, 0x3a, 0x71, 0x44,
0xb1, 0xe9, 0x59, 0x83, 0x8c, 0x48, 0x62, 0x03, 0x27, 0x38, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff,
0xff, 0x51, 0x87, 0x58, 0x64, 0xb2, 0x02, 0x00, 0x00,
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, 0x91, 0xe2, 0x4a, 0x4a, 0x2c, 0x86,
0xaa, 0x57, 0x2a, 0xe0, 0x12, 0xf6, 0x4b, 0xcc, 0x4d, 0x75, 0xce, 0x4f, 0x49, 0x0d, 0x48, 0x2c,
0x4a, 0xcd, 0x2b, 0x09, 0x4a, 0x2d, 0xc8, 0xa9, 0x14, 0x12, 0xe2, 0x62, 0xc9, 0x4b, 0xcc, 0x4d,
0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x41, 0x62, 0xc9, 0xf9, 0x29, 0xa9, 0x12,
0x4c, 0x10, 0x31, 0x10, 0x5b, 0xc8, 0x90, 0x8b, 0xad, 0x00, 0xac, 0x4d, 0x82, 0x59, 0x81, 0x51,
0x83, 0xdb, 0x48, 0x52, 0x0f, 0x64, 0xbf, 0x1e, 0x16, 0x23, 0x83, 0xa0, 0x0a, 0x95, 0xcc, 0xb9,
0x78, 0x61, 0xd2, 0x24, 0xd9, 0x65, 0xb4, 0x89, 0x99, 0x4b, 0x20, 0x00, 0xea, 0x5b, 0xc7, 0xa2,
0xd4, 0x44, 0xe7, 0xcc, 0x92, 0x4a, 0xa1, 0x00, 0x2e, 0x0e, 0x98, 0x98, 0x10, 0x2f, 0xc4, 0xf2,
0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x29, 0x61, 0x54, 0xb7, 0x80, 0x2d, 0x53, 0x52, 0x68,
0xba, 0xfc, 0x64, 0x32, 0x93, 0x94, 0x92, 0xa8, 0x7e, 0x99, 0xa1, 0x3e, 0x2c, 0xec, 0xf4, 0x41,
0x76, 0x82, 0xec, 0xb0, 0x62, 0xd4, 0x12, 0xf2, 0xe0, 0x62, 0x01, 0x99, 0x4e, 0x94, 0x69, 0x32,
0x60, 0xd3, 0xc4, 0x94, 0x04, 0x41, 0xa6, 0x25, 0x16, 0xa5, 0x26, 0xa2, 0x98, 0x14, 0xcd, 0xc5,
0x05, 0x32, 0x09, 0x12, 0x08, 0xe8, 0xe6, 0xe1, 0x0e, 0x29, 0x25, 0x65, 0xb0, 0xa9, 0xb2, 0x4a,
0x12, 0x18, 0xa6, 0xea, 0x43, 0xc2, 0x10, 0xea, 0x4c, 0x70, 0x00, 0x90, 0xec, 0xcc, 0xe4, 0xcc,
0x92, 0x4a, 0x74, 0x67, 0x82, 0x4c, 0xa2, 0xcc, 0x99, 0x28, 0xa6, 0x22, 0x9c, 0xe9, 0xc4, 0x11,
0xc5, 0xa6, 0x67, 0x0d, 0x32, 0x22, 0x89, 0x0d, 0x9c, 0xe0, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff,
0xff, 0x13, 0xfb, 0x70, 0x4d, 0xb2, 0x02, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.

21
way.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
context "context"
)
var ways = &KeyList{}
func init() {
LoadGob("./data/ways.gob", ways)
}
// wayserver can be embedded to have forward compatible implementations.
type wayserver struct {
}
func (ws *wayserver) Name(ctx context.Context, req *Request) (*Reply, error) {
reply := &Reply{}
reply.Message = GetRandomKey(ways).(string)
return reply, nil
}

124
way.pb.go Normal file
View File

@ -0,0 +1,124 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: way.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
func init() {
proto.RegisterFile("way.proto", fileDescriptor_8313664ff05de314)
}
var fileDescriptor_8313664ff05de314 = []byte{
// 148 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2c, 0x4f, 0xac, 0xd4,
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, 0x91, 0xe2, 0x4a, 0x4a, 0x2c, 0x4e, 0x85, 0xb0, 0x8d, 0x9c, 0xb8,
0x98, 0xc3, 0x13, 0x2b, 0x85, 0xac, 0xb9, 0x58, 0xfc, 0x12, 0x73, 0x53, 0x85, 0x78, 0xf5, 0x40,
0xfa, 0xf5, 0x82, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0xa4, 0xb8, 0x61, 0xdc, 0x82, 0x9c, 0x4a,
0x25, 0xf1, 0xa6, 0xcb, 0x4f, 0x26, 0x33, 0x09, 0x2a, 0xf1, 0xe8, 0x97, 0x19, 0xea, 0x97, 0x27,
0x56, 0xea, 0xe7, 0x25, 0xe6, 0xa6, 0x5a, 0x31, 0x6a, 0x39, 0x71, 0x44, 0xb1, 0xe9, 0x59, 0x83,
0x14, 0x26, 0xb1, 0x81, 0x0d, 0x35, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x21, 0x70, 0x64, 0x1b,
0x91, 0x00, 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
// WayClient is the client API for Way service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type WayClient interface {
Name(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error)
}
type wayClient struct {
cc grpc.ClientConnInterface
}
func NewWayClient(cc grpc.ClientConnInterface) WayClient {
return &wayClient{cc}
}
func (c *wayClient) Name(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) {
out := new(Reply)
err := c.cc.Invoke(ctx, "/main.Way/Name", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// WayServer is the server API for Way service.
type WayServer interface {
Name(context.Context, *Request) (*Reply, error)
}
// UnimplementedWayServer can be embedded to have forward compatible implementations.
type UnimplementedWayServer struct {
}
func (*UnimplementedWayServer) Name(ctx context.Context, req *Request) (*Reply, error) {
return nil, status.Errorf(codes.Unimplemented, "method Name not implemented")
}
func RegisterWayServer(s *grpc.Server, srv WayServer) {
s.RegisterService(&_Way_serviceDesc, srv)
}
func _Way_Name_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Request)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(WayServer).Name(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/main.Way/Name",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(WayServer).Name(ctx, req.(*Request))
}
return interceptor(ctx, in, info, handler)
}
var _Way_serviceDesc = grpc.ServiceDesc{
ServiceName: "main.Way",
HandlerType: (*WayServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Name",
Handler: _Way_Name_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "way.proto",
}

163
way.pb.gw.go Normal file
View File

@ -0,0 +1,163 @@
// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT.
// source: way.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_Way_Name_0(ctx context.Context, marshaler runtime.Marshaler, client WayClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq Request
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.Name(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_Way_Name_0(ctx context.Context, marshaler runtime.Marshaler, server WayServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq Request
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.Name(ctx, &protoReq)
return msg, metadata, err
}
// RegisterWayHandlerServer registers the http handlers for service Way to "mux".
// UnaryRPC :call WayServer directly.
// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906.
func RegisterWayHandlerServer(ctx context.Context, mux *runtime.ServeMux, server WayServer) error {
mux.Handle("POST", pattern_Way_Name_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_Way_Name_0(rctx, inboundMarshaler, server, req, pathParams)
ctx = runtime.NewServerMetadataContext(ctx, md)
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
forward_Way_Name_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
// RegisterWayHandlerFromEndpoint is same as RegisterWayHandler but
// automatically dials to "endpoint" and closes the connection when "ctx" gets done.
func RegisterWayHandlerFromEndpoint(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 RegisterWayHandler(ctx, mux, conn)
}
// RegisterWayHandler registers the http handlers for service Way to "mux".
// The handlers forward requests to the grpc endpoint over "conn".
func RegisterWayHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return RegisterWayHandlerClient(ctx, mux, NewWayClient(conn))
}
// RegisterWayHandlerClient registers the http handlers for service Way
// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "WayClient".
// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "WayClient"
// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in
// "WayClient" to call the correct interceptors.
func RegisterWayHandlerClient(ctx context.Context, mux *runtime.ServeMux, client WayClient) error {
mux.Handle("POST", pattern_Way_Name_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_Way_Name_0(rctx, inboundMarshaler, client, req, pathParams)
ctx = runtime.NewServerMetadataContext(ctx, md)
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
forward_Way_Name_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
var (
pattern_Way_Name_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "way", "name"}, "", runtime.AssumeColonVerbOpt(true)))
)
var (
forward_Way_Name_0 = runtime.ForwardResponseMessage
)

63
way_test.go Normal file
View File

@ -0,0 +1,63 @@
package main
import (
"io"
"log"
"os"
"runtime"
"testing"
"github.com/qedus/osmpbf"
)
func testPBF(t *testing.T) {
f, err := os.Open("/home/eson/tools/china-latest.osm.pbf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
d := osmpbf.NewDecoder(f)
// use more memory from the start, it is faster
d.SetBufferSize(osmpbf.MaxBlobSize)
// start decoding with several goroutines, it is faster
err = d.Start(runtime.GOMAXPROCS(-1))
if err != nil {
t.Fatal(err)
}
ways := &KeyList{}
var nc, wc, rc uint64
for {
if v, err := d.Decode(); err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
} else {
switch v := v.(type) {
case *osmpbf.Node:
// Process Node v.
case *osmpbf.Way:
// Process Way v.
// i++
if name, ok := v.Tags["name:zh"]; ok {
if _, ok := v.Tags["highway"]; ok {
wc++
ways.AppendKey(name)
}
}
case *osmpbf.Relation:
// Process Relation v.
default:
t.Fatalf("unknown type %T\n", v)
}
}
}
SaveData("./data/ways.gob", ways)
t.Errorf("Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)
}