gitlabircsender.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #! /usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. #
  4. # gitlab irc sender
  5. # Copyright (C) 2016-2017 Andrei Karas (4144)
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import logging
  20. from code.apihandler import ApiHandler
  21. from code.configuration import Configuration
  22. from code.core import Core
  23. from code.messagehandler import MessageHandler
  24. from code.serverhandler import ServerHandler
  25. try:
  26. from SocketServer import ThreadingMixIn
  27. except ImportError:
  28. from socketserver import ThreadingMixIn
  29. try:
  30. from BaseHTTPServer import HTTPServer
  31. except ImportError:
  32. from http.server import HTTPServer
  33. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  34. """Handle requests in a separate thread."""
  35. class MainClass:
  36. @staticmethod
  37. def init_config(name):
  38. Core.config = Configuration()
  39. Core.config.loadConfiguration(name)
  40. @staticmethod
  41. def init_messages():
  42. Core.messageHandler = MessageHandler()
  43. Core.messageHandler.start()
  44. @staticmethod
  45. def init_api():
  46. Core.apiHandler = ApiHandler()
  47. Core.apiHandler.start()
  48. @staticmethod
  49. def init_log():
  50. Core.log = logging.getLogger('log')
  51. Core.log.setLevel(Configuration.log_level)
  52. log_handler = logging.handlers.RotatingFileHandler(
  53. Configuration.log_file,
  54. maxBytes = Configuration.log_max_size,
  55. backupCount = 10)
  56. f = logging.Formatter("%(asctime)s %(message)s",
  57. "%B %d %H:%M:%S")
  58. log_handler.setFormatter(f)
  59. Core.log.addHandler(log_handler)
  60. @staticmethod
  61. def run_server():
  62. try:
  63. # single threaded
  64. # Core.server = HTTPServer((Core.config.global_options.listen_host, Core.config.global_options.listen_port), ServerHandler)
  65. # milti threaded
  66. Core.server = ThreadedHTTPServer((Core.config.global_options.listen_host, Core.config.global_options.listen_port), ServerHandler)
  67. Core.server.timeout = 5
  68. print('Started http server on {0}:{1}'.format(Core.config.global_options.listen_host, Core.config.global_options.listen_port))
  69. Core.server.serve_forever()
  70. except KeyboardInterrupt:
  71. print('^C received, shutting down the web server')
  72. Core.apiHandler.die = True
  73. Core.messageHandler.die = True
  74. Core.server.socket.close()
  75. Core.messageHandler.join()
  76. if __name__ == '__main__':
  77. MainClass.init_log()
  78. MainClass.init_config("config.yaml")
  79. MainClass.init_messages()
  80. MainClass.init_api()
  81. MainClass.run_server()