Skip to content

Commit

Permalink
update vendor deps; move guts of code cmd/ tools in to app/
Browse files Browse the repository at this point in the history
  • Loading branch information
sfomuseumbot committed Dec 8, 2022
1 parent 6d1b85e commit 0af21c1
Show file tree
Hide file tree
Showing 592 changed files with 56,123 additions and 31,308 deletions.
100 changes: 100 additions & 0 deletions app/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package client

import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/sfomuseum/go-flags/flagset"
"github.com/sfomuseum/go-flags/lookup"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/request"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/spatial"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
"google.golang.org/grpc"
"io"
"log"
"os"
)

func Run(ctx context.Context, logger *log.Logger) error {

fs, err := DefaultFlagSet()

if err != nil {
return fmt.Errorf("Failed to create default flagset, %w", err)
}

return RunWithFlagSet(ctx, fs, logger)
}

func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet, logger *log.Logger) error {

flagset.Parse(fs)

err := grpc_flags.ValidateGRPCClientFlags(fs)

if err != nil {
return fmt.Errorf("Failed to validate grpc client flags, %w", err)
}

err = spatial_flags.ValidateQueryFlags(fs)

if err != nil {
return fmt.Errorf("Failed to validate query flags, %w", err)
}

req, err := request.NewPointInPolygonRequestFromFlagSet(fs)

if err != nil {
return fmt.Errorf("Failed to create PIP request, %v", err)
}

host, _ := lookup.StringVar(fs, grpc_flags.HOST)
port, _ := lookup.IntVar(fs, grpc_flags.PORT)

var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())

addr := fmt.Sprintf("%s:%d", host, port)

conn, err := grpc.Dial(addr, opts...)

if err != nil {
return fmt.Errorf("Failed to dial '%s', %v", addr, err)
}

defer conn.Close()

client := spatial.NewSpatialClient(conn)

stream, err := client.PointInPolygon(ctx, req)

if err != nil {
return fmt.Errorf("Failed to perform point in polygon operation, %w", err)
}

to_stdout, _ := lookup.BoolVar(fs, grpc_flags.TO_STDOUT)
to_null, _ := lookup.BoolVar(fs, grpc_flags.TO_NULL)

writers := make([]io.Writer, 0)

if to_stdout {
writers = append(writers, os.Stdout)
}

if to_null {
writers = append(writers, io.Discard)
}

wr := io.MultiWriter(writers...)

enc := json.NewEncoder(wr)
err = enc.Encode(stream)

if err != nil {
return fmt.Errorf("Failed to encode stream, %v", err)
}

return nil
}
28 changes: 28 additions & 0 deletions app/client/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

import (
"flag"
"fmt"
"github.com/sfomuseum/go-flags/flagset"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
)

func DefaultFlagSet() (*flag.FlagSet, error) {

fs := flagset.NewFlagSet("client")

err := grpc_flags.AppendGRPCClientFlags(fs)

if err != nil {
return nil, fmt.Errorf("Failed to append grpc client flags, %w", err)
}

err = spatial_flags.AppendQueryFlags(fs)

if err != nil {
return nil, fmt.Errorf("Failed to append query flags, %w", err)
}

return fs, nil
}
31 changes: 31 additions & 0 deletions app/server/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package server

import (
"flag"
"fmt"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
)

func DefaultFlagSet() (*flag.FlagSet, error) {

fs, err := spatial_flags.CommonFlags()

if err != nil {
return nil, fmt.Errorf("Failed to create common flags, %v", err)
}

err = spatial_flags.AppendIndexingFlags(fs)

if err != nil {
return nil, fmt.Errorf("Failed to append indexing flags, %v", err)
}

err = grpc_flags.AppendGRPCServerFlags(fs)

if err != nil {
return nil, fmt.Errorf("Failed to append server flags, %v", err)
}

return fs, nil
}
35 changes: 8 additions & 27 deletions server/app.go → app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/sfomuseum/go-flags/flagset"
"github.com/sfomuseum/go-flags/lookup"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"
grpc_server "github.com/whosonfirst/go-whosonfirst-spatial-grpc/server"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/spatial"
spatial_app "github.com/whosonfirst/go-whosonfirst-spatial/app"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
Expand All @@ -15,41 +16,21 @@ import (
"net"
)

