~oubiwann/txjsonrpc/main

« back to all changes in this revision

Viewing changes to examples/web2Auth/server.tac

  • Committer: Duncan McGreggor
  • Date: 2012-01-09 06:22:38 UTC
  • Revision ID: duncan@bluelibris.com-20120109062238-xspxa7a10sp19v7a
* Removed web2 examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
 
3
 
from twisted.application import service, internet
4
 
from twisted.cred.checkers import FilePasswordDB
5
 
from twisted.web2 import server
6
 
from twisted.web2.channel import http
7
 
 
8
 
from txjsonrpc.auth import wrapResource
9
 
from txjsonrpc.web2 import jsonrpc
10
 
 
11
 
 
12
 
class Example(jsonrpc.JSONRPC):
13
 
    """An example object to be published."""
14
 
 
15
 
    addSlash = True
16
 
 
17
 
    def jsonrpc_echo(self, request, x):
18
 
        """Return all passed args."""
19
 
        return x
20
 
 
21
 
    def jsonrpc_add(self, request, a, b):
22
 
        """Return sum of arguments."""
23
 
        return a + b
24
 
 
25
 
 
26
 
# Set up the application and the JSON-RPC resource.
27
 
application = service.Application("Example JSON-RPC Server")
28
 
root = Example()
29
 
 
30
 
# Define the credential checker the application will be using and wrap the
31
 
# JSON-RPC resource.
32
 
dirname = os.path.dirname(__file__)
33
 
checker = FilePasswordDB(os.path.join(dirname, "passwd.db"))
34
 
realmName = "My JSON-RPC App"
35
 
wrappedRoot = wrapResource(root, [checker], realmName=realmName)
36
 
 
37
 
# With the wrapped root, we can set up the server as usual.
38
 
site = server.Site(wrappedRoot)
39
 
chan = http.HTTPFactory(site)
40
 
jsonrpcServer = internet.TCPServer(7080, chan)
41
 
jsonrpcServer.setServiceParent(application)