__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 certifi
  15. import logging
  16. from os import environ
  17. from os.path import realpath, dirname, join, abspath, isfile
  18. from io import open
  19. from ssl import OPENSSL_VERSION_INFO, OPENSSL_VERSION
  20. from yaml import safe_load
  21. searx_dir = abspath(dirname(__file__))
  22. engine_dir = dirname(realpath(__file__))
  23. static_path = abspath(join(dirname(__file__), 'static'))
  24. def check_settings_yml(file_name):
  25. if isfile(file_name):
  26. return file_name
  27. else:
  28. return None
  29. # find location of settings.yml
  30. if 'SEARX_SETTINGS_PATH' in environ:
  31. # if possible set path to settings using the
  32. # enviroment variable SEARX_SETTINGS_PATH
  33. settings_path = check_settings_yml(environ['SEARX_SETTINGS_PATH'])
  34. else:
  35. # if not, get it from searx code base or last solution from /etc/searx
  36. settings_path = check_settings_yml(join(searx_dir, 'settings.yml')) or check_settings_yml('/etc/searx/settings.yml')
  37. if not settings_path:
  38. raise Exception('settings.yml not found')
  39. # load settings
  40. with open(settings_path, 'r', encoding='utf-8') as settings_yaml:
  41. settings = safe_load(settings_yaml)
  42. if settings['ui']['static_path']:
  43. static_path = settings['ui']['static_path']
  44. '''
  45. enable debug if
  46. the environnement variable SEARX_DEBUG is 1 or true
  47. (whatever the value in settings.yml)
  48. or general.debug=True in settings.yml
  49. disable debug if
  50. the environnement variable SEARX_DEBUG is 0 or false
  51. (whatever the value in settings.yml)
  52. or general.debug=False in settings.yml
  53. '''
  54. searx_debug_env = environ.get('SEARX_DEBUG', '').lower()
  55. if searx_debug_env == 'true' or searx_debug_env == '1':
  56. searx_debug = True
  57. elif searx_debug_env == 'false' or searx_debug_env == '0':
  58. searx_debug = False
  59. else:
  60. searx_debug = settings.get('general', {}).get('debug')
  61. if searx_debug:
  62. logging.basicConfig(level=logging.DEBUG)
  63. else:
  64. logging.basicConfig(level=logging.WARNING)
  65. logger = logging.getLogger('searx')
  66. logger.debug('read configuration from %s', settings_path)
  67. # Workaround for openssl versions <1.0.2
  68. # https://github.com/certifi/python-certifi/issues/26
  69. if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
  70. if hasattr(certifi, 'old_where'):
  71. environ['REQUESTS_CA_BUNDLE'] = certifi.old_where()
  72. logger.warning('You are using an old openssl version({0}), please upgrade above 1.0.2!'.format(OPENSSL_VERSION))
  73. logger.info('Initialisation done')
  74. if 'SEARX_SECRET' in environ:
  75. settings['server']['secret_key'] = environ['SEARX_SECRET']
  76. if 'SEARX_BIND_ADDRESS' in environ:
  77. settings['server']['bind_address'] = environ['SEARX_BIND_ADDRESS']