Mercurial > libervia-web
annotate src/twisted/plugins/libervia_server.py @ 826:70939916dc80
server (blog): added unhandler errback in getItemById
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 08 Jan 2016 19:50:40 +0100 |
parents | f8a7a046ff9c |
children | fd438e51bda8 |
rev | line source |
---|---|
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 #!/usr/bin/python |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
818 | 5 # Copyright (C) 2013-2016 Jérôme Poisson <goffi@goffi.org> |
6 # Copyright (C) 2013-2016 Adrien Cossa <souliane@mailoo.org> | |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
7 # Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
9 # This program is free software: you can redistribute it and/or modify |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
10 # it under the terms of the GNU Affero General Public License as published by |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
11 # the Free Software Foundation, either version 3 of the License, or |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
12 # (at your option) any later version. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
14 # This program is distributed in the hope that it will be useful, |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
17 # GNU Affero General Public License for more details. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
19 # You should have received a copy of the GNU Affero General Public License |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
22 from twisted.internet import defer |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
23 if defer.Deferred.debug: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
24 # if we are in debug mode, we want to use ipdb instead of pdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
25 try: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
26 import ipdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
27 import pdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
28 pdb.set_trace = ipdb.set_trace |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
29 pdb.post_mortem = ipdb.post_mortem |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
30 except ImportError: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
31 pass |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
32 |
514 | 33 import os.path |
34 | |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
35 from libervia.server.constants import Const as C |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
36 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
37 from sat.core.i18n import _ |
811 | 38 from sat.tools import config |
438
582c435dab6b
server side: new log system is used
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
39 |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 from zope.interface import implements |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 from twisted.python import usage |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 from twisted.plugin import IPlugin |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 from twisted.application.service import IServiceMaker |
811 | 45 import ConfigParser |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 |
811 | 47 |
48 CONFIG_SECTION = C.APP_NAME.lower() | |
514 | 49 |
50 | |
51 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS | |
52 allowed_values = ('http', 'https', 'both') | |
53 if value not in allowed_values: | |
54 raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)}) | |
55 return value | |
56 | |
57 def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS | |
58 html = os.path.join(value, C.HTML_DIR) | |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
59 if not os.path.isfile(os.path.join(html, C.LIBERVIA_MAIN_PAGE)): |
514 | 60 raise ValueError("%s is not a Libervia's browser HTML directory" % os.path.realpath(html)) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
61 themes_dir = os.path.join(value, C.THEMES_DIR) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
704
diff
changeset
|
62 if not os.path.isfile(os.path.join(themes_dir, 'default/styles/blog.css')): |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
63 raise ValueError("%s is not a Libervia's server data directory" % os.path.realpath(themes_dir)) |
514 | 64 return value |
65 | |
66 DATA_DIR_DEFAULT = '' | |
67 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType], | |
68 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int], | |
69 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int], | |
70 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int], | |
813
6e27604ec95a
server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
71 ['tls_private_key', '', '', _(u'TLS certificate private key (PEM format)').encode('utf-8'), str], |
814
e8c0c2b5e2d5
server: minor option help string correction
Goffi <goffi@goffi.org>
parents:
813
diff
changeset
|
72 ['tls_certificate', 'c', 'libervia.pem', _(u'TLS public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), str], |
813
6e27604ec95a
server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
73 ['tls_chain', '', '', _(u'TLS certificate intermediate chain (PEM format)').encode('utf-8'), str], |
514 | 74 ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int], |
75 ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int], | |
76 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], | |
77 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], | |
78 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html | |
79 OPT_PARAMETERS_CFG = [['empty_password_allowed_warning_dangerous_list', None, '', None]] # Options which are in sat.conf only | |
80 | |
81 def initialise(options): | |
82 """Method to initialise global modules""" | |
83 from twisted.internet import glib2reactor | |
84 glib2reactor.install() | |
85 # XXX: We need to configure logs before any log method is used, so here is the best place. | |
86 from sat.core import log_config | |
87 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C, backend_data=options) | |
88 from libervia.server import server | |
89 # we can't import this file from libervia.server.server because it's not a true module | |
90 # (there is no __init__.py file, as required by twistd plugin system), so we set the | |
91 # global values from here | |
92 server.DATA_DIR_DEFAULT = DATA_DIR_DEFAULT | |
93 server.OPT_PARAMETERS_BOTH = OPT_PARAMETERS_BOTH | |
94 server.OPT_PARAMETERS_CFG = OPT_PARAMETERS_CFG | |
95 server.coerceDataDir = coerceDataDir | |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 class Options(usage.Options): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
99 # optArgs is not really useful in our case, we need more than a flag |
514 | 100 optParameters = OPT_PARAMETERS_BOTH |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
101 |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
102 def __init__(self): |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
103 """Read SàT configuration file in order to overwrite the hard-coded default values. |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
104 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
105 Priority for the usage of the values is (from lowest to highest): |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
106 - hard-coded default values |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
107 - values from SàT configuration files |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
108 - values passed on the command line |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
109 """ |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
110 # If we do it the reading later: after the command line options have been parsed, there's no good way to know |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
111 # if the options values are the hard-coded ones or if they have been passed on the command line. |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
112 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
113 # FIXME: must be refactored + code can be factorised with backend |
811 | 114 config_parser = ConfigParser.SafeConfigParser() |
115 config_parser.read(C.CONFIG_FILES) | |
116 self.handleDeprecated(config_parser) | |
514 | 117 for param in self.optParameters + OPT_PARAMETERS_CFG: |
432
8ecc5a7062e4
browser and server sides: fixes module import + use enumerate instead of xrange
souliane <souliane@mailoo.org>
parents:
421
diff
changeset
|
118 name = param[0] |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
119 try: |
811 | 120 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception) |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
121 try: |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
122 param[2] = param[4](value) |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
123 except IndexError: # the coerce method is optional |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
124 param[2] = value |
811 | 125 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
126 pass |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
127 usage.Options.__init__(self) |
817
cf1812a4445e
server: fixed empty_password_allowed_warning_dangerous_list option
Goffi <goffi@goffi.org>
parents:
814
diff
changeset
|
128 for opt_data in OPT_PARAMETERS_CFG: |
cf1812a4445e
server: fixed empty_password_allowed_warning_dangerous_list option
Goffi <goffi@goffi.org>
parents:
814
diff
changeset
|
129 self[opt_data[0]] = opt_data[2] |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 |
811 | 131 def handleDeprecated(self, config_parser): |
132 """display warning and/or change option when a deprecated option if found | |
133 | |
134 param config_parser(ConfigParser): read ConfigParser instance for sat.conf | |
135 """ | |
136 replacements = (('ssl_certificate', 'tls_certificate'),) | |
137 for old, new in replacements: | |
138 try: | |
139 value = config.getConfig(config_parser, CONFIG_SECTION, old, Exception) | |
140 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): | |
141 pass | |
142 else: | |
143 print u"\n/!\\ Use of {old} is deprecated, please use {new} instead\n".format(old=old, new=new) | |
144 config_parser.set(CONFIG_SECTION, new, value) | |
145 | |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
146 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 class LiberviaMaker(object): |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
148 implements(IServiceMaker, IPlugin) |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
149 |
512
750db9ff8525
server side: launching script improvments:
Goffi <goffi@goffi.org>
parents:
474
diff
changeset
|
150 tapname = C.APP_NAME_FILE |
514 | 151 description = _(u'The web frontend of Salut à Toi') |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 options = Options |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
154 def makeService(self, options): |
514 | 155 initialise(options.parent) |
156 from libervia.server import server | |
810 | 157 return server.Libervia(options) |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
158 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
159 |
421
39b07289ff42
server_side: added parameter port_https_ext (external port for HTTPS, used for example for the redirection)
souliane <souliane@mailoo.org>
parents:
417
diff
changeset
|
160 # affectation to some variable is necessary for twisted introspection to work |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
161 serviceMaker = LiberviaMaker() |