"""
serve remote wiki pages

run with twistd -y remote.tac
access the remote wiki on localhost:8080

"""

remote_wiki = 'http://moinmoin.wikiwikiweb.de'

from twisted.application import internet, service
from twisted.protocols import http
from twisted.web import client

class RemoteRequest(http.Request):
    def process(self):
        """ Process request """
        page = self.channel.factory.getPage(self.uri)
        page.addErrback(lambda e: "Internal server error")
        page.addCallback(lambda m: (self.write(m), self.finish()))
    
class RemoteChannel(http.HTTPChannel):
    """ Create request for each connection """   
    requestFactory = RemoteRequest
    
class RemoteFactory(http.HTTPFactory):
    protocol = RemoteChannel
    def __init__(self, remote):
        self.remote = remote
        
    def getPage(self, uri):
        """ Get page from remote wiki """
        return client.getPage(self.remote + uri)

application = service.Application('remote', uid=1, gid=1)
factory = RemoteFactory(remote_wiki)
internet.TCPServer(8080, factory).setServiceParent(application)

