Update sample 2 to work for 'Next Steps' section in Readme (#149)

* Update sample 2 to work for 'Next Steps' section in Readme

* Copy node and python app code
* Correct the Dockerfiles
* Correct the makefile
* Add tags to images in yaml

* Delete package-lock.json

Co-authored-by: Young Bu Park <youngp@microsoft.com>
This commit is contained in:
pruthvidhodda 2020-02-11 15:27:31 -08:00 committed by GitHub
parent a8baa63b0f
commit 98fb473cbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 107 additions and 14 deletions

View File

@ -36,7 +36,7 @@ spec:
spec:
containers:
- name: node
image: dapriosamples/hello-k8s-node
image: dapriosamples/hello-k8s-node:0.4.0
ports:
- containerPort: 3000
imagePullPolicy: Always

View File

@ -19,4 +19,4 @@ spec:
spec:
containers:
- name: python
image: dapriosamples/hello-k8s-python
image: dapriosamples/hello-k8s-python:0.4.0

View File

@ -14,16 +14,7 @@ DOCKERFILE:=Dockerfile
.PHONY: build
SAMPLE_APPS:=$(foreach ITEM,$(APPS),$(DOCKER_IMAGE_PREFIX)$(ITEM))
build: copy $(SAMPLE_APPS)
# copy app files from 1.hello-world
copy:
mkdir -p python/.dockerfiles
mkdir -p node/.dockerfiles
cp ../1.hello-world/app.py python/.dockerfiles/
cp ../1.hello-world/app.js node/.dockerfiles/
cp ../1.hello-world/package.json node/.dockerfiles/
cp ../1.hello-world/package-lock.json node/.dockerfiles/
build: $(SAMPLE_APPS)
# Generate docker image build targets
# Params:

View File

@ -1,6 +1,6 @@
FROM node:8-alpine
WORKDIR /app
COPY .dockerfiles .
COPY . .
RUN npm install
EXPOSE 3000
CMD [ "node", "app.js" ]

View File

@ -0,0 +1,63 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
const express = require('express');
const bodyParser = require('body-parser');
require('isomorphic-fetch');
const app = express();
app.use(bodyParser.json());
const daprPort = process.env.DAPR_HTTP_PORT || 3500;
const stateStoreName = `statestore`;
const stateUrl = `http://localhost:${daprPort}/v1.0/state/${stateStoreName}`;
const port = 3000;
app.get('/order', (_req, res) => {
fetch(`${stateUrl}/order`)
.then((response) => {
if (!response.ok) {
throw "Could not get state.";
}
return response.text();
}).then((orders) => {
res.send(orders);
}).catch((error) => {
console.log(error);
res.status(500).send({message: error});
});
});
app.post('/neworder', (req, res) => {
const data = req.body.data;
const orderId = data.orderId;
console.log("Got a new order! Order ID: " + orderId);
const state = [{
key: "order",
value: data
}];
fetch(stateUrl, {
method: "POST",
body: JSON.stringify(state),
headers: {
"Content-Type": "application/json"
}
}).then((response) => {
if (!response.ok) {
throw "Failed to persist state.";
}
console.log("Successfully persisted state.");
res.status(200).send();
}).catch((error) => {
console.log(error);
res.status(500).send({message: error});
});
});
app.listen(port, () => console.log(`Node App listening on port ${port}!`));

View File

@ -0,0 +1,16 @@
{
"name": "node_server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"isomorphic-fetch": "^2.2.1"
}
}

View File

@ -1,6 +1,6 @@
FROM python:3.7.1-alpine3.8
WORKDIR /app
COPY .dockerfiles .
COPY . .
RUN pip install requests
ENTRYPOINT ["python"]
CMD ["app.py"]

View File

@ -0,0 +1,23 @@
# ------------------------------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------------------------------
import time
import requests
import os
dapr_port = os.getenv("DAPR_HTTP_PORT", 3500)
dapr_url = "http://localhost:{}/v1.0/invoke/nodeapp/method/neworder".format(dapr_port)
n = 0
while True:
n += 1
message = {"data": {"orderId": n}}
try:
response = requests.post(dapr_url, json=message)
except Exception as e:
print(e)
time.sleep(1)