我目前正在使用Docker运行一个节点服务器,它将与已经上传的智能合约进行交互。下面是我的Docker文件:
FROM node:7
WORKDIR /app
COPY package.json /app
RUN npm install
RUN npm install -g npm
RUN ls
COPY . /app
CMD node gameserver.js
EXPOSE 8081
ARG walletPrivate
ENV walletPrivate = $private
ARG walletPublic
ENV walletPublic = $public
我正在定义将在运行时传递的变量。下面是服务器代码:
const express = require('express');
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const MissionTrackerJson = require('./contracts/MissionTracker.json');
const app = express();
const port = process.env.PORT || 5001;
const providerUrl = 'https://rinkeby.infura.io/N9Txfkh1TNZhoeKXV6Xm';
const gamePublicKey = process.env.public;
const gamePrivateKey = process.env.private;
const production = true;
let contractAddress = null;
let contract = null;
let web3 = new Web3(new Web3.providers.HttpProvider(providerUrl));
if (typeof web3 === 'undefined') throw 'No web3 detected. Is Metamask/Mist being used?';
console.log("Using web3 version: " + Web3.version);
let contractDataPromise = MissionTrackerJson;
let networkIdPromise = web3.eth.net.getId(); // resolves on the current network id
Promise.all([contractDataPromise, networkIdPromise])
.then(results => {
let contractData = results[0];
let networkId = results[1];
// Make sure the contract is deployed on the connected network
if (!(networkId in contractData.networks)) {
throw new Error("Contract not found in selected Ethereum network on MetaMask.");
}
contractAddress = contractData.networks[networkId].address;
contract = new web3.eth.Contract(contractData.abi, contractAddress);
app.listen(port, () => console.log(`Site server on port ${port}`));
})
.catch(console.error);
if (production) {
app.use('/', express.static(`${__dirname}/client/build`));
}
app.get('/api/complete_checkpoint/:reviewer/:checkpoint', (req, res) => {
let reviewerId = req.params.reviewer;
let checkpointId = req.params.checkpoint;
let encodedABI = contract.methods.setCheckpointComplete(reviewerId, checkpointId).encodeABI();
web3.eth.getTransactionCount(gamePublicKey, 'pending')
.then(nonce => {
let rawTx = {
from: gamePublicKey,
to: contractAddress,
gas: 2000000,
data: encodedABI,
gasPrice: '100',
nonce,
};
let tx = new Tx(rawTx);
tx.sign(gamePrivateKey);
let serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log)
.catch(console.error);
})
});
app.get('/api/add_checkpoint/:checkpoint_name', (req, res) => {
console.log("hello");
let checkpointName = decodeURIComponent(req.params.checkpoint_name);
let encodedABI = contract.methods.addGameCheckpoint(checkpointName).encodeABI();
web3.eth.getTransactionCount(gamePublicKey, 'pending')
.then(nonce => {
let rawTx = {
from: gamePublicKey,
to: contractAddress,
gas: 2000000,
data: encodedABI,
gasPrice: '100',
nonce,
};
let tx = new Tx(rawTx);
tx.sign(gamePrivateKey);
let serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log)
.catch(console.error);
})
});
console.log("end");
要调用合约,我需要使用HTTPGET方法ping服务器。当我运行以下命令时,我已经计算出我的Docker服务器的IP地址是172.17.0.2:8081:
docker run -t -i --env private=0x12345 --env public=0x11111 -p 8081:8081 game_server
我正在做我的出港8081
如何向服务器发送HTTP GET方法?还有其他地址吗?
1条答案
按热度按时间r7knjye21#
我觉得你的
Dockerfile
有点乱:当你运行你的容器时,你看到它实际上在工作吗?由于您有/app/app功能,如果
CMD node gameserver.js
命令真的找到了您的服务器文件,我会感到惊讶。另外,
gameserver.js
文件需要一个PORT环境变量,而您没有提供该变量。由于没有提供,您的节点服务器正在侦听端口5001,而不是8081......因此,暴露8081对您没有帮助。不要从容器中暴露8081,而是将-p "8081:5001"
添加到run命令中。或者,提供8081作为环境变量。