Skip to content

Latest commit

 

History

History
46 lines (39 loc) · 1.14 KB

tryOnTeapot.md

File metadata and controls

46 lines (39 loc) · 1.14 KB

Install Teapot

Metacello new
  baseline: 'Teapot';
  repository: 'github://zeroflag/Teapot:v2.7.0/source';
  load.

Configure the server

"Configure Teapot defaults"
teapot := Teapot configure: {
		#port -> 9090.
		#debugMode -> true.
		#defaultOutput -> #html
	}.

"Add an exception handler to fail gracefully"
teapot	 exception: Error ->  [ :ex :req | | content |
	content := (Smalltalk isHeadless and: [ Smalltalk isInteractiveGraphic ])
		ifTrue: [ 'Ouch: {1}' format: { ex messageText } ]
		ifFalse: [ 'Internal error' ].
		TeaResponse serverError body: content.
	].

Setup routes

"Add a ping route"
teapot GET: '/ping' -> 'pong'.

"Create an SSTemplate"
sstEcho := STTemplate on: '<% [ | echo | echo := self allButFirst %><h1>Echoing:</h1><h2><%= self %></h2><h3><%= echo %></h3><p><%= echo %></p> <% ] value %>'.

"Add a echo route that will render the HTML the template based on the received message value."
teapot GET: '/echo/<message>' -> [ :req | sstEcho renderOn: (req at: #message) ].

Operation

"Start the HTTP server."
teapot start.

"Stop the HTTP server."
teapot stop.