2019-08-20 00:24:51 +02:00
|
|
|
import sys
|
2019-09-01 23:13:24 +02:00
|
|
|
import logging
|
2019-08-20 00:24:51 +02:00
|
|
|
|
|
|
|
import bottle
|
|
|
|
|
2019-08-21 13:51:43 +02:00
|
|
|
from icalendar_timeseries_server.cal import start_scrape_calendar
|
2019-08-20 00:24:51 +02:00
|
|
|
from icalendar_timeseries_server.config import load_config, load_default_config, get_config
|
|
|
|
|
|
|
|
# Contains decorated bottle handler function for /api/v1/query
|
|
|
|
# noinspection PyUnresolvedReferences
|
|
|
|
from icalendar_timeseries_server.api import prometheus_api
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-09-01 23:13:24 +02:00
|
|
|
# Set up logger
|
|
|
|
log_handler = logging.StreamHandler()
|
|
|
|
log_handler.setFormatter(logging.Formatter(
|
|
|
|
'%(asctime)s %(filename)s:%(lineno)d(%(funcName)s) [%(levelname)s]: %(message)s'))
|
|
|
|
logging.getLogger().addHandler(log_handler)
|
|
|
|
|
|
|
|
# Load configuration
|
2019-08-20 15:53:51 +02:00
|
|
|
config = get_config()
|
2019-09-01 23:13:24 +02:00
|
|
|
try:
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
load_default_config()
|
|
|
|
elif len(sys.argv) == 2:
|
|
|
|
load_config(sys.argv[1])
|
|
|
|
else:
|
|
|
|
logging.log(logging.FATAL, f'Can only read one config file, got "{" ".join(sys.argv[1:])}"')
|
|
|
|
exit(1)
|
|
|
|
# Re-fetch config after parsing
|
|
|
|
config = get_config()
|
|
|
|
except:
|
|
|
|
logging.fatal('Could not parse configuration file')
|
|
|
|
exit(1)
|
|
|
|
|
2019-08-21 13:51:43 +02:00
|
|
|
# Schedule calendar scraping in the background
|
2019-08-20 15:53:51 +02:00
|
|
|
for calname in config.calendars.keys():
|
2019-08-21 13:51:43 +02:00
|
|
|
start_scrape_calendar(calname, config.calendars[calname])
|
2019-09-01 23:13:24 +02:00
|
|
|
|
2019-08-21 13:51:43 +02:00
|
|
|
# Start the Bottle HTTP server
|
2019-08-20 15:53:51 +02:00
|
|
|
bottle.run(host=config.addr, port=get_config().port)
|
2019-08-20 00:24:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|