Mercurial > libervia-web
comparison libervia/server/server.py @ 1246:aaf28d45ae67
pages: browser code, first draft:
- code for browser can now be specified if a page subdirectory named `_browser` exists
- the default engine used is `Brython` (not yet implemented, so it will do nothing for
now)
- build_path now uses C.SITE_NAME_DEFAULT for default site instead of empty string, to
avoid collision with other sites
- ProtectedFile transtype path (first positional argument) to str, so a pathlib.Path can
be used
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 26 Apr 2020 22:01:13 +0200 |
parents | 079e8eb6e327 |
children | a1606e2a92eb |
comparison
equal
deleted
inserted
replaced
1245:079e8eb6e327 | 1246:aaf28d45ae67 |
---|---|
123 | 123 |
124 | 124 |
125 class ProtectedFile(static.File): | 125 class ProtectedFile(static.File): |
126 """A static.File class which doesn't show directory listing""" | 126 """A static.File class which doesn't show directory listing""" |
127 | 127 |
128 def __init__(self, *args, **kwargs): | 128 def __init__(self, path, *args, **kwargs): |
129 if "defaultType" not in kwargs and len(args) < 2: | 129 if "defaultType" not in kwargs and len(args) < 2: |
130 # defaultType is second positional argument, and Twisted uses it | 130 # defaultType is second positional argument, and Twisted uses it |
131 # in File.createSimilarFile, so we set kwargs only if it is missing | 131 # in File.createSimilarFile, so we set kwargs only if it is missing |
132 # in kwargs and it is not in a positional argument | 132 # in kwargs and it is not in a positional argument |
133 kwargs["defaultType"] = "application/octet-stream" | 133 kwargs["defaultType"] = "application/octet-stream" |
134 super(ProtectedFile, self).__init__(*args, **kwargs) | 134 super(ProtectedFile, self).__init__(str(path), *args, **kwargs) |
135 | 135 |
136 def directoryListing(self): | 136 def directoryListing(self): |
137 return web_resource.NoResource() | 137 return web_resource.NoResource() |
138 | 138 |
139 | 139 |
157 self.host = host | 157 self.host = host |
158 self.host_name = host_name | 158 self.host_name = host_name |
159 self.site_name = site_name | 159 self.site_name = site_name |
160 self.site_path = site_path | 160 self.site_path = site_path |
161 self.named_pages = {} | 161 self.named_pages = {} |
162 self.browser_modules = {} | |
162 self.uri_callbacks = {} | 163 self.uri_callbacks = {} |
163 self.pages_redirects = {} | 164 self.pages_redirects = {} |
164 self.cached_urls = {} | 165 self.cached_urls = {} |
165 self.main_menu = None | 166 self.main_menu = None |
167 build_path = host.getBuildPath(site_name) | |
168 build_path.mkdir(parents=True, exist_ok=True) | |
169 self.putChild( | |
170 C.BUILD_DIR.encode(), | |
171 ProtectedFile( | |
172 build_path, | |
173 defaultType="application/octet-stream"), | |
174 ) | |
166 | 175 |
167 def __str__(self): | 176 def __str__(self): |
168 return ("Root resource for {host_name} using {site_name} at {site_path} and " | 177 return ("Root resource for {host_name} using {site_name} at {site_path} and " |
169 "deserving files at {path}".format( | 178 "deserving files at {path}".format( |
170 host_name=self.host_name, site_name=self.site_name, | 179 host_name=self.host_name, site_name=self.site_name, |
753 self.vhost_root = vhost.NameVirtualHost() | 762 self.vhost_root = vhost.NameVirtualHost() |
754 default_site_path = os.path.abspath(os.path.dirname(libervia.__file__)) | 763 default_site_path = os.path.abspath(os.path.dirname(libervia.__file__)) |
755 # self.sat_root is official Libervia site | 764 # self.sat_root is official Libervia site |
756 root_path = os.path.join(default_site_path, C.TEMPLATE_STATIC_DIR) | 765 root_path = os.path.join(default_site_path, C.TEMPLATE_STATIC_DIR) |
757 self.sat_root = default_root = LiberviaRootResource( | 766 self.sat_root = default_root = LiberviaRootResource( |
758 host=self, host_name='', site_name='', site_path=default_site_path, | 767 host=self, host_name='', site_name='', |
759 path=root_path) | 768 site_path=default_site_path, path=root_path) |
760 if self.options['dev_mode']: | 769 if self.options['dev_mode']: |
761 self.files_watcher.watchDir( | 770 self.files_watcher.watchDir( |
762 default_site_path, auto_add=True, recursive=True, | 771 default_site_path, auto_add=True, recursive=True, |
763 callback=LiberviaPage.onFileChange, site_root=self.sat_root, | 772 callback=LiberviaPage.onFileChange, site_root=self.sat_root, |
764 site_path=default_site_path) | 773 site_path=default_site_path) |
770 self.sat_root._setMenu(self.options["menu_json"]) | 779 self.sat_root._setMenu(self.options["menu_json"]) |
771 self.vhost_root.default = default_root | 780 self.vhost_root.default = default_root |
772 existing_vhosts = {b'': default_root} | 781 existing_vhosts = {b'': default_root} |
773 | 782 |
774 for host_name, site_name in self.options["vhosts_dict"].items(): | 783 for host_name, site_name in self.options["vhosts_dict"].items(): |
784 if site_name == C.SITE_NAME_DEFAULT: | |
785 raise ValueError( | |
786 f"{C.DEFAULT_SITE_NAME} is reserved and can't be used in vhosts_dict") | |
775 encoded_site_name = site_name.encode('utf-8') | 787 encoded_site_name = site_name.encode('utf-8') |
776 try: | 788 try: |
777 site_path = self.renderer.sites_paths[site_name] | 789 site_path = self.renderer.sites_paths[site_name] |
778 except KeyError: | 790 except KeyError: |
779 log.warning(_( | 791 log.warning(_( |
803 if self.options['dev_mode']: | 815 if self.options['dev_mode']: |
804 self.files_watcher.watchDir( | 816 self.files_watcher.watchDir( |
805 site_path, auto_add=True, recursive=True, | 817 site_path, auto_add=True, recursive=True, |
806 callback=LiberviaPage.onFileChange, site_root=res, | 818 callback=LiberviaPage.onFileChange, site_root=res, |
807 site_path=site_path) | 819 site_path=site_path) |
808 res.putChild( | |
809 C.BUILD_DIR.encode('utf-8'), | |
810 ProtectedFile( | |
811 self.getBuildPath(site_name), | |
812 defaultType="application/octet-stream"), | |
813 ) | |
814 | 820 |
815 LiberviaPage.importPages(self, res) | 821 LiberviaPage.importPages(self, res) |
816 # FIXME: default pages are accessible if not overriden by external website | 822 # FIXME: default pages are accessible if not overriden by external website |
817 # while necessary for login or re-using existing pages | 823 # while necessary for login or re-using existing pages |
818 # we may want to disable access to the page by direct URL | 824 # we may want to disable access to the page by direct URL |
1275 | 1281 |
1276 def getBuildPath(self, site_name): | 1282 def getBuildPath(self, site_name): |
1277 """Generate build path for a given site name | 1283 """Generate build path for a given site name |
1278 | 1284 |
1279 @param site_name(unicode): name of the site | 1285 @param site_name(unicode): name of the site |
1280 @return (unicode): path to the build directory | 1286 @return (Path): path to the build directory |
1281 """ | 1287 """ |
1282 build_path_elts = [ | 1288 build_path_elts = [ |
1283 config.getConfig(self.main_conf, "", "local_dir"), | 1289 config.getConfig(self.main_conf, "", "local_dir"), |
1284 C.CACHE_DIR, | 1290 C.CACHE_DIR, |
1285 C.LIBERVIA_CACHE, | 1291 C.LIBERVIA_CACHE, |
1286 regex.pathEscape(site_name)] | 1292 regex.pathEscape(site_name or C.SITE_NAME_DEFAULT)] |
1287 build_path = "/".join(build_path_elts) | 1293 build_path = Path("/".join(build_path_elts)) |
1288 return os.path.abspath(os.path.expanduser(build_path)) | 1294 return build_path.expanduser().resolve() |
1289 | 1295 |
1290 def getExtBaseURLData(self, request): | 1296 def getExtBaseURLData(self, request): |
1291 """Retrieve external base URL Data | 1297 """Retrieve external base URL Data |
1292 | 1298 |
1293 this method tried to retrieve the base URL found by external user | 1299 this method tried to retrieve the base URL found by external user |