Skip to content

aakashsharan/akka-serverless-social-trading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

social-trading-app

Details

Building social trading app as part of Akka Serverless Hackathon. Architecture will be added later This repository contains the Trade Service. Trade has the following properties:

State

trade_id (string)
buyer_user_id(string)
seller_user_id(string)
buyer_item_ids(string list)
seller_item_ids(string list)
trade_offered_timestamp(int64)
status[CREATED, ACCEPTED, REJECTED)

Command

CreateTrade
AcceptTrade
RejectTrade
GetTrade

Event

TradeOffered
TradeAccepted
TradeRejected

Eventing (Publish)

TradeOffered
TradeAccepted
TradeRejected

View

getTradeByUser

  • Query: select* from trade_by_user where user_id = :user_id

Building

To build, at a minimum you need to generate and process sources, particularly when using an IDE. A convenience is compile your project:

mvn compile

Running Locally

In order to run your application locally, you must run the Akka Serverless proxy. The included docker-compose file contains the configuration required to run the proxy for a locally running application. It also contains the configuration to start a local Google Pub/Sub emulator that the Akka Serverless proxy will connect to. To start the proxy, run the following command from this directory:

docker-compose up

On Linux this requires Docker 20.10 or later (moby/moby#40007), or for a USER_FUNCTION_HOST environment variable to be set manually.

docker-compose -f docker-compose.yml -f docker-compose.linux.yml up

To start the application locally, the exec-maven-plugin is used. Use the following command:

mvn compile exec:java

Exercise the service - Locally

Note - You can also directly use http call to do the same actions. For example to get the cart: localhost:9000/carts/3232

Create a trade

grpcurl \
  -d '{"tradeId": "1212", "buyerUserId": "3", "sellerUserId": "5", "buyerItemIds" : [{"itemId" : "1"}, {"itemId" : "2"}], "sellerItemIds" : [{"itemId" : "10"}, {"itemId" : "12"}]}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.CreateTrade

Output:

{

}

Get the trade

grpcurl \
  -d '{"tradeId": "1212"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "1212",
  "buyer_user_id": "3",
  "seller_user_id": "5",
  "buyer_item_ids": [
    {
      "item_id": "1"
    },
    {
      "item_id": "2"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "10"
    },
    {
      "item_id": "12"
    }
  ],
  "trade_offered_timestamp": "1624642878834"
}

Accept the trade

grpcurl \
  -d '{"tradeId": "1212"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.AcceptTrade

Output:

{

}

Now, get the trade

grpcurl \
  -d '{"tradeId": "1212"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "1212",
  "buyer_user_id": "3",
  "seller_user_id": "5",
  "buyer_item_ids": [
    {
      "item_id": "1"
    },
    {
      "item_id": "2"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "10"
    },
    {
      "item_id": "12"
    }
  ],
  "trade_offered_timestamp": "1624642920511",
  "status": "ACCEPTED"
}

Create a trade

grpcurl \
  -d '{"tradeId": "113", "buyerUserId": "1", "sellerUserId": "3", "buyerItemIds" : [{"itemId" : "8"}, {"itemId" : "11"}], "sellerItemIds" : [{"itemId" : "2"}, {"itemId" : "6"}]}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.CreateTrade

Output:

{

}

Get the trade

grpcurl \
  -d '{"tradeId": "113"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "113",
  "buyer_user_id": "1",
  "seller_user_id": "3",
  "buyer_item_ids": [
    {
      "item_id": "8"
    },
    {
      "item_id": "11"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "2"
    },
    {
      "item_id": "6"
    }
  ],
  "trade_offered_timestamp": "1624643005560"
}

Reject the trade

grpcurl \
  -d '{"tradeId": "113"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.RejectTrade

Output:

{

}

Now, get the trade

grpcurl \
  -d '{"tradeId": "113"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "113",
  "buyer_user_id": "1",
  "seller_user_id": "3",
  "buyer_item_ids": [
    {
      "item_id": "8"
    },
    {
      "item_id": "11"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "2"
    },
    {
      "item_id": "6"
    }
  ],
  "trade_offered_timestamp": "1624643005560",
  "status": "REJECTED"
}

Now, get the trade on buyer id

grpcurl \
  -d '{"userId": "1"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.view.TradeViewService.GetTradeByUserId

Output:

{
  "trade_id": "113",
  "buyer_user_id": "1",
  "seller_user_id": "3",
  "buyer_item_ids": [
    {
      "item_id": "8"
    },
    {
      "item_id": "11"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "2"
    },
    {
      "item_id": "6"
    }
  ],
  "trade_offered_timestamp": "1624643005571",
  "status": "REJECTED"
}

Now, get the trade on seller id

grpcurl \
  -d '{"userId": "5"}' \
  -plaintext localhost:9000 \
  com.lightbend.gsa.trade.view.TradeViewService.GetTradeByUserId

