__init__.py 3.3 KB

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