Skip to content

Commit

Permalink
Add currencyservice
Browse files Browse the repository at this point in the history
  • Loading branch information
hfrog committed Jan 26, 2024
1 parent 9dfbb8c commit 7d4059b
Show file tree
Hide file tree
Showing 16 changed files with 4,473 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/.helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ dependencies:
export-values:
- parent: werf
child: werf

- name: currencyservice
version: "1.0.0"
export-values:
- parent: werf
child: werf
3 changes: 3 additions & 0 deletions app/.helm/charts/currencyservice/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v2
name: currencyservice
version: 1.0.0
54 changes: 54 additions & 0 deletions app/.helm/charts/currencyservice/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: currencyservice
spec:
selector:
matchLabels:
app: currencyservice
template:
metadata:
labels:
app: currencyservice
spec:
serviceAccountName: default
terminationGracePeriodSeconds: 5
securityContext:
fsGroup: 1000
runAsGroup: 1000
runAsNonRoot: true
runAsUser: 1000
imagePullSecrets:
- name: registrysecret
containers:
- name: server
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
readOnlyRootFilesystem: true
image: {{ $.Values.werf.image.currencyservice }}
ports:
- name: grpc
containerPort: 7000
env:
- name: PORT
value: "7000"
- name: DISABLE_PROFILER
value: "1"
readinessProbe:
grpc:
port: 7000
livenessProbe:
grpc:
port: 7000
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
cpu: 200m
memory: 128Mi
13 changes: 13 additions & 0 deletions app/.helm/charts/currencyservice/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: v1
kind: Service
metadata:
name: currencyservice
spec:
type: ClusterIP
selector:
app: currencyservice
ports:
- name: grpc
port: 7000
targetPort: 7000
2 changes: 2 additions & 0 deletions app/currencyservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
client.js
node_modules/
1 change: 1 addition & 0 deletions app/currencyservice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
42 changes: 42 additions & 0 deletions app/currencyservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM node:20.11.0-alpine@sha256:8e6a472eb9742f4f486ca9ef13321b7fc2e54f2f60814f339eeda2aff3037573 as base

FROM base as builder

# Some packages (e.g. @google-cloud/profiler) require additional
# deps for post-install scripts
RUN apk add --update --no-cache \
python3 \
make \
g++

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=production

FROM base

WORKDIR /usr/src/app

COPY --from=builder /usr/src/app/node_modules ./node_modules

COPY . .

EXPOSE 7000

ENTRYPOINT [ "node", "server.js" ]
68 changes: 68 additions & 0 deletions app/currencyservice/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
require('@google-cloud/trace-agent').start();

const path = require('path');
const grpc = require('grpc');
const pino = require('pino');

const PROTO_PATH = path.join(__dirname, './proto/demo.proto');
const PORT = 7000;

const shopProto = grpc.load(PROTO_PATH).hipstershop;
const client = new shopProto.CurrencyService(`localhost:${PORT}`,
grpc.credentials.createInsecure());

const logger = pino({
name: 'currencyservice-client',
messageKey: 'message',
formatters: {
level (logLevelString, logLevelNum) {
return { severity: logLevelString }
}
}
});

const request = {
from: {
currency_code: 'CHF',
units: 300,
nanos: 0
},
to_code: 'EUR'
};

function _moneyToString (m) {
return `${m.units}.${m.nanos.toString().padStart(9,'0')} ${m.currency_code}`;
}

client.getSupportedCurrencies({}, (err, response) => {
if (err) {
logger.error(`Error in getSupportedCurrencies: ${err}`);
} else {
logger.info(`Currency codes: ${response.currency_codes}`);
}
});

client.convert(request, (err, response) => {
if (err) {
logger.error(`Error in convert: ${err}`);
} else {
logger.log(`Convert: ${_moneyToString(request.from)} to ${_moneyToString(response)}`);
}
});
35 changes: 35 additions & 0 deletions app/currencyservice/data/currency_conversion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"EUR": "1.0",
"USD": "1.1305",
"JPY": "126.40",
"BGN": "1.9558",
"CZK": "25.592",
"DKK": "7.4609",
"GBP": "0.85970",
"HUF": "315.51",
"PLN": "4.2996",
"RON": "4.7463",
"SEK": "10.5375",
"CHF": "1.1360",
"ISK": "136.80",
"NOK": "9.8040",
"HRK": "7.4210",
"RUB": "74.4208",
"TRY": "6.1247",
"AUD": "1.6072",
"BRL": "4.2682",
"CAD": "1.5128",
"CNY": "7.5857",
"HKD": "8.8743",
"IDR": "15999.40",
"ILS": "4.0875",
"INR": "79.4320",
"KRW": "1275.05",
"MXN": "21.7999",
"MYR": "4.6289",
"NZD": "1.6679",
"PHP": "59.083",
"SGD": "1.5349",
"THB": "36.012",
"ZAR": "16.0583"
}
23 changes: 23 additions & 0 deletions app/currencyservice/genproto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash -eu
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_currencyservice_genproto]

# protos are loaded dynamically for node, simply copies over the proto.
mkdir -p proto
cp -r ../../protos/* ./proto

# [END gke_currencyservice_genproto]
Loading

0 comments on commit 7d4059b

Please sign in to comment.