<commit_before>require 'rubygems'
require 'rack'

require 'certmeister'
require 'certmeister/redis/store'
require 'redis'

allow = Certmeister::Policy::Noop.new

ca = Certmeister.new(
  Certmeister::Config.new(
    sign_policy: allow,
    fetch_policy: allow,
    remove_policy: allow,
    store: Certmeister::Redis::Store.new(Redis.new, "development"),
    ca_cert: File.read("../fixtures/ca.crt"),
    ca_key: File.read("../fixtures/ca.key"),
  )
)

sign_action = ->(params) do
  response = ca.sign(params)
  if response.error?
    [500, {'Content-Type' => 'text/plain'}, ["500 Internal Server Error (#{response.error})"]]
  elsif response.denied?
    [403, {'Content-Type' => 'text/plain'}, ["403 Forbidden (#{response.error})"]]
  else
    [303, {'Content-Type' => 'text/plain',
           'Location' => "/certificate/#{params[:cn]}"}, ["303 See Other"]]
  end
end

fetch_action = ->(params) do
  response = ca.fetch(params)
  if response.error?
    [500, {'Content-Type' => 'text/plain'}, ["500 Internal Server Error (#{response.error})"]]
  elsif response.denied?
    [403, {'Content-Type' => 'text/plain'}, ["403 Forbidden (#{response.error})"]]
  elsif response.miss?
    [404, {'Content-Type' => 'text/plain'}, ["404 Not Found"]]
  else
    [200, {'Content-Type' => 'application/x-pem-file'}, [response.pem]]
  end
end

remove_action = ->(params) do
  response = ca.remove(params)
  if response.error?
    [500, {'Content-Type' => 'text/plain'}, ["500 Internal Server Error (#{response.error})"]]
  elsif response.denied?
    [403, {'Content-Type' => 'text/plain'}, ["403 Forbidden (#{response.error})"]]
  elsif response.miss?
    [404, {'Content-Type' => 'text/plain'}, ["404 Not Found"]]
  else
    [200, {'Content-Type' => 'text/plain'}, ["200 OK"]]
  end
end

router = ->(env) do
  req = Rack::Request.new(env)
  if req.path_info =~ /^\/certificate\/(.+)/
    params = req.params.tap do |p|
      p[:cn] = $1
      p[:ip] = req.ip
    end
    case req.request_method
      when 'POST' then sign_action.call(params)
      when 'GET' then fetch_action.call(params)
      when 'DELETE' then remove_action.call(params)
      else [405, {'Content-Type' => 'text-plain'}, ["405 Method Not Allowed"]]
    end
  else
    [501, {'Content-Type' => 'text-plain'}, ["501 Not Implemented"]]
  end
end

run router<commit_msg>Update contrib example to use certmeister/rack/app<commit_after>require 'rubygems'
require 'rack'

require 'certmeister'
require 'certmeister/redis/store'
require 'certmeister/rack/app'
require 'redis'

allow = Certmeister::Policy::Noop.new

ca = Certmeister.new(
  Certmeister::Config.new(
    sign_policy: allow,
    fetch_policy: allow,
    remove_policy: allow,
    store: Certmeister::Redis::Store.new(Redis.new, "development"),
    ca_cert: File.read("../fixtures/ca.crt"),
    ca_key: File.read("../fixtures/ca.key"),
  )
)
certmeister = Certmeister::Rack::App.new(ca)

app = Rack::Builder.new do
  map "/ca" do
    run certmeister
  end
end

run app
{'fraction_non_alphanumeric': 0.1851106639839034, 'fraction_numerical': 0.025486250838363516, 'mean_word_length': 3.8346839546191247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2593447', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1056, 'n_words': 238}