def __init__(self, bot, **kwargs):
super().__init__(**kwargs)
self.bot = bot
self.file = os.path.join('data', 'ricedb.json')
datadir = os.path.dirname(self.file)
try:
with open(self.file, 'r') as fd:
self.update(json.load(fd))
except FileNotFoundError:
# Database file itself doesn't need to exist on first run, it will be created on first write.
if not os.path.exists(datadir):
os.mkdir(datadir)
self.bot.log.debug('Created {0}/ directory'.format(datadir))
try:
self.config = self.bot.config[__name__]
if not self.config.get('enable_http_server'):
return
host, port = self.config['http_host'], int(self.config['http_port'])
except KeyError:
host, port = '127.0.0.1', 8080
bottle.hook('before_request')(self.__strip_path)
bottle.route('/')(self.__http_index)
bottle.route('/<user>')(self.__http_index)
bottle.route('/<user>/<key>')(self.__http_index)
bottle_thread = threading.Thread(
target=bottle.run,
kwargs={'quiet': True, 'host': host, 'port': port},
name='{0} HTTP server'.format(__name__),
daemon=True
)
bottle_thread.start()
self.bot.log.info('{0} started on http://{1}:{2}'.format(bottle_thread.name, host, port))