__init__.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. '''
  48. enable debug if
  49. the environnement variable SEARX_DEBUG is 1 or true
  50. (whatever the value in settings.yml)
  51. or general.debug=True in settings.yml
  52. disable debug if
  53. the environnement variable SEARX_DEBUG is 0 or false
  54. (whatever the value in settings.yml)
  55. or general.debug=False in settings.yml
  56. '''
  57. searx_debug_env = environ.get('SEARX_DEBUG', '').lower()
  58. if searx_debug_env == 'true' or searx_debug_env == '1':
  59. searx_debug = True
  60. elif searx_debug_env == 'false' or searx_debug_env == '0':
  61. searx_debug = False
  62. else:
  63. searx_debug = settings.get('general', {}).get('debug')
  64. if searx_debug:
  65. logging.basicConfig(level=logging.DEBUG)
  66. else:
  67. logging.basicConfig(level=logging.WARNING)
  68. logger = logging.getLogger('searx')
  69. logger.debug('read configuration from %s', settings_path)
  70. # Workaround for openssl versions <1.0.2
  71. # https://github.com/certifi/python-certifi/issues/26
  72. if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
  73. if hasattr(certifi, 'old_where'):
  74. environ['REQUESTS_CA_BUNDLE'] = certifi.old_where()
  75. logger.warning('You are using an old openssl version({0}), please upgrade above 1.0.2!'.format(OPENSSL_VERSION))
  76. logger.info('Initialisation done')
  77. if 'SEARX_SECRET' in environ:
  78. settings['server']['secret_key'] = environ['SEARX_SECRET']
  79. if 'SEARX_BIND_ADDRESS' in environ:
  80. settings['server']['bind_address'] = environ['SEARX_BIND_ADDRESS']