search.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # frozen_string_literal: true
  2. lib_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'lib')
  3. $LOAD_PATH.unshift(lib_dir) unless
  4. $LOAD_PATH.include?(lib_dir) || $LOAD_PATH.include?(lib_dir)
  5. require 'json'
  6. require 'neoinfra'
  7. require 'sinatra'
  8. require 'sinatra/base'
  9. require 'sinatra/contrib'
  10. # Handle loading data into the graph db
  11. class Search < Sinatra::Base
  12. register Sinatra::RespondWith
  13. set :views, File.join(File.dirname(__FILE__), '..', '/views')
  14. post '/all' do
  15. if params['search'] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
  16. n = NeoInfra::Nodes.new
  17. respond_to do |wants|
  18. wants.html do
  19. erb :view_node,
  20. layout: :base_layout,
  21. locals: { node: n.search_nodes_by_ip(params['search']) }
  22. end
  23. end
  24. elsif params['search'] =~ /i-[a-f0-9]{6,20}/
  25. n = NeoInfra::Nodes.new
  26. respond_to do |wants|
  27. wants.html do
  28. erb :view_node,
  29. layout: :base_layout,
  30. locals: { node: n.search_nodes_by_node_id(params['search']) }
  31. end
  32. end
  33. else
  34. n = NeoInfra::Nodes.new
  35. respond_to do |wants|
  36. wants.html do
  37. erb :view_nodes,
  38. layout: :base_layout,
  39. locals: { nodes: n.search_nodes_by_name(params['search']) }
  40. end
  41. end
  42. end
  43. end
  44. end