|
| 1 | +# CONFIG FILE LOCATION |
| 2 | +# relative to this file or absolute path |
| 3 | + |
| 4 | +config_file = '/path/to/config/pyapns_conf.json' |
| 5 | + |
| 6 | +# you don't need to change anything below this line really |
| 7 | + |
| 8 | +import twisted.application, twisted.web, twisted.application.internet |
| 9 | +import pyapns.server, pyapns._json |
| 10 | +import pyapns.rest_service, pyapns.model |
| 11 | +import os |
| 12 | + |
| 13 | +config = {} |
| 14 | + |
| 15 | +if os.path.exists(os.path.abspath(config_file)): |
| 16 | + with open(os.path.abspath(config_file)) as f: |
| 17 | + config.update(pyapns._json.loads(f.read())) |
| 18 | +else: |
| 19 | + print 'No config file loaded. Alter the `config_file` variable at', \ |
| 20 | + 'the top of this file to set one.' |
| 21 | + |
| 22 | +xml_service = pyapns.server.APNSServer() |
| 23 | + |
| 24 | +# get automatic provisioning |
| 25 | +if 'autoprovision' in config: |
| 26 | + for app in config['autoprovision']: |
| 27 | + # for XML-RPC |
| 28 | + xml_service.xmlrpc_provision(app['app_id'], app['cert'], |
| 29 | + app['environment'], app['timeout']) |
| 30 | + # for REST |
| 31 | + pyapns.model.AppRegistry.put( |
| 32 | + app['app_id'], app['environment'], app['cert'], |
| 33 | + timeout=app['timeout'] |
| 34 | + ) |
| 35 | + |
| 36 | +application = twisted.application.service.Application("pyapns application") |
| 37 | + |
| 38 | +# XML-RPC server support ------------------------------------------------------ |
| 39 | + |
| 40 | +if 'port' in config: |
| 41 | + port = config['port'] |
| 42 | +else: |
| 43 | + port = 7077 |
| 44 | + |
| 45 | +resource = twisted.web.resource.Resource() |
| 46 | +resource.putChild('', xml_service) |
| 47 | + |
| 48 | +site = twisted.web.server.Site(resource) |
| 49 | + |
| 50 | +server = twisted.application.internet.TCPServer(port, site) |
| 51 | +server.setServiceParent(application) |
| 52 | + |
| 53 | +# rest service support -------------------------------------------------------- |
| 54 | +if 'rest_port' in config: |
| 55 | + rest_port = config['rest_port'] |
| 56 | +else: |
| 57 | + rest_port = 8088 |
| 58 | + |
| 59 | +site = twisted.web.server.Site(pyapns.rest_service.default_resource) |
| 60 | + |
| 61 | +server = twisted.application.internet.TCPServer(rest_port, site) |
| 62 | +server.setServiceParent(application) |
0 commit comments