Commit bd55af84 authored by Gero Müller's avatar Gero Müller
Browse files

fix url, cleanup server

parent 526de8cb
......@@ -19,8 +19,8 @@ use_forgot = False
#base.static: http://localhost:8080/
[websockets]
use_websockets = False
secure_websockets = False
enabled = False
secure = False
[executable]
# pxlrun command options, call of executable will be:
......
......@@ -6,9 +6,7 @@ import vispa
from vispa.server import Server
def parse_commandline():
# setup the option parser
parser = OptionParser()
# basedir
parser.add_option("-b", "--base-dir", dest="basedir",
help="Base directory in which the server "\
"is launched (default: CURRENT_DIR)")
......@@ -20,8 +18,9 @@ def parse_commandline():
def run():
options, _args = parse_commandline()
Server(options).run()
server = Server(options)
server.setup()
server.run()
if __name__ == "__main__":
run()
# -*- coding: utf-8 -*-
# imports
import sys
import os
import tempfile
import cherrypy
from cherrypy import _cplogging
from cherrypy.lib import httputil
import vispa, vispa.url, vispa.log
import logging, log
import vispa.url
import vispa.tools.db
import vispa.tools.device
import vispa.tools.profile
import vispa.tools.stats
import vispa.tools.template
import vispa.tools.user
import vispa.tools.workspace
import vispa.plugins.db
import vispa.plugins.template
import logging
from logging import config as loggingcfg
import sqlalchemy
systemlog = logging.getLogger("SYSTEM")
systemlog = logging.getLogger(__name__)
class Server(object):
__default_server_config = {
'tools.proxy.on': False,
'server.socket_host': '127.0.0.1',
'server.socket_port': 4282,
'server.thread_pool': 10,
'engine.autoreload_on': False
}
__default_mount_config = {
'/': {
'tools.db.on': True,
'tools.encode.on': False,
'tools.sessions.on': True,
'tools.sessions.storage_type': 'file',
'tools.sessions.storage_path': vispa.abspath('var', 'sessions'),
'tools.sessions.timeout': 1440,
'tools.staticdir.root': vispa.abspath('static'),
'tools.gzip.on': True,
'tools.gzip.mime_types': ['text/html', 'text/css', 'application/x-javascript'],
},
'/static': {
'tools.db.on': False,
'tools.etags.on': True,
'tools.staticdir.on': True,
'tools.staticdir.dir': '',
'tools.sessions.on': False,
},
'/static/css': {
'tools.staticdir.dir': 'css',
},
'/static/js': {
'tools.staticdir.dir': 'js',
},
'/static/frameworks': {
'tools.staticdir.dir': 'frameworks',
},
'/static/img': {
'tools.gzip.on': False,
'tools.staticdir.dir': 'img',
}
}
def __init__(self, options):
self.__platform = None
# the base dir
self.base_dir = os.path.abspath(options.basedir)
......@@ -60,149 +109,55 @@ class Server(object):
cherrypy.log('No logging.conf found, using default logging.', 'SERVER')
# update the global settings for the HTTP server and engine
server_config = {
'tools.proxy.on': False,
'server.socket_host': '127.0.0.1',
'server.socket_port': 4282,
'server.thread_pool': 10,
'engine.autoreload_on': False
}
cherrypy.config.update(server_config)
cherrypy.config.update(self.__default_server_config)
cherrypy_conf = vispa.abspath('conf', 'cherrypy.ini')
if os.path.isfile(cherrypy_conf):
cherrypy.config.update(cherrypy_conf)
# setup base
base_dynamic = vispa.config('web', 'base.dynamic', None) or vispa.config('web', 'base', '/')
vispa.url.base_dynamic = base_dynamic if base_dynamic.endswith('/') else base_dynamic + '/'
base_static = vispa.config('web', 'base.static', None) or vispa.config('web', 'base', '/')
vispa.url.base_static = base_static if base_static.endswith('/') else base_static + '/'
# shorthand
engine = cherrypy.engine
# we amend the system path so that Python can find
# the application's modules. 0 => Python starts(!) searching in
# self.base_dir
sys.path.insert(0, self.base_dir)
# register the Template engine tool
from vispa.tools.template import MakoTool
cherrypy.tools.render = MakoTool({'base_dynamic': vispa.url.base_dynamic, 'base_static': vispa.url.base_static})
# register the Database access tool
from vispa.tools.db import SATool
cherrypy.tools.db = SATool()
# tool to load the logged in user or redirect
# the client to the login page
from vispa.tools.user import UserTool
cherrypy.tools.user = UserTool(vispa.url.dynamic('/login'))
# the workspace tool
from vispa.tools.workspace import WorkspaceTool
cherrypy.tools.workspace = WorkspaceTool(vispa.url.dynamic('/workspace'))
# the Device Tool
from vispa.tools.device import DeviceTool
cherrypy.tools.device = DeviceTool()
# the profile tool
from vispa.tools.profile import ProfileTool
cherrypy.tools.profile = ProfileTool()
def setupTools(self):
cherrypy.tools.render = vispa.tools.template.MakoTool({'base_dynamic': vispa.url.dynamic('/'), 'base_static': vispa.url.static('/', timestamp=False)})
cherrypy.tools.db = vispa.tools.db.SATool()
cherrypy.tools.user = vispa.tools.user.UserTool(vispa.url.dynamic('/login'))
cherrypy.tools.workspace = vispa.tools.workspace.WorkspaceTool(vispa.url.dynamic('/workspace'))
cherrypy.tools.device = vispa.tools.device.DeviceTool()
cherrypy.tools.profile = vispa.tools.profile.ProfileTool()
cherrypy.tools.stats = vispa.tools.stats.StatsTool()
if vispa.config('websockets', 'enabled', False):
cherrypy.tools.websocket = vispa.tools.socket.WSTool()
def setupPlugins(self):
if vispa.config('websockets', 'enabled', False):
cherrypy.tools.websocket.set_platform(self.__platform)
from ws4py.server.cherrypyserver import WebSocketPlugin
WebSocketPlugin(cherrypy.engine).subscribe()
# the stats tool
from vispa.tools.stats import StatsTool
cherrypy.tools.stats = StatsTool()
vispa.plugins.template.MakoPlugin(cherrypy.engine, base_dir=self.base_dir, module_dir=self.cache_dir).subscribe()
# the websocket tool
use_websockets = vispa.config('websockets', 'use_websockets', False)
if use_websockets:
from vispa.tools.socket import WSTool
cherrypy.tools.websocket = WSTool()
self.sa_identifier = vispa.config('database', 'sqlalchemy.url', 'sqlite:///var/db/vispa.db')
self.sa_engine = sqlalchemy.create_engine(self.sa_identifier)
vispa.plugins.db.SAPlugin(cherrypy.engine, self.sa_identifier).subscribe()
def setupPlatform(self):
from vispa.platform import Platform
platform = Platform(self.var_dir)
platform.load_extensions(platform)
# the websocket plugin (and ws tool update)
if use_websockets:
cherrypy.tools.websocket.set_platform(platform)
from ws4py.server.cherrypyserver import WebSocketPlugin
WebSocketPlugin(engine).subscribe()
platform_config = {
'/': {
'tools.db.on': True,
'tools.encode.on': False,
'tools.sessions.on': True,
'tools.sessions.storage_type': 'file',
'tools.sessions.storage_path': vispa.abspath('var', 'sessions'),
'tools.sessions.timeout': 1440,
'tools.staticdir.root': vispa.abspath('static'),
'tools.gzip.on': True,
'tools.gzip.mime_types':['text/html', 'text/css', 'application/x-javascript'],
},
'/static': {
'tools.db.on': False,
'tools.etags.on': True,
'tools.staticdir.on': True,
'tools.staticdir.dir': '',
'tools.sessions.on': False,
},
'/static/css': {
'tools.staticdir.dir': 'css',
},
'/static/js': {
'tools.staticdir.dir': 'js',
},
'/static/frameworks': {
'tools.staticdir.dir': 'frameworks',
},
'/static/img': {
'tools.gzip.on': False,
'tools.staticdir.dir': 'img',
}
}
app = cherrypy.tree.mount(platform.controller, vispa.url.base_dynamic, platform_config)
cherrypy_conf = vispa.abspath('conf', 'cherrypy.ini')
app = cherrypy.tree.mount(platform.controller, vispa.url.dynamic('/'), self.__default_mount_config)
if os.path.isfile(cherrypy_conf):
app.merge(cherrypy_conf)
# self.make_rotate_logs(app)
# template engine plugin
from vispa.plugins.template import MakoPlugin
MakoPlugin(engine, base_dir=self.base_dir, module_dir=self.cache_dir).subscribe()
# database connection management plugin
# add the db dir to var if not present
# create the SqlAlchemy identifier
if vispa.config.has_option('database', 'sqlalchemy.url'):
self.sa_identifier = vispa.config.get('database', 'sqlalchemy.url')
else:
systemlog.warning('Using default sqlalchemy.url: sqlite:///var/db/vispa.db')
self.sa_identifier = 'sqlite:///var/db/vispa.db'
from vispa.plugins.db import SAPlugin
SAPlugin(engine, self.sa_identifier).subscribe()
# store a permanent engine for db access outside requests
import sqlalchemy
self.sa_engine = sqlalchemy.create_engine(self.sa_identifier)
def setup(self):
self.setupTools()
self.setupPlatform()
self.setupPlugins()
def run(self):
engine = cherrypy.engine
if hasattr(engine, 'signal_handler'):
engine.signal_handler.subscribe()
# start the CherryPy engine
engine.start()
# run the engine main loop
engine.block()
if hasattr(cherrypy.engine, 'signal_handler'):
cherrypy.engine.signal_handler.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
def on_error(self, status, message, traceback, version):
code = '404' if status.startswith('404') else 'error'
......
......@@ -45,7 +45,7 @@ class UserTool(cherrypy.Tool):
if path:
# cut trailing slash to work with os.path.join
path = path[1:] if path.startswith("/") else path
path = os.path.join(vispa.url.base_static, path)
path = os.path.join(vispa.url.static('/', timestamp=False), path)
# store if there should be a redirect at the end of this function
redirect = False
......
import re
import os
base_dynamic = u"/"
base_static = u"/"
import vispa
def clean(url):
u = url.replace(u"\\",u"/").replace(u"///",u"/").strip()
......@@ -12,16 +10,25 @@ def join(*args):
return clean(u"/".join(args))
def dynamic(relative_url):
base_dynamic = vispa.config(u'web', u'base.dynamic', None) or vispa.config(u'web', u'base', u'/')
return join(base_dynamic, relative_url)
def static(relative_url, extension=None):
def static(relative_url, **kwargs):
base_static = vispa.config(u'web', u'base.static', None) or vispa.config(u'web', u'base', u'/')
path = os.path.join(u"static", relative_url)
url = join(base_static, u"static", relative_url)
extension = kwargs.get('extension', None)
if extension:
url = join(base_static, u"extensions", extension , path)
path = os.path.join(u"vispa/extensions", extension, path)
try:
t = os.path.getmtime(path)
except:
t = 0
return u"%s?%s" % (url, unicode(t))
if kwargs.get('timestamp', True):
try:
t = os.path.getmtime(path)
except:
t = 0
return u"%s?%s" % (url, unicode(t))
else:
return url
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment