Permalink
Browse files

initial import

  • Loading branch information...
peter-edge committed Oct 5, 2015
0 parents commit 713ae140fd49f1c8b3fa2387403954a3ddaa314b
Showing with 84,576 additions and 0 deletions.
  1. +1 −0 .dockerignore
  2. +9 −0 Dockerfile.flights-dev
  3. +5 −0 Dockerfile.flightsd
  4. +22 −0 LICENSE
  5. +92 −0 Makefile
  6. +1 −0 README.md
  7. +132 −0 api_server.go
  8. +129 −0 client.go
  9. +50 −0 cmd/flightsd/main.go
  10. +69 −0 cmd/gen-flights-csv-store/main.go
  11. +103 −0 cmd/gen-flights-id-store/main.go
  12. +57 −0 cmd/internal/common/common.go
  13. +121 −0 code_store.go
  14. +40 −0 csv_store.go
  15. +87 −0 flights.go
  16. +627 −0 flights.pb.go
  17. +482 −0 flights.pb.gw.go
  18. +180 −0 flights.proto
  19. +85 −0 flights_test.go
  20. +81,833 −0 generated.go
  21. +277 −0 id_store.go
  22. +59 −0 local_api_client.go
  23. +13 −0 server_client.go
  24. +102 −0 util.go
@@ -0,0 +1 @@
.git
@@ -0,0 +1,9 @@
FROM golang:1.5.1
MAINTAINER peter.edge@gmail.com

RUN \
curl -sSL https://get.docker.com/builds/Linux/x86_64/docker-1.8.2 > /bin/docker && \
chmod +x /bin/docker
RUN mkdir -p /go/src/go.pedge.io/flights
ADD . /go/src/go.pedge.io/flights/
WORKDIR /go/src/go.pedge.io/flights
@@ -0,0 +1,5 @@
FROM scratch
MAINTAINER peter.edge@gmail.com

ADD _tmp/flightsd /
ENTRYPOINT ["/flightsd"]
22 LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Peter Edge

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@@ -0,0 +1,92 @@
.PHONY: \
all \
deps \
updatedeps \
testdeps \
updatetestdeps \
build \
install \
lint \
vet \
errcheck \
pretest \
test \
clean \
proto \
generate \
docker-build-flights-dev \
docker-build-flightsd-internal \
docker-build-flightsd \
launch

all: test

deps:
go get -d -v ./...

updatedeps:
go get -d -v -u -f ./...

testdeps:
go get -d -v -t ./...

updatetestdeps:
go get -d -v -t -u -f ./...

build: deps
go build ./...

install: deps
go install ./...

lint: testdeps
go get -v github.com/golang/lint/golint
for file in $$(find . -name '*.go' | grep -v '\.pb\.go' | grep -v '\.pb\.gw\.go' | grep -v '\.pb.log\.go'); do \
golint $${file}; \
if [ -n "$$(golint $${file})" ]; then \
exit 1; \
fi; \
done

vet: testdeps
go vet ./...

errcheck: testdeps
go get -v github.com/kisielk/errcheck
errcheck ./...

pretest: lint

test: testdeps pretest
go test ./...

clean:
go clean -i ./...

proto:
go get -v go.pedge.io/tools/protoc-all
STRIP_PACKAGE_COMMENTS=1 protoc-all go.pedge.io/flights

generate:
go run cmd/gen-flights-csv-store/main.go flights generated.go

docker-build-flights-dev:
docker build -t pedge/flights-dev -f Dockerfile.flights-dev .

docker-build-flightsd-internal: deps
rm -rf _tmp
mkdir -p _tmp
go build \
-a \
-installsuffix netgo \
-tags netgo \
-ldflags '-w -linkmode external -extldflags "-static"' \
-o _tmp/flightsd \
cmd/flightsd/main.go
docker build -t pedge/flightsd -f Dockerfile.flightsd .

docker-build-flightsd: docker-build-flights-dev
docker run -v /var/run/docker.sock:/var/run/docker.sock pedge/flights-dev make docker-build-flightsd-internal

