Http Entrypoint

Until now, we were working with rpc with amqp which uses rpc as an entrypoint to the service method. We will change that pattern and will use http entrypoint this time.

Service Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# http_service.py

from nameko.web import handlers

class HttpService(object):
    name = 'http_service'

    @handlers.http('GET', '/')
    def get_method(self, request):
        return 'index'

    @handlers.http('POST', '/post')
    def post_method(self, request):
        return request.get_data(as_text=True)
line-1: imports handlers from web which contains http entrypoint.
line-3: Defines the service class.
line-4: declares the name of the service
line-6: defines http entrypoint with suitable http method and path.
line-7: service method to do real task.

Working

Working of http entrypoint is much simpler.

  1. nameko run the service code. It will start a process at port 8000.
  2. use curl -i localhost:8000/ to send a http request.
  3. This service being a GET request, get_method will process this request.
  4. It will send 'index' string as response with other http headers.