示例#1
0
class MicroApp(App):
    """Puts a micro framework (see flask, sinatra etc.) on top of the Webserver framework.

    Usage:
    app = MicroApp()

    @app.get('')
    def index(request, response, pathmatch):
        response.send(body="Hey!")

    @app.put('thing/(?P<thing_id>)')
    def update_thing(request, response, pathmatch):
        thing = model.find_thing(pathmatch.group('thing_id'))
        thing.foo = request.params['foo']
        thing.store()
        response.send_redirect('/')

    @app.route('info')
    def info(request, response, pathmatch):
        response.send(body="Just for info")

    @app.route('info', methods=['PUT', 'POST'])
    def info(request, response, pathmatch):
        response.send(body="Just for info")
    """

    def __init__(self, **kwargs):
        self.server = Webserver()
        super().__init__(**kwargs)

        # add middlewares and other apps here...
        self.server.add_app(StaticApp(prefix='static', path='static'))
        self.server.add_app(self)

    def get(self, url):
        """Decorator function for GET routes that maps to the route method."""
        return self.route(url, ['GET'])

    def post(self, url):
        """Decorator function for GET routes that maps to the route method."""
        return self.route(url, ['POST'])

    def put(self, url):
        """Decorator function for GET routes that maps to the route method."""
        return self.route(url, ['PUT'])

    def delete(self, url):
        """Decorator function for GET routes that maps to the route method."""
        return self.route(url, ['DELETE'])

    def route(self, url, methods=None):
        """Decorator function with arguments. Returns a decorator function that takes a function and wraps it or
           has some side effetcs (like registering the route)."""
        def wrap(f):
            self.add_route(url, f, methods=methods)
            return f
        return wrap

    def serve(self):
        self.server.serve()
示例#2
0
            else:
                return response.send_template(
                    'login.tmpl',
                    {'message': 'Wrong username or password. Try again.'})
        # send login form
        return response.send_template(
            'login.tmpl', {'user': server.usermodel.AnonymousUser()})


if __name__ == '__main__':

    db = sqlite.connect('minitwitter.sqlite')  # use sqlite db
    db.row_factory = sqlite.Row  # fetch rows as Row objects (easier to access)

    s = Webserver()
    s.set_templating("jinja2")
    s.set_templating_path("templates.jinja2")

    s.add_middleware(SessionMiddleware())
    s.add_middleware(CsrfMiddleware())

    s.add_app(UsermanagementApp(db_connection=db)
              )  # Sub-App: create, change, delete users. (code in server/apps)
    s.add_app(StaticApp(prefix='static',
                        path='static'))  # deliver static files

    s.add_app(MiniTwitterApp('data', db_connection=db))  # the twitter app

    log(0, "Server running.")
    s.serve()
示例#3
0
from server.webserver import Webserver, App


class DummyApp(App):
    """A very silly simple App."""
    def register_routes(self):
        self.add_route("", self.dummy)

    def dummy(self, request, response, pathmatch):
        response.send(200, body="Huhuh, ich bin ein dummy.")

server = Webserver(8080)  # Initialize webserver on port 8080
server.add_app(DummyApp())  # register DummyApp without prefix
server.serve()  # listen for connections and serve
示例#4
0
            for (url, values) in hitlist:
                msg += """<h3><a href="{link}">{title}</a></h3>
                         <p><a href="{link}">{link}</a></p>
                         <p>{teaser}<p>""".format(
                    link=url,
                    title=self.store.pages[url]['title'],
                    teaser=self.store.get_teaser(url, q))
        response.send_template('templates/search/search.tmpl', {
            'q': q,
            'netloc': self.store.netloc,
            'msg': msg
        })


if __name__ == '__main__':
    # entry = 'https://www.informatik.uni-osnabrueck.de'
    # entry = 'https://www.uni-osnabrueck.de'
    # entry = 'https://www.virtuos.uni-osnabrueck.de'
    entry = 'http://vm009.rz.uos.de'

    s = load_store(entry)
    if not s:
        s = Store(entry)
        c = Crawler(s)
        c.crawl()
        s.save()

    w = Webserver()
    w.add_app(SearchApp(s, prefix=''))
    w.serve()
    webbrowser.open_new_tab("http://localhost:8080/")