def main(): cfg = utils.getcfg([ 'serve.conf', '~/.webserver/serve.conf', '/etc/webserver/serve.conf']) utils.initlog(cfg.get('log', 'loglevel'), cfg.get('log', 'logfile')) addr = (cfg.get('main', 'addr'), cfg.getint('main', 'port')) engine = cfg.get('server', 'engine') if engine == 'apps': import apps ws = http.WebServer(apps.dis, cfg.get('log', 'access')) elif engine == 'wsgi': import app_webpy ws = http.WSGIServer(app_webpy.app.wsgifunc(), cfg.get('log', 'access')) else: raise Exception('invaild engine %s' % engine) server = cfg.get('server', 'server') if server == 'gevent': from gevent.server import StreamServer ws = StreamServer(addr, ws.handler) elif server == 'thread': ws = ThreadServer(addr, ws.handler) else: raise Exception('invaild server %s' % server) try: ws.serve_forever() except KeyboardInterrupt: pass
def main(as_mod=False): global optdict optlist, args = getopt.getopt(sys.argv[1:], 'bc:dhjs:') optdict = dict(optlist) if '-h' in optdict: print main.__doc__ return cfgpathes = ['cves.ini', '/etc/cves.ini'] if '-c' in optdict: cfgpathes.insert(0, optdict['-c']) utils.cfg = utils.getcfg(cfgpathes) utils.initlog(utils.cfg.get('log', 'level'), utils.cfg.get('log', 'file')) echo = utils.cfg.has_option('db', 'echo') and utils.cfg.getboolean('db', 'echo') utils.engine = sqlalchemy.create_engine(utils.cfg.get('db', 'url'), echo=echo) utils.sess = sqlalchemy.orm.sessionmaker(bind=utils.engine)() if '-j' in optdict: return cron_job() elif '-b' in optdict: return built_db() application = web_main() if as_mod: return application return bottle.run(app=application, host=app.config['baseurl'].hostname, port=app.config['baseurl'].port, reloader=True)
def main(): """ durain [-c configfile] [-h] options: * -c: config file * -h: show help """ optlist, args = getopt.getopt(sys.argv[1:], "c:f:l:h") optdict = dict(optlist) if "-h" in optdict: print main.__doc__ return cfg = utils.getcfg(optdict.get("-c", ["durian.conf", "~/.durian.conf", "/etc/durian/durian.conf"])) utils.initlog(cfg.get("log", "loglevel"), cfg.get("log", "logfile")) import http if cfg.has_section("pool"): http.connector.max_addr = cfg.getint("pool", "maxaddr") addr = (cfg.get("main", "addr"), cfg.getint("main", "port")) import proxy, manager p = proxy.Proxy(accesslog=cfg.get("log", "access")) p.application = manager.setup(p) if cfg.getboolean("log", "verbose"): p.VERBOSE = True import midware if cfg.has_section("auth"): auth = midware.Auth() if cfg.has_option("auth", "userfile"): auth.loadfile(cfg.get("auth", "userfile")) elif cfg.has_option("auth", "username"): auth.add(cfg.get("auth", "username"), cfg.get("auth", "password")) auth.setup(p) if cfg.has_section("cache"): store = None if cfg.get("cache", "engine") == "memory": store = midware.MemoryCache(cfg.getint("cache", "size")) if store: midware.Cache(store).setup(p) try: StreamServer(addr, p.handler).serve_forever() except KeyboardInterrupt: pass
#!/usr/bin/python # -*- coding: utf-8 -*- ''' @date: 2011-05-05 @author: shell.xu @version: 0.8.1 ''' import os, sys, web, base64, getopt from os import path from web.contrib.template import render_mako import utils from mgr import * cfg = utils.getcfg(['cves.conf', '/etc/cves.conf']) web.config.cfg = cfg DEBUG = not path.isfile('RELEASE') web.config.debug = DEBUG web.config.rootdir = path.dirname(__file__) web.config.db = web.database(**dict(cfg.items('db'))) web.config.render = render_mako( directories = ['templates'], imports = ['import web'], default_filters = ['decode.utf8'], filesystem_checks = DEBUG, module_directory = None if DEBUG else '/tmp/mako_modules', input_encoding = 'utf-8', output_encoding = 'utf-8') def serve_file(filepath): class ServeFile(object): def GET(self): with open(filepath, 'rb') as fi: return fi.read() return ServeFile
def main(): optlist, args = getopt.getopt(sys.argv[1:], 'c:hp:') optdict = dict(optlist) if '-h' in optdict: print main.__doc__ return cfg = utils.getcfg(optdict.get('-c', [ '/etc/lxcweb/lxcweb.conf', 'lxcweb.conf'])) utils.initlog(cfg.get('log', 'loglevel'), cfg.get('log', 'logfile')) if cfg.has_section('lxc'): lxc.global_configfile = cfg.get('lxc', 'config') lxc.default_lxcpath = cfg.get('lxc', 'lxcpath') lxc.sudoflag = cfg.getboolean('lxc', 'sudo') # if web.config.rootdir: os.chdir(web.config.rootdir) static_path = None for p in ['static', '/usr/share/lxcweb/static']: if path.isdir(p): static_path = p break assert static_path is not None, "static path not exist." app = web.application(( '/static/(.*)', serve_path(static_path), # info actions '/', serve_file(path.join(static_path, 'home.html')), '/list.json', lxcweb.ListJson, '/info/(.*).json', lxcweb.InfoJson, '/ps/(.*).json', lxcweb.PsJson, '/ps/.*', serve_file(path.join(static_path, 'ps.html')), '/config/(.*).json', lxcweb.ConfigJson, '/config/.*', serve_file(path.join(static_path, 'config.html')), # image actions '/clone/(.*)/(.*)', lxcweb.Clone, '/create/(.*)', lxcweb.Create, '/destroy/(.*)', lxcweb.Destroy, '/export/(.*)', lxcweb.Export, '/import/(.*)', lxcweb.Import, # container actions '/start/(.*)', lxcweb.Start, '/stop/(.*)', lxcweb.Stop, '/shutdown/(.*)', lxcweb.Shutdown, '/reboot/(.*)', lxcweb.Reboot, '/freeze/(.*)', lxcweb.Freeze, '/unfreeze/(.*)', lxcweb.Unfreeze, # runtime actions '/attach/(.*)', lxcweb.Attach, )) kw = {} port = int(optdict.get('-p') or cfg.getint('main', 'port')) if cfg.has_section('ssl'): keyfile = cfg.get('ssl', 'key') certfile = cfg.get('ssl', 'cert') if path.exists(keyfile) and path.exists(certfile): kw = {'keyfile': keyfile, 'certfile': certfile} if cfg.has_section('users'): app.add_processor(Authenticate( cfg.get('users', 'username'), cfg.get('users', 'password'))) from gevent.pywsgi import WSGIServer print 'service port :%d' % port WSGIServer(('', port), app.wsgifunc(), **kw).serve_forever()