Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/class/core/config.py

116 lines
3.0 KiB

7 years ago
# coding:utf-8
import sys
import io
import os
import time
7 years ago
import shutil
7 years ago
6 years ago
reload(sys)
sys.setdefaultencoding('utf8')
7 years ago
from flask import Flask
from datetime import timedelta
7 years ago
sys.path.append(os.getcwd() + "/class/core")
import db
import public
6 years ago
class MiddleWare:
def __init__(self, wsgi_app):
self.wsgi_app = wsgi_app
def __call__(self, *args, **kwargs):
# print args
return self.wsgi_app(*args, **kwargs)
7 years ago
7 years ago
class config:
__version = '0.0.1'
__app = None
6 years ago
__modules = None
7 years ago
def __init__(self):
pass
def makeApp(self, name):
app = Flask(name)
6 years ago
self.__app = app
7 years ago
7 years ago
app.config.version = self.__version
7 years ago
app.config['SECRET_KEY'] = os.urandom(24)
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
7 years ago
7 years ago
self.initDB()
7 years ago
self.initInitD()
6 years ago
self.initRoute()
6 years ago
self.startDebug()
6 years ago
app.wsgi_app = MiddleWare(app.wsgi_app)
7 years ago
return app
6 years ago
def startDebug(self):
6 years ago
self.__app.debug = True
self.__app.config.version = self.__version + str(time.time())
6 years ago
7 years ago
def initDB(self):
try:
sql = db.Sql().dbfile('default')
csql = public.readFile('data/sql/default.sql')
csql_list = csql.split(';')
for index in range(len(csql_list)):
sql.execute(csql_list[index], ())
except Exception, ex:
print str(ex)
7 years ago
def initUser(self):
pass
def initInitD(self):
script = public.getRunDir() + '/scripts/init.d/mw.tpl'
script_bin = public.getRunDir() + '/scripts/init.d/mw'
7 years ago
if os.path.exists(script_bin):
return
7 years ago
content = public.readFile(script)
content = content.replace("{$SERVER_PATH}", public.getRunDir())
public.writeFile(script_bin, content)
public.execShell('chmod +x ' + script_bin)
7 years ago
if public.getOs() != 'darwin':
initd_bin = '/etc/init.d/mw'
if not os.path.exists(initd_bin):
shutil.copyfile(script_bin, initd_bin)
7 years ago
public.execShell('chmod +x ' + initd_bin)
7 years ago
7 years ago
def getVersion(self):
return self.__version
def getApp(self):
return self.__app
6 years ago
def initRoute(self):
6 years ago
import route
6 years ago
DEFAULT_MODULES = (
(route.dashboard, "/"),
(route.site, "/site"),
(route.files, "/files"),
(route.soft, "/soft"),
(route.config, "/config"),
(route.plugins, "/plugins"),
(route.task, "/task"),
(route.system, "/system"),
(route.database, "/database"),
(route.crontab, "/crontab"),
(route.firewall, "/firewall"),
6 years ago
(route.control, "/control")
6 years ago
)
self.modules = DEFAULT_MODULES
self.settingModules(self.__app, DEFAULT_MODULES)
def settingModules(self, app, modules):
for module, url_prefix in modules:
app.register_blueprint(module, url_prefix=url_prefix)