/thumbor/app.py

https://github.com/cezarsa/thumbor · Python · 95 lines · 64 code · 20 blank · 11 comment · 10 complexity · 34b0da1e6df92b82dfc4c03c739ee12e MD5 · raw file

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # thumbor imaging service
  4. # https://github.com/globocom/thumbor/wiki
  5. # Licensed under the MIT license:
  6. # http://www.opensource.org/licenses/mit-license
  7. # Copyright (c) 2011 globo.com timehome@corp.globo.com
  8. import os
  9. from os.path import join, abspath, dirname, expanduser, exists
  10. import tornado.web
  11. import tornado.ioloop
  12. from tornado.options import parse_config_file, options
  13. from thumbor.config import conf
  14. from thumbor.handlers.unsafe import MainHandler
  15. from thumbor.handlers.healthcheck import HealthcheckHandler
  16. from thumbor.handlers.crypto import CryptoHandler
  17. from thumbor.utils import real_import, logger
  18. from thumbor.url import Url
  19. from thumbor import filters
  20. class ThumborServiceApp(tornado.web.Application):
  21. def __init__(self, conf_file=None, security_key=None, custom_handlers=None):
  22. if conf_file is None:
  23. conf_file = ThumborServiceApp.get_conf_file(conf_file)
  24. logger.info('Config file: %s' % conf_file)
  25. parse_config_file(conf_file)
  26. self.loader = real_import(conf.LOADER)
  27. self.storage = real_import(conf.STORAGE)
  28. self.engine = real_import(conf.ENGINE)
  29. self.detectors = [real_import(detector_name).Detector for detector_name in conf.DETECTORS]
  30. self.filters = [real_import(filter_name).Filter for filter_name in conf.FILTERS]
  31. filters.compile_filters(self.filters)
  32. # run again to overwrite the default settings on the
  33. # imported modules with the ones defined into the config file
  34. parse_config_file(conf_file)
  35. #storage = storage.Storage()
  36. #self.engine = self.engine.Engine()
  37. if security_key:
  38. options.SECURITY_KEY = security_key
  39. handler_context = {
  40. 'loader': self.loader,
  41. 'storage': self.storage,
  42. 'engine': self.engine,
  43. 'detectors': self.detectors,
  44. 'filters': self.filters
  45. }
  46. handlers = [
  47. (r'/healthcheck', HealthcheckHandler)
  48. ]
  49. if conf.ALLOW_UNSAFE_URL:
  50. handlers.append(
  51. (Url.regex(), MainHandler, handler_context),
  52. )
  53. if custom_handlers:
  54. for handler in custom_handlers:
  55. handlers.append((handler[0], handler[1], handler_context))
  56. else:
  57. handlers.append(
  58. (r'/(?P<crypto>[^/]+)/(?P<image>(.+))', CryptoHandler, handler_context)
  59. )
  60. super(ThumborServiceApp, self).__init__(handlers)
  61. @classmethod
  62. def get_conf_file(cls, conf_file):
  63. lookup_conf_file_paths = [
  64. os.curdir,
  65. expanduser('~'),
  66. '/etc/',
  67. dirname(__file__)
  68. ]
  69. for conf_path in lookup_conf_file_paths:
  70. conf_path_file = abspath(join(conf_path, 'thumbor.conf'))
  71. if exists(conf_path_file):
  72. return conf_path_file
  73. raise ConfFileNotFoundError('thumbor.conf file not passed and not found on the lookup paths %s' % lookup_conf_file_paths)
  74. class ConfFileNotFoundError(RuntimeError):
  75. pass