-
Notifications
You must be signed in to change notification settings - Fork 72
/
orders-function.yaml
165 lines (145 loc) · 3.94 KB
/
orders-function.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
apiVersion: serverless.kyma-project.io/v1alpha1
kind: Function
metadata:
name: orders-function
namespace: orders-service
labels:
app: orders-function
example: orders-function
spec:
deps: |-
{
"name": "orders-function",
"version": "1.0.0",
"dependencies": {
"redis": "3.0.2"
}
}
maxReplicas: 1
minReplicas: 1
resources:
limits:
cpu: 20m
memory: 32Mi
requests:
cpu: 10m
memory: 16Mi
env:
- name: APP_REDIS_PREFIX
value: "REDIS_"
source: |-
const redis = require("redis");
const { promisify } = require("util");
let storage = undefined;
const errors = {
codeRequired: new Error("orderCode is required"),
alreadyExists: new Error("object already exists"),
}
module.exports = {
main: async function (event, _) {
const storage = getStorage();
if (!event.data || !Object.keys(event.data).length) {
return await onList(storage, event);
}
const { orderCode, consignmentCode, consignmentStatus } = event.data;
if (orderCode && consignmentCode && consignmentStatus) {
return await onCreate(storage, event);
}
event.extensions.response.status(500);
}
}
async function onList(storage, event) {
try {
return await storage.getAll();
} catch(err) {
event.extensions.response.status(500);
return;
}
}
async function onCreate(storage, event) {
try {
await storage.set(event.data);
} catch(err) {
let status = 500;
switch (err) {
case errors.codeRequired: {
status = 400;
break;
};
case errors.alreadyExists: {
status = 409;
break;
};
}
event.extensions.response.status(status);
}
}
class RedisStorage {
storage = undefined;
asyncGet = void 0;
asyncKeys = void 0;
asyncSet = void 0;
constructor(options) {
this.storage = redis.createClient(options);
this.asyncGet = promisify(this.storage.get).bind(this.storage);
this.asyncKeys = promisify(this.storage.keys).bind(this.storage);
this.asyncSet = promisify(this.storage.set).bind(this.storage);
}
async getAll() {
let values = [];
const keys = await this.asyncKeys("*");
for (const key of keys) {
const value = await this.asyncGet(key);
values.push(JSON.parse(value));
}
return values;
}
async set(order = {}) {
if (!order.orderCode) {
throw errors.codeRequired;
}
const value = await this.asyncGet(order.orderCode);
if (value) {
throw errors.alreadyExists;
}
await this.asyncSet(order.orderCode, JSON.stringify(order));
}
}
class InMemoryStorage {
storage = new Map();
getAll() {
return Array.from(this.storage)
.map(([_, order]) => order)
}
set(order = {}) {
if (!order.orderCode) {
throw errors.codeRequired;
}
if (this.storage.get(order.orderCode)) {
throw errors.alreadyExists;
}
return this.storage.set(order.orderCode, order);
}
}
function readEnv(env = "") {
return process.env[env] || undefined;
}
function createStorage() {
let redisPrefix = readEnv("APP_REDIS_PREFIX");
if (!redisPrefix) {
redisPrefix = "REDIS_";
}
const port = readEnv(redisPrefix + "PORT");
const host = readEnv(redisPrefix + "HOST");
const password = readEnv(redisPrefix + "REDIS_PASSWORD");
if (host && port && password) {
return new RedisStorage({ host, port, password });
}
return new InMemoryStorage();
}
function getStorage() {
if (!storage) {
storage = createStorage();
}
return storage;
}