73177a1a842529f1ef61731ee763a810ce30c765
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2015 Guilherme Mansur<guilhermerpmansur@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Hyper Text Coffee Pot Control Protocol
19 # A server that implements HTCPCP. At the moment there are no additions.
24 # Nitcorn Action used to answer requests.
34 redef fun answer
(http_request
, turi
) do
36 var method
= http_request
.method
37 var response
: HttpResponse
40 response
= new HttpResponse(418)
41 response
.body
= "I'm a teapot!\n"
42 response
.header
["Content-Type"] = "text"
46 if method
== "POST" or method
== "BREW" then
49 response
= new HttpResponse(400)
51 message
= "Brewing a new pot of coffee\n"
53 response
= new HttpResponse(200)
55 else if method
== "WHEN" then
57 message
= "Stopped adding milk, your coffee is ready!\n"
59 response
= new HttpResponse(200)
61 message
= "There is no coffee brewing!\n"
62 response
= new HttpResponse(405)
64 else if method
== "PROPFIND" or method
== "GET" then
66 message
= "The pot is busy\n"
68 message
= "The pot is ready to brew more coffee\n"
70 response
= new HttpResponse(200)
72 message
= "Unknown method: {method}"
74 response
= new HttpResponse(405)
77 response
.header
["Content-Type"] = "text"
78 response
.body
= message
92 var vh
= new VirtualHost("localhost:{port}")
93 vh
.routes
.add
new Route("/", new HTCPCPAction)
94 var factory
= new HttpFactory.and_libevent
95 factory
.config
.virtual_hosts
.add vh
96 print
"Nit4Coffee is now running at port: {port}"
101 var server
= new HTCPCServer(1227)