Skip to content

Commit

Permalink
fix bugs as requirements, adjust routers rest API accordingly
Browse files Browse the repository at this point in the history
remove useless comments
  • Loading branch information
MakeHoney authored and junbeomlee committed Sep 10, 2018
1 parent 6152fb6 commit 649c31b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 71 deletions.
57 changes: 25 additions & 32 deletions api_gateway/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import (
)

type Endpoints struct {
FindAllCommittedBlocksEndpoint endpoint.Endpoint
FindCommittedBlockByHeightEndpoint endpoint.Endpoint
FindCommittedBlockBySealEndpoint endpoint.Endpoint
FindAllMetaEndpoint endpoint.Endpoint
FindAllCommittedBlocksEndpoint endpoint.Endpoint
FindCommittedBlockBySealEndpoint endpoint.Endpoint
FindAllMetaEndpoint endpoint.Endpoint
}

/*
Expand All @@ -36,9 +35,8 @@ type Endpoints struct {

func MakeBlockchainEndpoints(b BlockQueryApi) Endpoints {
return Endpoints{
FindAllCommittedBlocksEndpoint: makeFindAllCommittedBlocksEndpoint(b),
FindCommittedBlockByHeightEndpoint: makeFindCommittedBlockByHeightEndpoint(b),
FindCommittedBlockBySealEndpoint: makeFindCommittedBlockBySealEndpoint(b),
FindAllCommittedBlocksEndpoint: makeFindAllCommittedBlocksEndpoint(b),
FindCommittedBlockBySealEndpoint: makeFindCommittedBlockBySealEndpoint(b),
}
}

Expand All @@ -53,39 +51,31 @@ func MakeIvmEndpoints(i ICodeQueryApi) Endpoints {
*/
func makeFindAllCommittedBlocksEndpoint(b BlockQueryApi) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {

blocks, err := b.blockRepository.FindAllBlock()

if err != nil {
return nil, err
if request == nil {
blocks, err := b.blockRepository.FindAllBlock()
if err != nil {
return nil, err
}
return blocks, nil
// when request is not nil, it means endponint takes attribute(params) as a request
} else {
req := request.(FindCommittedBlockByHeightRequest)
block, err := b.blockRepository.FindBlockByHeight(req.Height)
if err != nil {
return nil, err
}
return block, nil
}

return blocks, nil
}
}

func makeFindCommittedBlockBySealEndpoint(b BlockQueryApi) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(FindCommittedBlockByIdsRequest)
req := request.(FindCommittedBlockBySealRequest)
block, err := b.blockRepository.FindBlockBySeal(req.Seal)

if err != nil {
return nil, err
}

return block, nil
}
}

func makeFindCommittedBlockByHeightEndpoint(b BlockQueryApi) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(FindCommittedBlockByIdsRequest)
block, err := b.blockRepository.FindBlockByHeight(req.Height)

if err != nil {
return nil, err
}

return block, nil
}
}
Expand All @@ -102,7 +92,10 @@ func makeFindAllMetaEndpoint(i ICodeQueryApi) endpoint.Endpoint {
}
}

type FindCommittedBlockByIdsRequest struct {
type FindCommittedBlockByHeightRequest struct {
Height uint64
Seal []byte
}

type FindCommittedBlockBySealRequest struct {
Seal []byte
}
54 changes: 18 additions & 36 deletions api_gateway/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
ErrBadConversion = errors.New("Conversion failed: invalid argument in url endpoint.")
)

func BlockchainApiHandler(bqa BlockQueryApi, iqa ICodeQueryApi, logger kitlog.Logger) http.Handler {
func NewApiHandler(bqa BlockQueryApi, iqa ICodeQueryApi, logger kitlog.Logger) http.Handler {
r := mux.NewRouter()

be := MakeBlockchainEndpoints(bqa)
Expand All @@ -45,26 +45,19 @@ func BlockchainApiHandler(bqa BlockQueryApi, iqa ICodeQueryApi, logger kitlog.Lo
kithttp.ServerErrorLogger(logger),
}

// GET /blocks/ retrieves all blocks committed
// GET /blocks/:seal retrieves a particular block committed
// GET /blocks/:height retrieves a particular block committed
// GET /icodes about icodes
// GET /blocks/ retrieves all blocks committed
// GET /blocks?height=:height retrieves a particular block committed
// GET /blocks/:seal retrieves a particular block committed
// GET /icodes about icodes

r.Methods("GET").Path("/blocks").Handler(kithttp.NewServer(
be.FindAllCommittedBlocksEndpoint,
decodeFindAllUncommittedTransactionsRequest,
decodeFindAllCommittedBlocksRequest,
encodeResponse,
opts...,
))

r.Methods("GET").Path("/blocks/height/{id}").Handler(kithttp.NewServer(
be.FindCommittedBlockByHeightEndpoint,
decodeFindCommittedBlockByHeightRequest,
encodeResponse,
opts...,
))

r.Methods("GET").Path("/blocks/seal/{seal}").Handler(kithttp.NewServer(
r.Methods("GET").Path("/blocks/{id}").Handler(kithttp.NewServer(
be.FindCommittedBlockBySealEndpoint,
decodeFindCommittedBlockBySealRequest,
encodeResponse,
Expand All @@ -87,47 +80,36 @@ func decodeFindAllUncommittedTransactionsRequest(_ context.Context, r *http.Requ
}

func decodeFindAllCommittedBlocksRequest(_ context.Context, r *http.Request) (interface{}, error) {
if heightStr := r.URL.Query().Get("height"); heightStr != "" {
height, err := strconv.ParseUint(heightStr, 10, 64)
if err != nil {
return nil, ErrBadConversion
}
return FindCommittedBlockByHeightRequest{Height: height}, nil
}
// length of query string is zero => means that there are no restful params
return nil, nil
}

func decodeFindAllMetaRequest(_ context.Context, r *http.Request) (interface{}, error) {
return nil, nil
}

func decodeFindCommittedBlockByHeightRequest(_ context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
heightStr, ok := vars["id"]

if !ok {
return nil, ErrBadRouting
}

height, err := strconv.ParseUint(heightStr, 10, 64)

if err != nil {
return nil, ErrBadConversion
}

return FindCommittedBlockByIdsRequest{Height: height}, nil
}

func decodeFindCommittedBlockBySealRequest(_ context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
sealStr, ok := vars["seal"]

sealStr, ok := vars["id"]
if !ok {
return nil, ErrBadRouting
}

seal, err := hex.DecodeString(sealStr)

if err != nil {
return nil, ErrBadRouting
return nil, ErrBadConversion
}

seal = []byte(seal)

return FindCommittedBlockByIdsRequest{Seal: seal}, nil
return FindCommittedBlockBySealRequest{Seal: seal}, nil
}

func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
Expand Down
6 changes: 3 additions & 3 deletions it-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ func initApiGateway(config *conf.Configuration, errs chan error) func() {
panic(err)
}

var h http.Handler
var handler http.Handler
{
h = api_gateway.BlockchainApiHandler(blockQueryApi, icodeQueryApi, httpLogger)
handler = api_gateway.NewApiHandler(blockQueryApi, icodeQueryApi, httpLogger)
}

http.Handle("/", mux)

go func() {
logger.Infof(nil, "[Main] Api-gateway is staring on port:%s", config.ApiGateway.Port)
errs <- http.ListenAndServe(ipAddress, h)
errs <- http.ListenAndServe(ipAddress, handler)
}()

return func() {
Expand Down

0 comments on commit 649c31b

Please sign in to comment.