-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp_context.cr
58 lines (47 loc) · 1.19 KB
/
http_context.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
require "http/server"
require "../src/honeybadger"
class Router
include HTTP::Handler
def initialize
@matches = {} of String => Proc(String)
end
def call(context)
if route = @matches[context.request.path]?
context.response.print route.call
else
call_next context
end
end
def on(path : String, &block : -> String) : Nil
@matches[path] = block
end
end
class MyHoneybadgerNotifier < Honeybadger::Handler
def context : Honeybadger::ContextHash
Honeybadger::ContextHash.new.tap do |c|
c["user_id"] = user_id
end
end
def user_id
23
end
end
router = Router.new
router.on("/raise") do
raise "Broken!"
end
honeybadger_api_key = ENV["HONEYBADGER_API_KEY"]? || "00000000"
Honeybadger.configure(api_key: honeybadger_api_key)
server = HTTP::Server.new([
HTTP::LogHandler.new(Log.for("http.server")),
HTTP::ErrorHandler.new,
MyHoneybadgerNotifier.new,
router
]) do |http_context|
http_context.response.content_type = "text/html"
http_context.response.status = HTTP::Status::NOT_FOUND
http_context.response.print "<strong>Not found.</strong>"
end
address = server.bind_tcp 8080
puts "Listening on http://#{address}"
server.listen