Class: Server
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Server
- Defined in:
- app/server.rb
Overview
SQLUI Sinatra server.
Class Method Summary collapse
Class Method Details
.init_and_run(config, resources_dir) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/server.rb', line 17 def self.init_and_run(config, resources_dir) Mysql2::Client.[:as] = :array Mysql2::Client.[:cast_booleans] = true Mysql2::Client.[:database_timezone] = :utc Mysql2::Client.[:cache_rows] = false set :logging, true set :bind, '0.0.0.0' set :port, config.port set :environment, config.environment set :raise_errors, false set :show_exceptions, false logger = WEBrick::Log.new get '/-/health' do status 200 body 'OK' end get '/?' do redirect config.list_url_path, 301 end get "#{config.list_url_path}/?" do erb :databases, locals: { config: config } end config.database_configs.each do |database| get "#{database.url_path}/?" do redirect "#{database.url_path}/query", 301 end get "#{database.url_path}/sqlui.css" do @css ||= File.read(File.join(resources_dir, 'sqlui.css')) status 200 headers 'Content-Type' => 'text/css; charset=utf-8' body @css end get "#{database.url_path}/sqlui.js" do @js ||= File.read(File.join(resources_dir, 'sqlui.js')) status 200 headers 'Content-Type' => 'text/javascript; charset=utf-8' body @js end post "#{database.url_path}/metadata" do = database.with_client do |client| { server: "#{config.name} - #{database.display_name}", list_url_path: config.list_url_path, schemas: DatabaseMetadata.lookup(client, database), table_aliases: database.table_aliases, joins: database.joins, saved: Dir.glob("#{database.saved_path}/*.sql").to_h do |path| contents = File.read(path) comment_lines = contents.split("\n").take_while do |l| l.start_with?('--') end filename = File.basename(path) description = comment_lines.map { |l| l.sub(/^-- */, '') }.join("\n") [ filename, { filename: filename, description: description, contents: contents.strip } ] end } end status 200 headers 'Content-Type' => 'application/json; charset=utf-8' body .to_json end post "#{database.url_path}/query" do params.merge!(JSON.parse(request.body.read, symbolize_names: true)) break client_error('missing sql') unless params[:sql] variables = params[:variables] || {} sql = find_selected_query(params[:sql], params[:selection]) result = database.with_client do |client| query_result = execute_query(client, variables, sql) # NOTE: the call to result.field_types must go before other interaction with the result. Otherwise you will # get a seg fault. Seems to be a bug in Mysql2. # TODO: stream this and render results on the client as they are returned? { columns: query_result.fields, column_types: MysqlTypes.map_to_google_charts_types(query_result.field_types), total_rows: query_result.size, rows: (query_result.to_a || []).take(Sqlui::MAX_ROWS) } end result[:selection] = params[:selection] result[:query] = params[:sql] status 200 headers 'Content-Type' => 'application/json; charset=utf-8' body result.to_json end get "#{database.url_path}/download_csv" do break client_error('missing sql') unless params[:sql] sql = Base64.decode64(params[:sql]).force_encoding('UTF-8') logger.info "sql: #{sql}" variables = params.map { |k, v| k[0] == '_' ? [k, v] : nil }.compact.to_h sql = find_selected_query(sql, params[:selection]) logger.info "sql: #{sql}" content_type 'application/csv; charset=utf-8' 'result.csv' status 200 database.with_client do |client| query_result = execute_query(client, variables, sql) stream do |out| out << CSV::Row.new(query_result.fields, query_result.fields, header_row: true).to_s.strip query_result.each do |row| out << "\n#{CSV::Row.new(query_result.fields, row).to_s.strip}" end end end end get(%r{#{Regexp.escape(database.url_path)}/(query|graph|structure|saved)}) do @html ||= File.read(File.join(resources_dir, 'sqlui.html')) status 200 headers 'Content-Type' => 'text/html; charset=utf-8' body @html end end error do |e| status 500 headers 'Content-Type' => 'application/json; charset=utf-8' = e..lines.first&.strip || 'unexpected error' result = { error: , stacktrace: e.backtrace.map { |b| b }.join("\n") } body result.to_json end run! end |