launch: docker-build-flightsd
docker run -d -p 1747:1747 -p 8080:8080 pedge/flightsd
@@ -0,0 +1 @@
# flights
@@ -0,0 +1,132 @@
package flights

import (
"time"

"github.com/gogo/protobuf/proto"
"go.pedge.io/google-protobuf"
"go.pedge.io/proto/rpclog"
"golang.org/x/net/context"
)

var (
emptyInstance = &google_protobuf.Empty{}
)

type apiServer struct {
client Client
}

func newAPIServer(client Client) *apiServer {
return &apiServer{client}
}

func (a *apiServer) GetAllAirports(_ context.Context, request *google_protobuf.Empty) (response *Airports, err error) {
start := time.Now()
defer func() { a.log("GetAllAirports", request, response, err, start) }()
airports, err := a.client.GetAllAirports()
if err != nil {
return nil, err
}
return &Airports{
Airport: airports,
}, nil
}

func (a *apiServer) GetAllAirlines(_ context.Context, request *google_protobuf.Empty) (response *Airlines, err error) {
start := time.Now()
defer func() { a.log("GetAllAirlines", request, response, err, start) }()
airlines, err := a.client.GetAllAirlines()
if err != nil {
return nil, err
}
return &Airlines{
Airline: airlines,
}, nil
}

func (a *apiServer) GetAllRoutes(_ context.Context, request *google_protobuf.Empty) (response *Routes, err error) {
start := time.Now()
defer func() { a.log("GetAllRoutes", request, response, err, start) }()
routes, err := a.client.GetAllRoutes()
if err != nil {
return nil, err
}
return &Routes{
Route: routes,
}, nil
}

func (a *apiServer) GetAirportByID(_ context.Context, request *GetAirportByIDRequest) (response *Airport, err error) {
start := time.Now()
defer func() { a.log("GetAirportByID", request, response, err, start) }()
return a.client.GetAirportByID(request.Id)
}

func (a *apiServer) GetAirlineByID(_ context.Context, request *GetAirlineByIDRequest) (response *Airline, err error) {
start := time.Now()
defer func() { a.log("GetAirlineByID", request, response, err, start) }()
return a.client.GetAirlineByID(request.Id)
}

func (a *apiServer) GetRouteByID(_ context.Context, request *GetRouteByIDRequest) (response *Route, err error) {
start := time.Now()
defer func() { a.log("GetRouteByID", request, response, err, start) }()
route, err := a.client.GetRouteByID(request.Id)
if err != nil {
return nil, err
}
return route, nil
}

func (a *apiServer) GetDistanceByID(_ context.Context, request *GetDistanceByIDRequest) (response *google_protobuf.UInt32Value, err error) {
start := time.Now()
defer func() { a.log("GetDistanceByID", request, response, err, start) }()
distance, err := a.client.GetDistanceByID(request.SourceAirportId, request.DestinationAirportId)
if err != nil {
return nil, err
}
return &google_protobuf.UInt32Value{
Value: distance,
}, nil
}

func (a *apiServer) GetAirportByCode(_ context.Context, request *GetAirportByCodeRequest) (response *Airport, err error) {
start := time.Now()
defer func() { a.log("GetAirportByCode", request, response, err, start) }()
return a.client.GetAirportByCode(request.Code)
}

func (a *apiServer) GetAirlineByCode(_ context.Context, request *GetAirlineByCodeRequest) (response *Airline, err error) {
start := time.Now()
defer func() { a.log("GetAirlineByCode", request, response, err, start) }()
return a.client.GetAirlineByCode(request.Code)
}

func (a *apiServer) GetRoutesByCode(_ context.Context, request *GetRoutesByCodeRequest) (response *Routes, err error) {
start := time.Now()
defer func() { a.log("GetRoutesByCode", request, response, err, start) }()
routes, err := a.client.GetRoutesByCode(request.AirlineCode, request.SourceAirportCode, request.DestinationAirportCode)
if err != nil {
return nil, err
}
return &Routes{
Route: routes,
}, nil
}

