lib/cryptoform/server.rb in cryptoform-0.2.0 vs lib/cryptoform/server.rb in cryptoform-0.4.0

- old
+ new

@@ -1,56 +1,21 @@ # frozen_string_literal: true class Cryptoform::Server def initialize(config) - @config = config - end + @server = WEBrick::HTTPServer.new( + Port: config.port, + BindAddress: "0.0.0.0", + AccessLog: [ + [$stdout, WEBrick::AccessLog::COMMON_LOG_FORMAT], + [$stdout, WEBrick::AccessLog::REFERER_LOG_FORMAT] + ] + ) + trap("INT") { @server.shutdown } - def run - logger.info { "Cryptoform is listening on #{endpoint.url}..." } - Async::HTTP::Server.for(endpoint) do |request| - log_request(request) { handle_request(request) } - end.run + @server.mount("/", Rackup::Handler::WEBrick, Cryptoform::Application.new(config)) end - private - - def handle_request(request) # rubocop:disable Metrics/AbcSize - name = request.path.split("/")&.[](1)&.to_sym - return ::Protocol::HTTP::Response[404, {}, []] unless @config.states.key?(name) - - handler_name = :"#{request.method.downcase}_state" - return ::Protocol::HTTP::Response[405, {}, []] unless respond_to?(handler_name, true) - - send(handler_name, @config.states[name], request:) - rescue Cryptoform::StateMissingError - ::Protocol::HTTP::Response[404, {}, []] - rescue StandardError => e - logger.error(e) - ::Protocol::HTTP::Response[500, {}, []] - end - - def get_state(state_config, *) - state = state_config.encryption_backend.decrypt(state_config.storage_backend.read) - ::Protocol::HTTP::Response[200, {}, [JSON.pretty_generate(state)]] - end - - def post_state(state_config, request:) - state_config.storage_backend.write(state_config.encryption_backend.encrypt(request.body.read)) - - ::Protocol::HTTP::Response[201, {}, []] - end - - def logger - @logger ||= Logger.new($stdout) - end - - def endpoint - @endpoint ||= Async::HTTP::Endpoint.parse("http://localhost:#{@config.port}") - end - - def log_request(request) - yield.tap do |response| - logger.info { "#{request.method} #{request.path}: #{response.status}" } - end + def run + @server.start end end