summaryrefslogtreecommitdiff
path: root/pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-11-23 17:05:53 +0100
committerEugen Wissner <belka@caraus.de>2025-11-23 17:05:53 +0100
commitf3b3d4b1a26eba872c32ad95fc112650011b625c (patch)
tree483c90b08fb640f9f96b83393f64af9b7e752984 /pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st
parentbf11813e4fa859a4833cab226c4ea560765d6d77 (diff)
downloadbook-exercises-f3b3d4b1a26eba872c32ad95fc112650011b625c.tar.gz
Add the tiny-chat project from the Pharo MOOC
Diffstat (limited to 'pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st')
-rw-r--r--pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st75
1 files changed, 75 insertions, 0 deletions
diff --git a/pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st b/pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st
new file mode 100644
index 0000000..cda06a0
--- /dev/null
+++ b/pharo-mooc/tiny-chat/src/TinyChat-server/TCServer.class.st
@@ -0,0 +1,75 @@
+Class {
+ #name : 'TCServer',
+ #superclass : 'Object',
+ #instVars : [
+ 'teapotServer',
+ 'messagesQueue'
+ ],
+ #category : 'TinyChat-server',
+ #package : 'TinyChat-server'
+}
+
+{ #category : 'initialization' }
+TCServer class >> startOn: aPortNumber [
+ ^self new
+ initializePort: aPortNumber;
+ registerRoutes;
+ registerErrorHandlers;
+ yourself
+]
+
+{ #category : 'initialization' }
+TCServer class >> stopAll [
+ self allInstancesDo: #stop
+]
+
+{ #category : 'initialization' }
+TCServer >> addMessage: aRequest [
+ messagesQueue add: (TCMessage from: (aRequest at: #sender) text: (aRequest at: #text)).
+]
+
+{ #category : 'initialization' }
+TCServer >> initialize [
+ super initialize.
+ messagesQueue := TCMessageQueue new
+]
+
+{ #category : 'initialization' }
+TCServer >> initializePort: anInteger [
+ teapotServer := Teapot configure: {
+ #defaultOutput -> #text.
+ #port -> anInteger.
+ #debugMode -> true
+ }.
+ teapotServer start
+]
+
+{ #category : 'initialization' }
+TCServer >> messageCount [
+ ^ messagesQueue size
+]
+
+{ #category : 'initialization' }
+TCServer >> messagesFrom: request [
+ ^ messagesQueue formattedMessagesFrom: (request at: #id)
+]
+
+{ #category : 'initialization' }
+TCServer >> registerErrorHandlers [
+ teapotServer
+ exception: KeyNotFound -> (TeaResponse notFound body: 'No such message')
+]
+
+{ #category : 'initialization' }
+TCServer >> registerRoutes [
+ teapotServer
+ GET: '/messages/count' -> (Send message: #messageCount to: self);
+ GET: '/messages/<id:IsInteger>' -> (Send message: #messagesFrom: to: self);
+ POST: '/messages/add' -> (Send message: #addMessage: to: self)
+]
+
+{ #category : 'initialization' }
+TCServer >> stop [
+ teapotServer stop.
+ messagesQueue reset
+]