func (a *apiServer) GetDistanceByCode(_ context.Context, request *GetDistanceByCodeRequest) (response *google_protobuf.UInt32Value, err error) {
start := time.Now()
defer func() { a.log("GetDistanceByCode", request, response, err, start) }()
distance, err := a.client.GetDistanceByCode(request.SourceAirportCode, request.DestinationAirportCode)
if err != nil {
return nil, err
}
return &google_protobuf.UInt32Value{
Value: distance,
}, nil
}

func (a *apiServer) log(methodName string, request proto.Message, response proto.Message, err error, start time.Time) {
protorpclog.Info("flights.API", methodName, request, response, err, time.Since(start))
}
129 client.go
@@ -0,0 +1,129 @@
package flights

import "golang.org/x/net/context"

type client struct {
apiClient APIClient
}

func newClient(apiClient APIClient) *client {
return &client{
apiClient,
}
}

func (c *client) GetAllAirports() ([]*Airport, error) {
airports, err := c.apiClient.GetAllAirports(context.Background(), emptyInstance)
if err != nil {
return nil, err
}
return airports.Airport, nil
}

func (c *client) GetAllAirlines() ([]*Airline, error) {
airlines, err := c.apiClient.GetAllAirlines(context.Background(), emptyInstance)
if err != nil {
return nil, err
}
return airlines.Airline, nil
}

func (c *client) GetAllRoutes() ([]*Route, error) {
routes, err := c.apiClient.GetAllRoutes(context.Background(), emptyInstance)
if err != nil {
return nil, err
}
return routes.Route, nil
}

func (c *client) GetAirportByID(id string) (*Airport, error) {
return c.apiClient.GetAirportByID(
context.Background(),
&GetAirportByIDRequest{
Id: id,
},
)
}

func (c *client) GetAirlineByID(id string) (*Airline, error) {
return c.apiClient.GetAirlineByID(
context.Background(),
&GetAirlineByIDRequest{
Id: id,
},
)
}

func (c *client) GetRouteByID(id string) (*Route, error) {
route, err := c.apiClient.GetRouteByID(
context.Background(),
&GetRouteByIDRequest{
Id: id,
},
)
if err != nil {
return nil, err
}
return route, nil
}

func (c *client) GetDistanceByID(sourceAirportID string, destinationAirportID string) (uint32, error) {
uint32Value, err := c.apiClient.GetDistanceByID(
context.Background(),
&GetDistanceByIDRequest{
SourceAirportId: sourceAirportID,
DestinationAirportId: destinationAirportID,
},
)
if err != nil {
return 0, err
}
return uint32Value.Value, nil
}

func (c *client) GetAirportByCode(code string) (*Airport, error) {
return c.apiClient.GetAirportByCode(
context.Background(),
&GetAirportByCodeRequest{
Code: code,
},
)
}

func (c *client) GetAirlineByCode(code string) (*Airline, error) {
return c.apiClient.GetAirlineByCode(
context.Background(),
&GetAirlineByCodeRequest{
Code: code,
},
)
}

func (c *client) GetRoutesByCode(airlineCode string, sourceAirportCode string, destinationAirportCode string) ([]*Route, error) {
routes, err := c.apiClient.GetRoutesByCode(
context.Background(),
&GetRoutesByCodeRequest{
AirlineCode: airlineCode,
SourceAirportCode: sourceAirportCode,
DestinationAirportCode: destinationAirportCode,
},
)
if err != nil {
return nil, err
}
return routes.Route, nil
}

func (c *client) GetDistanceByCode(sourceAirportCode string, destinationAirportCode string) (uint32, error) {
uint32Value, err := c.apiClient.GetDistanceByCode(
context.Background(),
&GetDistanceByCodeRequest{
SourceAirportCode: sourceAirportCode,
DestinationAirportCode: destinationAirportCode,
},
)
if err != nil {
return 0, err
}
return uint32Value.Value, nil
}
Oops, something went wrong.

0 comments on commit 713ae14

Please sign in to comment.