main.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require 'async'
  2. require 'logger'
  3. require_relative 'registry'
  4. require_relative 'utilities'
  5. require_relative 'config'
  6. require_relative 'whoisd'
  7. Async do |task|
  8. logger = Logger.new($stdout)
  9. logger.level = Logger::WARN
  10. original_formatter = Logger::Formatter.new
  11. logger.formatter = proc { |severity, datetime, progname, msg|
  12. original_formatter.call(severity, datetime, progname, msg.sanitize)
  13. }
  14. begin
  15. configuration_file = (ARGV.empty? ? "#{Dir.home}#{File::SEPARATOR}.dnet-whois.json" : ARGV[0].strip)
  16. config = Config.new(configuration_file:).read
  17. case config.logging_level
  18. when 'fatal'
  19. logger.level = Logger::FATAL
  20. when 'error'
  21. logger.level = Logger::ERROR
  22. when 'warning'
  23. logger.level = Logger::WARN
  24. when 'info'
  25. logger.level = Logger::INFO
  26. when 'debug'
  27. logger.level = Logger::DEBUG
  28. else
  29. raise 'Unknown logging level'
  30. end
  31. rescue ArgumentError, RuntimeError => e
  32. logger.fatal e.message
  33. exit!
  34. end
  35. logger.debug '###### WARNUNG: In debug mode both the request and the IP address are logged. This can lead to privacy problems. It is not suitable for permanent productive operation. ######'
  36. begin
  37. registry = Registry.new(dir: config.registry_directory, logger:)
  38. rescue StandardError
  39. logger.fatal 'Failed to initialize the registry. Exiting now.'
  40. exit!
  41. end
  42. task.async(transient: true) do |subtask|
  43. loop do
  44. begin
  45. logger.info 'Update registry data.'
  46. registry.update
  47. rescue ArgumentError => e
  48. logger.error "Error: #{e} Continue."
  49. end
  50. subtask.sleep config.registry_reload_interval * 60
  51. end
  52. end
  53. task.async do
  54. whois = WhoisServer.new(registry:, logger:, endpoint: config.whois_endpoint)
  55. logger.info 'Start server.'
  56. whois.run
  57. end
  58. end.wait