type ServerApplication struct{}
func Run(ctx context.Context, logger *log.Logger) error {

func NewServerApplication() (*ServerApplication, error) {

server_app := &ServerApplication{}
return server_app, nil
}

func (server_app *ServerApplication) Run(ctx context.Context) error {

fs, err := spatial_flags.CommonFlags()

if err != nil {
return fmt.Errorf("Failed to create common flags, %v", err)
}

err = spatial_flags.AppendIndexingFlags(fs)
fs, err := DefaultFlagSet()

if err != nil {
return fmt.Errorf("Failed to append indexing flags, %v", err)
return fmt.Errorf("Failed to derive default flag set, %w", err)
}

err = grpc_flags.AppendGRPCServerFlags(fs)
return RunWithFlagSet(ctx, fs, logger)
}

if err != nil {
return fmt.Errorf("Failed to append server flags, %v", err)
}
func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet, logger *log.Logger) error {

flagset.Parse(fs)

return server_app.RunWithFlagSet(ctx, fs)
}

func (server_app *ServerApplication) RunWithFlagSet(ctx context.Context, fs *flag.FlagSet) error {

err := spatial_flags.ValidateCommonFlags(fs)

if err != nil {
Expand Down Expand Up @@ -85,7 +66,7 @@ func (server_app *ServerApplication) RunWithFlagSet(ctx context.Context, fs *fla
return fmt.Errorf("Failed to index paths, %v", err)
}

spatial_server, err := NewSpatialServer(sp_app)
spatial_server, err := grpc_server.NewSpatialServer(sp_app)

if err != nil {
return fmt.Errorf("Failed to create spatial server, %v", err)
Expand Down
91 changes: 4 additions & 87 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,18 @@ package main

import (
"context"
"encoding/json"
"fmt"
"github.com/sfomuseum/go-flags/flagset"
"github.com/sfomuseum/go-flags/lookup"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/request"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/spatial"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
"google.golang.org/grpc"
"io"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/app/client"
"log"
"os"
)

func main() {

fs := flagset.NewFlagSet("client")

err := grpc_flags.AppendGRPCClientFlags(fs)

if err != nil {
log.Fatal(err)
}

err = spatial_flags.AppendQueryFlags(fs)

if err != nil {
log.Fatal(err)
}

flagset.Parse(fs)

ctx := context.Background()
logger := log.Default()

err = grpc_flags.ValidateGRPCClientFlags(fs)

if err != nil {
log.Fatal(err)
}

err = spatial_flags.ValidateQueryFlags(fs)

if err != nil {
log.Fatal(err)
}

req, err := request.NewPointInPolygonRequestFromFlagSet(fs)

if err != nil {
log.Fatalf("Failed to create PIP request, %v", err)
}

host, _ := lookup.StringVar(fs, grpc_flags.HOST)
port, _ := lookup.IntVar(fs, grpc_flags.PORT)

var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())

addr := fmt.Sprintf("%s:%d", host, port)

conn, err := grpc.Dial(addr, opts...)

if err != nil {
log.Fatalf("fail to dial '%s', %v", addr, err)
}

defer conn.Close()

client := spatial.NewSpatialClient(conn)

stream, err := client.PointInPolygon(ctx, req)

if err != nil {
log.Fatal(err)
}

to_stdout, _ := lookup.BoolVar(fs, grpc_flags.TO_STDOUT)
to_null, _ := lookup.BoolVar(fs, grpc_flags.TO_NULL)

writers := make([]io.Writer, 0)

if to_stdout {
writers = append(writers, os.Stdout)
}

if to_null {
writers = append(writers, io.Discard)
}

wr := io.MultiWriter(writers...)

enc := json.NewEncoder(wr)
err = enc.Encode(stream)
err := client.Run(ctx, logger)

if err != nil {
log.Fatalf("Failed to encode stream, %v", err)
logger.Fatalf("Failed to run client, %v", err)
}
}
17 changes: 4 additions & 13 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package main

import (
_ "github.com/whosonfirst/go-whosonfirst-spatial-rtree"
)

import (
"context"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/server"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/app/server"
"log"
)

func main() {

ctx := context.Background()
logger := log.Default()

app, err := server.NewServerApplication()

if err != nil {
log.Fatalf("Failed to create new server application, %v", err)
}

err = app.Run(ctx)
err := server.Run(ctx, logger)

if err != nil {
log.Fatalf("Failed to run server application, %v", err)
logger.Fatalf("Failed to run client, %v", err)
}
}
Loading

0 comments on commit 0af21c1

Please sign in to comment.