/src/python/frontend/__main__.py

https://github.com/jkoshy/osm-api-server · Python · 119 lines · 64 code · 16 blank · 39 comment · 11 complexity · e7142af12bf4a246ca47037d25852c60 MD5 · raw file

  1. # Copyright (c) 2011 AOL Inc. All Rights Reserved.
  2. #
  3. # Permission is hereby granted, free of charge, to any person
  4. # obtaining a copy of this software and associated documentation files
  5. # (the "Software"), to deal in the Software without restriction,
  6. # including without limitation the rights to use, copy, modify, merge,
  7. # publish, distribute, sublicense, and/or sell copies of the Software,
  8. # and to permit persons to whom the Software is furnished to do so,
  9. # subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be
  12. # included in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. ## The script entry point for the front-end server.
  23. import os.path
  24. import sys
  25. import tornado.options
  26. import apiserver.const as C
  27. from fe import OSMFrontEndServer
  28. from apiserver.osmelement import init_osm_factory
  29. from datastore.slabutil import init_slabutil
  30. # Where to find configuration information.
  31. default_config_directory = "config"
  32. default_config_file = "osm-api-server.cfg"
  33. tornado.options.define("backend", default=None,
  34. type=str, metavar="BACKEND",
  35. help="datastore backend to use")
  36. tornado.options.define("config", default=default_config_file,
  37. type=str, metavar="FILE",
  38. help="configuration file to use")
  39. tornado.options.define("encoding", default=None,
  40. type=str, metavar="ENCODING",
  41. help="Encoding used for values")
  42. tornado.options.define("verbose", default=False,
  43. type=bool, metavar="BOOLEAN",
  44. help="Control verbosity")
  45. def error(message):
  46. "Print an error message and exit."
  47. sys.stderr.write("Error: " + message + "\n")
  48. sys.exit(1)
  49. ##
  50. ## Script entry point.
  51. ##
  52. def main():
  53. """Launch the API server."""
  54. # Parse command line options if present.
  55. tornado.options.parse_command_line()
  56. options = tornado.options.options
  57. # Bring in (server-wide) configuration information.
  58. try:
  59. import configparser # Python 3.0
  60. except ImportError:
  61. import ConfigParser as configparser
  62. # Read configuration information.
  63. configfiles = [options.config,
  64. os.path.join(sys.path[0], default_config_directory,
  65. default_config_file)]
  66. cfg = configparser.ConfigParser()
  67. cfg.read(configfiles)
  68. # Sanity check.
  69. if not cfg.has_section(C.FRONT_END):
  70. error("Incomplete configuration information, tried:\n\t" +
  71. "\n\t".join(configfiles))
  72. # Allow command-line options to override the configuration file.
  73. if options.backend:
  74. cfg.set(C.DATASTORE, C.DATASTORE_BACKEND, options.backend)
  75. if options.encoding:
  76. cfg.set(C.DATASTORE, C.DATASTORE_ENCODING, options.encoding)
  77. # Load the desired interface to the datastore.
  78. backend = cfg.get(C.DATASTORE, C.DATASTORE_BACKEND)
  79. try:
  80. module = __import__('datastore.ds_' + backend, fromlist=['Datastore'])
  81. datastore = module.Datastore(cfg)
  82. except ImportError, x:
  83. error("Could not initialize datastore of type \"%s\": %s" %
  84. (backend, str(x)))
  85. # Initialize the OSM element factory and other modules.
  86. init_slabutil(cfg)
  87. init_osm_factory(cfg)
  88. # Create an instance of the front-end server.
  89. port = cfg.getint(C.FRONT_END, C.PORT)
  90. feserver = OSMFrontEndServer(cfg, options, datastore)
  91. http_server = tornado.httpserver.HTTPServer(feserver.application)
  92. http_server.listen(port)
  93. # Start the server.
  94. try:
  95. tornado.ioloop.IOLoop.instance().start()
  96. except KeyboardInterrupt:
  97. if options.verbose:
  98. pass # Print statistics etc.
  99. #
  100. # Invoke main()
  101. #
  102. if __name__ == "__main__":
  103. main()