__init__.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. '''
  2. searx is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Affero General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. searx is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  12. (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
  13. '''
  14. import logging
  15. from os import environ
  16. from os.path import realpath, dirname, join, abspath, isfile
  17. from io import open
  18. from yaml import safe_load
  19. searx_dir = abspath(dirname(__file__))
  20. engine_dir = dirname(realpath(__file__))
  21. static_path = abspath(join(dirname(__file__), 'static'))
  22. def check_settings_yml(file_name):
  23. if isfile(file_name):
  24. return file_name
  25. else:
  26. return None
  27. # find location of settings.yml
  28. if 'SEARX_SETTINGS_PATH' in environ:
  29. # if possible set path to settings using the
  30. # enviroment variable SEARX_SETTINGS_PATH
  31. settings_path = check_settings_yml(environ['SEARX_SETTINGS_PATH'])
  32. else:
  33. # if not, get it from searx code base or last solution from /etc/searx
  34. settings_path = check_settings_yml(join(searx_dir, 'settings.yml')) or check_settings_yml('/etc/searx/settings.yml')
  35. if not settings_path:
  36. raise Exception('settings.yml not found')
  37. # load settings
  38. with open(settings_path, 'r', encoding='utf-8') as settings_yaml:
  39. settings = safe_load(settings_yaml)
  40. if settings['ui']['static_path']:
  41. static_path = settings['ui']['static_path']
  42. '''
  43. enable debug if
  44. the environnement variable SEARX_DEBUG is 1 or true
  45. (whatever the value in settings.yml)
  46. or general.debug=True in settings.yml
  47. disable debug if
  48. the environnement variable SEARX_DEBUG is 0 or false
  49. (whatever the value in settings.yml)
  50. or general.debug=False in settings.yml
  51. '''
  52. searx_debug_env = environ.get('SEARX_DEBUG', '').lower()
  53. if searx_debug_env == 'true' or searx_debug_env == '1':
  54. searx_debug = True
  55. elif searx_debug_env == 'false' or searx_debug_env == '0':
  56. searx_debug = False
  57. else:
  58. searx_debug = settings.get('general', {}).get('debug')
  59. if searx_debug:
  60. logging.basicConfig(level=logging.DEBUG)
  61. else:
  62. logging.basicConfig(level=logging.WARNING)
  63. logger = logging.getLogger('searx')
  64. logger.debug('read configuration from %s', settings_path)
  65. logger.info('Initialisation done')
  66. if 'SEARX_SECRET' in environ:
  67. settings['server']['secret_key'] = environ['SEARX_SECRET']
  68. if 'SEARX_BIND_ADDRESS' in environ:
  69. settings['server']['bind_address'] = environ['SEARX_BIND_ADDRESS']
  70. if not searx_debug and settings['server']['secret_key'] == 'ultrasecretkey':
  71. logger.error('server.secret_key is not changed. Please use something else instead of ultrasecretkey.')
  72. exit(1)