app.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import fetch from "node-fetch"
  2. import express from "express"
  3. const app = express();
  4. const port = 3000;
  5. //TODO load these values from setting or env
  6. const irohaApiHost = "http://localhost";
  7. const irohaApiPort = 5000;
  8. const irohaDomain = "test";
  9. app.post("/price", (req, res) => {
  10. //TODO implement price calculation depending on extent area
  11. res.send({
  12. price: 33
  13. });
  14. });
  15. app.get("/users/:userId/assets", async (req, res) => {
  16. try{
  17. res.send(await getUsersAssets(req.params.userId));
  18. }
  19. catch(error){
  20. console.error(error);
  21. res.status(500);
  22. res.send(error);
  23. }
  24. });
  25. app.get("/users/:userId/assets/:assetId", async (req, res) => {
  26. try{
  27. let response = await getUsersAssets(req.params.userId);
  28. let i = 0;
  29. for(i; i < response.assets.length; i++){
  30. if(response.assets[i].assetId.startsWith(req.params.assetId)){
  31. res.send(response.assets[i]);
  32. return;
  33. }
  34. }
  35. }
  36. catch(error){
  37. console.error(error);
  38. res.status(500);
  39. res.send(error);
  40. }
  41. });
  42. app.post("/buy", (req, res) => {
  43. //TODO implement...
  44. res.status(201);
  45. });
  46. app.listen(port, () => {
  47. console.log(`Listening at http://localhost:${port}`)
  48. });
  49. async function getUsersAssets(userId){
  50. let response = await fetch(irohaApiHost + ':' + irohaApiPort + '/accounts/' + userId + '@' + irohaDomain + '/assets/');
  51. return await response.json();
  52. }