comparison src/server/server.py @ 514:530c88c1deee

server_side: plugin refactoring: twisted.plugins.libervia_server is refactored for the same reason as for SàT backend (see SàT commit message for revision adea30ca0b51), and there is an additionnal trick: as we need to use some variables in both twisted.plugins.libervia_server and server.server, we can't import server.server before initialise() is called, and we can't neigher import twisted.plugins.libervia_server from server.server (there is no __init__.py file as requested by Twisted plugin systeme), these variable have been moved from server.server to twisted.plugins.libervia_server, and are set in server.server from the latter. This not super clean to read, but it solve the import order issues.
author Goffi <goffi@goffi.org>
date Mon, 25 Aug 2014 17:35:41 +0200
parents e588335b6aa8
children d58d4dd0cefe
comparison
equal deleted inserted replaced
513:ecec2cc13b33 514:530c88c1deee
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from twisted.application import service 20 from twisted.application import service
21 from twisted.internet import glib2reactor
22 glib2reactor.install()
23 from twisted.internet import reactor, defer 21 from twisted.internet import reactor, defer
24 from twisted.web import server 22 from twisted.web import server
25 from twisted.web.static import File 23 from twisted.web.static import File
26 from twisted.web.resource import Resource, NoResource 24 from twisted.web.resource import Resource, NoResource
27 from twisted.web.util import Redirect, redirectTo 25 from twisted.web.util import Redirect, redirectTo
57 ssl_available = False 55 ssl_available = False
58 56
59 from libervia.server.constants import Const as C 57 from libervia.server.constants import Const as C
60 from libervia.server.blog import MicroBlog 58 from libervia.server.blog import MicroBlog
61 59
60 # following value are set from twisted.plugins.libervia_server initialise (see the comment there)
61 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None
62 62
63 class ISATSession(Interface): 63 class ISATSession(Interface):
64 profile = Attribute("Sat profile") 64 profile = Attribute("Sat profile")
65 jid = Attribute("JID associated with the profile") 65 jid = Attribute("JID associated with the profile")
66 66
988 """ 988 """
989 profile = ISATSession(request.getSession()).profile 989 profile = ISATSession(request.getSession()).profile
990 return ("setAvatar", filepath, profile) 990 return ("setAvatar", filepath, profile)
991 991
992 992
993 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS
994 allowed_values = ('http', 'https', 'both')
995 if value not in allowed_values:
996 raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)})
997 return value
998
999
1000 def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS
1001 html = os.path.join(value, C.HTML_DIR)
1002 if not os.path.isfile(os.path.join(html, 'libervia.html')):
1003 raise ValueError("%s is not a Libervia's browser HTML directory" % os.path.realpath(html))
1004 server_css = os.path.join(value, C.SERVER_CSS_DIR)
1005 if not os.path.isfile(os.path.join(server_css, 'blog.css')):
1006 raise ValueError("%s is not a Libervia's server data directory" % os.path.realpath(server_css))
1007 return value
1008 993
1009 994
1010 class Libervia(service.Service): 995 class Libervia(service.Service):
1011 996
1012 DATA_DIR_DEFAULT = ''
1013 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType],
1014 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int],
1015 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int],
1016 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int],
1017 ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.').encode('utf-8'), str],
1018 ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int],
1019 ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int],
1020 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str],
1021 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir],
1022 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html
1023 OPT_PARAMETERS_CFG = [['empty_password_allowed_warning_dangerous_list', None, '', None]] # Options which are in sat.conf only
1024 997
1025 def __init__(self, *args, **kwargs): 998 def __init__(self, *args, **kwargs):
1026 self.initialised = defer.Deferred() 999 self.initialised = defer.Deferred()
1027 1000
1028 # options managing 1001 # options managing
1029 for opt in self.OPT_PARAMETERS_BOTH + self.OPT_PARAMETERS_CFG: 1002 for opt in OPT_PARAMETERS_BOTH + OPT_PARAMETERS_CFG:
1030 opt_name = opt[0] 1003 opt_name = opt[0]
1031 setattr(self, opt_name, kwargs.get(opt_name, opt[2])) 1004 setattr(self, opt_name, kwargs.get(opt_name, opt[2]))
1032 if not self.port_https_ext: 1005 if not self.port_https_ext:
1033 self.port_https_ext = self.port_https 1006 self.port_https_ext = self.port_https
1034 if self.data_dir == Libervia.DATA_DIR_DEFAULT: 1007 if self.data_dir == DATA_DIR_DEFAULT:
1035 coerceDataDir(self.data_dir) # this is not done when using the default value 1008 coerceDataDir(self.data_dir) # this is not done when using the default value
1036 1009
1037 self.html_dir = os.path.join(self.data_dir, C.HTML_DIR) 1010 self.html_dir = os.path.join(self.data_dir, C.HTML_DIR)
1038 self.server_css_dir = os.path.join(self.data_dir, C.SERVER_CSS_DIR) 1011 self.server_css_dir = os.path.join(self.data_dir, C.SERVER_CSS_DIR)
1039 1012