{"id":14957120,"url":"https://github.com/chhaileng/fabcar-client","last_synced_at":"2025-05-02T07:33:19.999Z","repository":{"id":55331754,"uuid":"206301888","full_name":"chhaileng/fabcar-client","owner":"chhaileng","description":"Fabric Car Client using React JS","archived":false,"fork":false,"pushed_at":"2023-11-12T01:51:47.000Z","size":400,"stargazers_count":10,"open_issues_count":1,"forks_count":11,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T23:07:41.044Z","etag":null,"topics":["fabcar","hyperledger-fabric","react-js"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chhaileng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-04T11:19:45.000Z","updated_at":"2024-08-19T15:07:26.000Z","dependencies_parsed_at":"2024-09-22T02:30:41.403Z","dependency_job_id":"7913d16a-4b1b-4064-8704-b9eb31d58cd7","html_url":"https://github.com/chhaileng/fabcar-client","commit_stats":{"total_commits":9,"total_committers":3,"mean_commits":3.0,"dds":0.2222222222222222,"last_synced_commit":"87c3ede84d7029a15969fc88ba2644f22668574c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chhaileng%2Ffabcar-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chhaileng%2Ffabcar-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chhaileng%2Ffabcar-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chhaileng%2Ffabcar-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chhaileng","download_url":"https://codeload.github.com/chhaileng/fabcar-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224305842,"owners_count":17289446,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fabcar","hyperledger-fabric","react-js"],"created_at":"2024-09-24T13:14:06.587Z","updated_at":"2024-11-12T16:04:55.259Z","avatar_url":"https://github.com/chhaileng.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FabCar Client (React JS)\n\nThis is the client web based application for FabCar Blockchain application which is a sample app from Hyperledger Fabric. \n\n## Screenshots\n\n![All cars](https://raw.githubusercontent.com/chhaileng/fabcar-client/master/screenshots/fabcar-all.png)\n\n![Add car](https://raw.githubusercontent.com/chhaileng/fabcar-client/master/screenshots/fabcar-add.png)\n\n![Change owner](https://raw.githubusercontent.com/chhaileng/fabcar-client/master/screenshots/fabcar-change-owner.png)\n\nTo start this application you have to setup as following. \n\n## Steps\n\n1. Network setup\n2. Enroll admin and register user\n3. REST Server\n4. Client app\n\n## Network\n\nClone the project from [fabric-samples](https://github.com/hyperledger/fabric-samples) repository. Go to `fabcar` directory and start the network.\n\n```\n$ git clone https://github.com/hyperledger/fabric-samples.git\n$ cd fabcar\n$ ./startFabric.sh\n```\n\nNow the network should be started and chaincode should be installed to the network.\n\n## Enroll admin and register user\n\nIn the `fabcar` directory, go to javascript directory and run:\n\n```\n$ node enrollAdmin.js\n$ node registerUser.js\n```\n\nAfter running this, we can test by querying some records from the network by running:\n\n```\n$ node query.js\n```\n\n## REST Server\n\nCreate a file `app.js` in that directory and put this code.\n\n```\nconst express = require('express')\nconst app = express()\n\nconst { FileSystemWallet, Gateway } = require('fabric-network');\nconst path = require('path');\n\nconst ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');\n\n// CORS Origin\napp.use(function (req, res, next) {\n  res.setHeader('Access-Control-Allow-Origin', '*');\n  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');\n  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');\n  res.setHeader('Access-Control-Allow-Credentials', true);\n  next();\n});\n\napp.use(express.json());\n\napp.get('/cars', async (req, res) =\u003e {\n  try {\n    const walletPath = path.join(process.cwd(), 'wallet');\n    const wallet = new FileSystemWallet(walletPath);\n    const userExists = await wallet.exists('user1');\n    if (!userExists) {\n      res.json({status: false, error: {message: 'User not exist in the wallet'}});\n      return;\n    }\n\n    const gateway = new Gateway();\n    await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });\n    const network = await gateway.getNetwork('mychannel');\n    const contract = network.getContract('fabcar');\n    const result = await contract.evaluateTransaction('queryAllCars');\n    res.json({status: true, cars: JSON.parse(result.toString())});\n  } catch (err) {\n    res.json({status: false, error: err});\n  }\n});\n\napp.get('/cars/:key', async (req, res) =\u003e {\n  try {\n    const walletPath = path.join(process.cwd(), 'wallet');\n    const wallet = new FileSystemWallet(walletPath);\n    const userExists = await wallet.exists('user1');\n    if (!userExists) {\n      res.json({status: false, error: {message: 'User not exist in the wallet'}});\n      return;\n    }\n\n    const gateway = new Gateway();\n    await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });\n    const network = await gateway.getNetwork('mychannel');\n    const contract = network.getContract('fabcar');\n    const result = await contract.evaluateTransaction('queryCar', req.params.key);\n    res.json({status: true, car: JSON.parse(result.toString())});\n  } catch (err) {\n    res.json({status: false, error: err});\n  }\n});\n\napp.post('/cars', async (req, res) =\u003e {\n  if ((typeof req.body.key === 'undefined' || req.body.key === '') ||\n      (typeof req.body.make === 'undefined' || req.body.make === '') ||\n      (typeof req.body.model === 'undefined' || req.body.model === '') ||\n      (typeof req.body.color === 'undefined' || req.body.color === '') ||\n      (typeof req.body.owner === 'undefined' || req.body.owner === '')) {\n    res.json({status: false, error: {message: 'Missing body.'}});\n    return;\n  }\n\n  try {\n    const walletPath = path.join(process.cwd(), 'wallet');\n    const wallet = new FileSystemWallet(walletPath);\n    const userExists = await wallet.exists('user1');\n    if (!userExists) {\n      res.json({status: false, error: {message: 'User not exist in the wallet'}});\n      return;\n    }\n\n    const gateway = new Gateway();\n    await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });\n    const network = await gateway.getNetwork('mychannel');\n    const contract = network.getContract('fabcar');\n    await contract.submitTransaction('createCar', req.body.key, req.body.make, req.body.model, req.body.color, req.body.owner);\n    res.json({status: true, message: 'Transaction (create car) has been submitted.'})\n  } catch (err) {\n    res.json({status: false, error: err});\n  }\n});\n\napp.put('/cars', async (req, res) =\u003e {\n  if ((typeof req.body.key === 'undefined' || req.body.key === '') ||\n      (typeof req.body.owner === 'undefined' || req.body.owner === '')) {\n    res.json({status: false, error: {message: 'Missing body.'}});\n    return;\n  }\n\n  try {\n    const walletPath = path.join(process.cwd(), 'wallet');\n    const wallet = new FileSystemWallet(walletPath);\n    const userExists = await wallet.exists('user1');\n    if (!userExists) {\n      res.json({status: false, error: {message: 'User not exist in the wallet'}});\n      return;\n    }\n\n    const gateway = new Gateway();\n    await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });\n    const network = await gateway.getNetwork('mychannel');\n    const contract = network.getContract('fabcar');\n    await contract.submitTransaction('changeCarOwner', req.body.key, req.body.owner);\n    res.json({status: true, message: 'Transaction (change car owner) has been submitted.'})\n  } catch (err) {\n    res.json({status: false, error: err});\n  }\n});\n\napp.listen(3000, () =\u003e {\n  console.log('REST Server listening on port 3000');\n});\n```\n\nRun\n\n```\n$ node app.js\n```\n\n## Running React Client Web\n\n### Windows\n\nClone this repository and update environment variables in project folder\n\n```\n$ cd fabcar-client/\n```\n\n[Optional] Change API endpoint by editing REACT_APP_API_HOST var in `.env.development`, `.env.test`, `.env.production`\n\n- change `localhost` to `Server IP`\n  \n```ENV\nREACT_APP_API_HOST=localhost # Server IP\n```\n\nInstall dependencies and build project\n\n```\n$ npm install create-react-app -g\n$ npm install axios react-dom react-router-dom\n$ npm run build\n```\n\n### Ubuntu\n\nClone this repository and update environment variables in project folder\n\n```\n$ cd fabcar-client/ \n```\n\n[Optional] Change API endpoint by editing REACT_APP_API_HOST var in `.env.development`, `.env.test`, `.env.production`\n\n- change `localhost` to `Server IP`\n  \n```ENV\nREACT_APP_API_HOST=localhost # Server IP\n```\n\nCreate new React App \n```\n$ cd ../\n$ npm install create-react-app -g\n$ create-react-app fabcar-ui\n```\n\nRemove default files created by React and copy source codes from fabcar-client to new React App folder\n```\n$ cd fabcar-ui\n$ rm -rf src\n$ cp -r ../fabcar-client/src ../fabcar-client/.env.* ./\n$ cp ../fabcar-client/public/index.html ./public/\n```\n\nInstall dependencies and build project\n```\n$ npm install axios react-dom react-router-dom\n$ npm run build\n```\n\n### Deploy docker container for both window and ubuntu\n\nDeploy NGINX container\n- Go to your React App root directory\n- Create `default.conf` file \n\n```\nvi default.conf\n```\n\nCopy and paste below script to `default.conf`\n\n```conf\nserver {\n   listen 80;\n   server_name  localhost;\n\n   location / {\n       root   /usr/share/nginx/html;\n       index  index.html;\n       try_files $uri $uri/ /index.html;\n   }\n}\n```\n\nRun docker NGINX container on port 8081 by exposed the port\n\n```sh\n$ docker run --name fabcar-nginx-container -d -p 8081:80 -v $(pwd)/build:/usr/share/nginx/html -v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf nginx\n```\n\n## Happy hacking...","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchhaileng%2Ffabcar-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchhaileng%2Ffabcar-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchhaileng%2Ffabcar-client/lists"}