Usage Example¶
Basic Usage Example¶
#!/usr/bin/env python
# encoding: utf-8
# Copyright (C) 2015 Chintalagiri Shashank
# Released under the MIT license.
"""
Simple Deployment Example
-------------------------
"""
from prefab_server.serve import get_resource
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import logging
logging.basicConfig(level=logging.DEBUG)
SERVER_PORT = 1081
if __name__ == '__main__':
root = Resource()
prefab_resource = get_resource(root)
factory = Site(root)
reactor.listenTCP(SERVER_PORT, factory)
reactor.run()
Self-contained Usage : Deploy using twistd and systemd¶
Twisted .tac
file:
"""
TAC Deployment Example
----------------------
A more usable example.
The important part of this, the part that makes it a .tac file, is
the final root-level section, which sets up the object called
'application' which twistd will look for.
You can run this .tac file directly with ::
twistd -ny prefab_server.tac
"""
# import sys
# from twisted.python import log
# log.startLogging(sys.stdout)
from twisted.application import service
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import LogFile
# this is the core part of any tac file, the creation of the root-level
# application object
application = service.Application("Tendril Prefab Server")
logfile = LogFile("prefab_server.log", "/var/log/tendril")
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
# attach the service to its parent application
from prefab_server.serve import get_service
service = get_service()
service.setServiceParent(application)
Systemd .service
file:
[Unit]
Description=Tendril FS Server
[Service]
ExecStart=/usr/bin/twistd --nodaemon --pidfile= fs_server.tac
WorkingDirectory=/home/tendril/tools/fs_server/deploy
User=tendril
Group=tendril
Restart=always
[Install]
WantedBy=multi-user.target