Skip to content

Commit 3f65acb

Browse files
committed
remove S3 trigger handling
1 parent 8e18bf9 commit 3f65acb

File tree

1 file changed

+53
-66
lines changed

1 file changed

+53
-66
lines changed

src/main/scala/Main.scala

+53-66
Original file line numberDiff line numberDiff line change
@@ -70,95 +70,82 @@ object Main {
7070
URI.create(s"http://$apiurl/2018-06-01/runtime/invocation/next")
7171
).build()
7272

73-
val res = client.send(req, BodyHandlers.ofString())
73+
val res = client.send(req, BodyHandlers.ofString())
7474
val reqId = res.headers().firstValue("Lambda-Runtime-Aws-Request-Id").get()
7575

76-
if (res.body().startsWith("{\n\"Records\":")) {
77-
val pattern = ".*key\": \"(?<key> [^\"]*)\".*".r
76+
val response = readFromString[InputEvent](res.body()) match {
77+
case GetListEvent =>
78+
s"""{"statusCode": 200, "vmID": "$vmID", "body": "${writeToString[List[TodoTask]](todoList.toList)}"}"""
7879

79-
val deltaStateKey = pattern.findFirstMatchIn(res.body()).get.group("key")
80+
case AddTaskEvent(desc) =>
81+
val task = TodoTask(desc)
8082

81-
if (!deltaStateKey.startsWith(vmID)) {
82-
val deltaState = getState(deltaStateKey)
83+
val mutatedList = todoList.prepend(task)
84+
todoList = mutatedList.resetDeltaBuffer()
8385

84-
todoList = todoList.applyDelta(Delta("remote", deltaState)).resetDeltaBuffer()
85-
}
86-
} else {
87-
val response = readFromString[InputEvent](res.body()) match {
88-
case GetListEvent =>
89-
s"""{"statusCode": 200, "vmID": "$vmID", "body": "${writeToString[List[TodoTask]](todoList.toList)}"}"""
86+
putDelta(mutatedList.deltaBuffer.head.deltaState)
9087

91-
case AddTaskEvent(desc) =>
92-
val task = TodoTask(desc)
88+
s"""{"statusCode": 200, "vmID": "$vmID", "body": "${task.id}"}"""
9389

94-
val mutatedList = todoList.prepend(task)
95-
todoList = mutatedList.resetDeltaBuffer()
96-
97-
putDelta(mutatedList.deltaBuffer.head.deltaState)
98-
99-
s"""{"statusCode": 200, "vmID": "$vmID", "body": "${task.id}"}"""
100-
101-
case ToggleTaskEvent(id) =>
102-
todoList.toList.find(_.id == id) match {
103-
case None =>
104-
s"""{"statusCode": 404, "vmID": "$vmID"}"""
105-
case Some(TodoTask(desc, done, _)) =>
106-
val mutatedList = todoList.updateBy(_.id == id, TodoTask(desc, done = !done, id))
107-
todoList = mutatedList.resetDeltaBuffer()
108-
109-
putDelta(mutatedList.deltaBuffer.head.deltaState)
90+
case ToggleTaskEvent(id) =>
91+
todoList.toList.find(_.id == id) match {
92+
case None =>
93+
s"""{"statusCode": 404, "vmID": "$vmID"}"""
94+
case Some(TodoTask(desc, done, _)) =>
95+
val mutatedList = todoList.updateBy(_.id == id, TodoTask(desc, done = !done, id))
96+
todoList = mutatedList.resetDeltaBuffer()
11097

111-
s"""{"statusCode": 200, "vmID": "$vmID"}"""
112-
}
98+
putDelta(mutatedList.deltaBuffer.head.deltaState)
11399

114-
case EditTaskEvent(id, desc) =>
115-
todoList.toList.find(_.id == id) match {
116-
case None =>
117-
s"""{"statusCode": 404, "vmID": "$vmID"}"""
118-
case Some(TodoTask(_, done, _)) =>
119-
val mutatedList = todoList.updateBy(_.id == id, TodoTask(desc, done, id))
120-
todoList = mutatedList.resetDeltaBuffer()
100+
s"""{"statusCode": 200, "vmID": "$vmID"}"""
101+
}
121102

122-
putDelta(mutatedList.deltaBuffer.head.deltaState)
103+
case EditTaskEvent(id, desc) =>
104+
todoList.toList.find(_.id == id) match {
105+
case None =>
106+
s"""{"statusCode": 404, "vmID": "$vmID"}"""
107+
case Some(TodoTask(_, done, _)) =>
108+
val mutatedList = todoList.updateBy(_.id == id, TodoTask(desc, done, id))
109+
todoList = mutatedList.resetDeltaBuffer()
123110

124-
s"""{"statusCode": 200, "vmID": "$vmID"}"""
125-
}
111+
putDelta(mutatedList.deltaBuffer.head.deltaState)
126112

127-
case RemoveTaskEvent(id) =>
128-
val mutatedList = todoList.deleteBy(_.id == id)
113+
s"""{"statusCode": 200, "vmID": "$vmID"}"""
114+
}
129115

130-
mutatedList.deltaBuffer.headOption match {
131-
case None =>
132-
s"""{"statusCode": 404, "vmID": "$vmID"}"""
133-
case Some(Delta(_, deltaState)) =>
134-
todoList = mutatedList.resetDeltaBuffer()
116+
case RemoveTaskEvent(id) =>
117+
val mutatedList = todoList.deleteBy(_.id == id)
135118

136-
putDelta(deltaState)
119+
mutatedList.deltaBuffer.headOption match {
120+
case None =>
121+
s"""{"statusCode": 404, "vmID": "$vmID"}"""
122+
case Some(Delta(_, deltaState)) =>
123+
todoList = mutatedList.resetDeltaBuffer()
137124

138-
s"""{"statusCode": 200, "vmID": "$vmID"}"""
139-
}
125+
putDelta(deltaState)
140126

141-
case RemoveDoneEvent =>
142-
val toRemove = todoList.toList.filter(_.done)
127+
s"""{"statusCode": 200, "vmID": "$vmID"}"""
128+
}
143129

144-
val mutatedList = todoList.deleteBy(toRemove.contains)
130+
case RemoveDoneEvent =>
131+
val toRemove = todoList.toList.filter(_.done)
145132

146-
if (mutatedList.deltaBuffer.nonEmpty) {
147-
putDelta(mutatedList.deltaBuffer.head.deltaState)
133+
val mutatedList = todoList.deleteBy(toRemove.contains)
148134

149-
todoList = mutatedList.resetDeltaBuffer()
150-
}
135+
if (mutatedList.deltaBuffer.nonEmpty) {
136+
putDelta(mutatedList.deltaBuffer.head.deltaState)
151137

152-
s"""{"statusCode": 200, "vmID": "$vmID"}"""
153-
}
138+
todoList = mutatedList.resetDeltaBuffer()
139+
}
154140

141+
s"""{"statusCode": 200, "vmID": "$vmID"}"""
142+
}
155143

156-
val req2 = HttpRequest.newBuilder().uri(
157-
URI.create(s"http://$apiurl/2018-06-01/runtime/invocation/$reqId/response")
158-
).method("POST", BodyPublishers.ofString(response)).build()
144+
val req2 = HttpRequest.newBuilder().uri(
145+
URI.create(s"http://$apiurl/2018-06-01/runtime/invocation/$reqId/response")
146+
).method("POST", BodyPublishers.ofString(response)).build()
159147

160-
client.send(req2, BodyHandlers.discarding())
161-
}
148+
client.send(req2, BodyHandlers.discarding())
162149
}
163150
}
164151

0 commit comments

Comments
 (0)