]> git.ipfire.org Git - people/ms/westferry.git/commitdiff
Create a basic, modular frontend web service
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 17 Oct 2015 21:43:14 +0000 (23:43 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 17 Oct 2015 22:09:24 +0000 (00:09 +0200)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
po/POTFILES.in
src/scripts/westferry.in
src/westferry/__init__.py
src/westferry/application.py [new file with mode: 0644]
src/westferry/handlers/__init__.py [new file with mode: 0644]
src/westferry/handlers/base.py [new file with mode: 0644]
src/westferry/handlers/index.py [new file with mode: 0644]
src/westferry/services.py [new file with mode: 0644]
src/westferry/ui/__init__.py [new file with mode: 0644]
src/westferry/ui/base.py [new file with mode: 0644]

index cd6aebd17de1ff6291c5de88a27e6e2dd8b551ba..edeaac94085c91135fbcbf777724fb5132c8ca41 100644 (file)
@@ -75,7 +75,9 @@ dist_configs_DATA = \
        westferry.conf.sample
 
 westferry_PYTHON = \
-       src/westferry/__init__.py
+       src/westferry/__init__.py \
+       src/westferry/application.py \
+       src/westferry/services.py
 
 westferrydir = $(pythondir)/westferry
 
@@ -84,6 +86,19 @@ westferry_backend_PYTHON = \
 
 westferry_backenddir = $(pythondir)/westferry/backend
 
+westferry_handlers_PYTHON = \
+       src/westferry/handlers/__init__.py \
+       src/westferry/handlers/base.py \
+       src/westferry/handlers/index.py
+
+westferry_handlersdir = $(pythondir)/westferry/handlers
+
+westferry_ui_PYTHON = \
+       src/westferry/ui/__init__.py \
+       src/westferry/ui/base.py
+
+westferry_uidir = $(pythondir)/westferry/ui
+
 # ------------------------------------------------------------------------------
 
 if ENABLE_MANPAGES
index ee12baa8a827a753e6cf84c3572648c80faff886..77f3c22575404abb08eb817acb6322770c895006 100644 (file)
@@ -1,2 +1,3 @@
 src/westferry/backend/__version__.py
+src/westferry/handlers/base.py
 src/westferry/__init__.py
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..aa23655027d6cc10fde25b62ced13f96df1d8ac1 100644 (file)
@@ -0,0 +1,25 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import westferry
+
+s = westferry.services.WebService()
+s.run()
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9bd1ba15a082eb47e252e0a1da9cbd7b060d4f0b 100644 (file)
@@ -0,0 +1,22 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+from . import services
diff --git a/src/westferry/application.py b/src/westferry/application.py
new file mode 100644 (file)
index 0000000..0caabdf
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import tornado.web
+
+from . import handlers
+from . import ui
+
+class WebApplication(tornado.web.Application):
+       def __init__(self, **kwargs):
+               settings = {
+                       # Enable compressed output
+                       "compress_response" : True,
+
+                       # Use Cross-Site-Request-Forgery protection
+                       "xsrf_cookies" : True,
+               }
+               settings.update(kwargs)
+
+               # Get handlers
+               h = [(h.url, h) for h in handlers.get_handlers()]
+
+               # Get UI modules and methods
+               settings.update({
+                       "ui_methods" : ui.get_ui_methods(),
+                       "ui_modules" : ui.get_ui_modules(),
+               })
+
+               # Initialise the web application
+               tornado.web.Application.__init__(self, handlers=h, **settings)
diff --git a/src/westferry/handlers/__init__.py b/src/westferry/handlers/__init__.py
new file mode 100644 (file)
index 0000000..a934bbd
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+from . import base
+from . import index
+
+def get_handlers():
+       """
+               Returns a list of all registered handlers
+       """
+       return base.HandlerRegistration.get_handlers()
diff --git a/src/westferry/handlers/base.py b/src/westferry/handlers/base.py
new file mode 100644 (file)
index 0000000..e3d2461
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import tornado.web
+
+_handlers = []
+
+class HandlerRegistration(type):
+       def __init__(handler, name, bases, dict):
+               type.__init__(handler, name, bases, dict)
+
+               # The main class from which is inherited is not registered
+               # as a plugin
+               if name.endswith("BaseHandler"):
+                       return
+
+               if handler.url is None:
+                       raise RuntimeError(_("Handler %s is improperly configured") % handler)
+
+               _handlers.append(handler)
+
+       @staticmethod
+       def get_handlers():
+               return _handlers
+
+
+class BaseHandler(tornado.web.RequestHandler, metaclass=HandlerRegistration):
+       url = None
diff --git a/src/westferry/handlers/index.py b/src/westferry/handlers/index.py
new file mode 100644 (file)
index 0000000..3edf7bc
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+from . import base
+
+class IndexHandler(base.BaseHandler):
+       url = r"/"
+
+       def get(self):
+               self.finish("Hello World")
diff --git a/src/westferry/services.py b/src/westferry/services.py
new file mode 100644 (file)
index 0000000..f459318
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import tornado.httpserver
+import tornado.ioloop
+
+from . import application
+
+class Service(object):
+       pass
+
+
+class WebService(Service):
+       def __init__(self, port=80):
+               self.port = port
+
+       def make_application(self, **kwargs):
+               return application.WebApplication(**kwargs)
+
+       @property
+       def ioloop(self):
+               return tornado.ioloop.IOLoop.instance()
+
+       def run(self, **kwargs):
+               app = self.make_application(**kwargs)
+
+               # Create a HTTP server instance
+               server = tornado.httpserver.HTTPServer(app)
+               server.bind(self.port)
+
+               # Launch the server
+               server.start()
+
+               # Launch the IOLoop
+               self.ioloop.start()
diff --git a/src/westferry/ui/__init__.py b/src/westferry/ui/__init__.py
new file mode 100644 (file)
index 0000000..a7641dd
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+from . import base
+
+def get_ui_methods():
+       """
+               Returns all registered UI methods.
+       """
+       return base.UIMethodsRegistration.get_ui_methods()
+
+def get_ui_modules():
+       """
+               Returns all registered UI modules.
+       """
+       return base.UIModulesRegistration.get_ui_modules()
diff --git a/src/westferry/ui/base.py b/src/westferry/ui/base.py
new file mode 100644 (file)
index 0000000..699d07d
--- /dev/null
@@ -0,0 +1,73 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Westferry - The IPFire web user interface                                   #
+# Copyright (C) 2015 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import tornado.web
+
+_ui_methods = {}
+_ui_modules = {}
+
+class UIMethodsRegistration(type):
+       def __init__(ui_method, name, bases, dict):
+               type.__init__(ui_method, name, bases, dict)
+
+               # The main class from which is inherited is not registered
+               # as a plugin
+               if name == "BaseUIMethod":
+                       return
+
+               if ui_method.handle is None:
+                       raise RuntimeError
+
+               _ui_methods[name] = ui_method()
+
+       @staticmethod
+       def get_ui_methods():
+               return _ui_methods
+
+
+class UIModulesRegistration(type):
+       def __init__(ui_module, name, bases, dict):
+               type.__init__(ui_module, name, bases, dict)
+
+               # The main class from which is inherited is not registered
+               # as a plugin
+               if name == "BaseUIModule":
+                       return
+
+               if name.endswith("Module"):
+                       name = name[:-6]
+
+               _ui_modules[name] = ui_module
+
+       @staticmethod
+       def get_ui_modules():
+               return _ui_modules
+
+
+class BaseUIMethod(object, metaclass=UIMethodsRegistration):
+       handle = None
+
+       def __call__(self, *args, **kwargs):
+               raise NotImplementedError
+
+
+class BaseUIModule(tornado.web.UIModule, metaclass=UIModulesRegistration):
+       pass