Output:

{
  "trade_id": "1212",
  "buyer_user_id": "3",
  "seller_user_id": "5",
  "buyer_item_ids": [
    {
      "item_id": "1"
    },
    {
      "item_id": "2"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "10"
    },
    {
      "item_id": "12"
    }
  ],
  "trade_offered_timestamp": "1624643493608",
  "status": "ACCEPTED"
}

Deploying - to Akka Serverless

To deploy your service, install the akkasls CLI as documented in

To skip tests:

mvn clean install -Dmaven.test.skip=true

else

mvn clean install

Push the image to the repository:

docker push {docker-username}/{project-name}

Authorize akka serverless:

akkasls auth login

Set the project:

akkasls config set project <project-name>

Deploy the service to akka serverless

akkasls services deploy <servicename> docker.io/{docker-username}/{project-name}

Expose the service

akkasls service expose <servicename> --enable-cors

Output:

Service <servicename> was successfully exposed at: exampleABCD.us-east1.apps.akkaserverless.io

Exercise the example - Service exposed on Akka Serverless

Create a trade

grpcurl -d '{"tradeId": "1212", "buyerUserId": "3", "sellerUserId": "5", "buyerItemIds" : [{"itemId" : "1"}, {"itemId" : "2"}], "sellerItemIds" : [{"itemId" : "10"}, {"itemId" : "12"}]}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.CreateTrade

Output:

{

}

Get the trade

grpcurl -d '{"tradeId": "1212"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "1212",
  "buyer_user_id": "3",
  "seller_user_id": "5",
  "buyer_item_ids": [
    {
      "item_id": "1"
    },
    {
      "item_id": "2"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "10"
    },
    {
      "item_id": "12"
    }
  ],
  "trade_offered_timestamp": "1624645398129"
}

Accept the trade

grpcurl -d '{"tradeId": "1212"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.AcceptTrade

Output:

{

}

Now, get the trade

grpcurl -d '{"tradeId": "1212"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "1212",
  "buyer_user_id": "3",
  "seller_user_id": "5",
  "buyer_item_ids": [
    {
      "item_id": "1"
    },
    {
      "item_id": "2"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "10"
    },
    {
      "item_id": "12"
    }
  ],
  "trade_offered_timestamp": "1624645457811",
  "status": "ACCEPTED"
}

Create a trade

grpcurl -d '{"tradeId": "113", "buyerUserId": "1", "sellerUserId": "3", "buyerItemIds" : [{"itemId" : "8"}, {"itemId" : "11"}], "sellerItemIds" : [{"itemId" : "2"}, {"itemId" : "6"}]}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.CreateTrade

Output:

{

}

Get the trade

grpcurl -d '{"tradeId": "113"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "113",
  "buyer_user_id": "1",
  "seller_user_id": "3",
  "buyer_item_ids": [
    {
      "item_id": "8"
    },
    {
      "item_id": "11"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "2"
    },
    {
      "item_id": "6"
    }
  ],
  "trade_offered_timestamp": "1624645631208"
}

Reject the trade

grpcurl -d '{"tradeId": "113"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.RejectTrade

Output:

{

}

Now, get the trade

grpcurl -d '{"tradeId": "113"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.TradeService.GetTrade

Output:

{
  "trade_id": "113",
  "buyer_user_id": "1",
  "seller_user_id": "3",
  "buyer_item_ids": [
    {
      "item_id": "8"
    },
    {
      "item_id": "11"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "2"
    },
    {
      "item_id": "6"
    }
  ],
  "trade_offered_timestamp": "1624645678491",
  "status": "REJECTED"
}

Now, get the trade on buyer id

grpcurl -d '{"userId": "1"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.view.TradeViewService.GetTradeByUserId

Output:

{
  "trade_id": "113",
  "buyer_user_id": "1",
  "seller_user_id": "3",
  "buyer_item_ids": [
    {
      "item_id": "8"
    },
    {
      "item_id": "11"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "2"
    },
    {
      "item_id": "6"
    }
  ],
  "trade_offered_timestamp": "1624645600890",
  "status": "REJECTED"
}

Now, get the trade on seller id

grpcurl -d '{"userId": "5"}' exampleABCD.us-east1.apps.akkaserverless.io:443 com.lightbend.gsa.trade.view.TradeViewService.GetTradeByUserId

Output:

{
  "trade_id": "1212",
  "buyer_user_id": "3",
  "seller_user_id": "5",
  "buyer_item_ids": [
    {
      "item_id": "1"
    },
    {
      "item_id": "2"
    }
  ],
  "seller_item_ids": [
    {
      "item_id": "10"
    },
    {
      "item_id": "12"
    }
  ],
  "trade_offered_timestamp": "1624645328653",
  "status": "ACCEPTED"
}

About

A social trading app using Akka Serverless - Part of Hackathon

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages