This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin support and status plugin
- Loading branch information
Showing
3 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<html> | ||
<head> | ||
<title>Puma Express Status</title> | ||
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css"> | ||
<style> | ||
body { | ||
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="navbar navbar-fixed-top"> | ||
<div class="navbar-inner"> | ||
<div class="container"> | ||
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> | ||
<span class="icon-bar"></span> | ||
<span class="icon-bar"></span> | ||
<span class="icon-bar"></span> | ||
</a> | ||
<a class="brand" href="#">Puma Express Status</a> | ||
<div class="nav-collapse"> | ||
<ul class="nav"> | ||
<li class="active"><a href="#">Home</a></li> | ||
<li><a href="#about">About</a></li> | ||
<li><a href="#contact">Contact</a></li> | ||
</ul> | ||
</div><!--/.nav-collapse --> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="container"> | ||
<% apps.each do |h,a| %> | ||
<i class="icon-star"></i> <strong><%= h %></strong> | ||
<ul> | ||
<% hits[a].reverse_each do |hit| %> | ||
<div class="hit"> | ||
<h1> | ||
<span class="label label-success"><%= hit.code %></span> | ||
<%= hit.path %> | ||
</h1> | ||
|
||
<dl> | ||
<dt>Request</dt> | ||
<dd> | ||
<table class="table table-striped table-condensed"> | ||
<tbody> | ||
<% hit.sorted_request.each do |k,v| %> | ||
<tr><th><%= k %></th><td><code><%= v %></code></td></tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</dd> | ||
<dt>Response</dt> | ||
<dd> | ||
<table class="table table-striped table-condensed"> | ||
<tbody> | ||
<tr> | ||
<th>Code</th> | ||
<td><span class="label label-success"><%= hit.code %></span></td> | ||
</tr> | ||
<tr> | ||
<th>Headers</th> | ||
<td> | ||
<table class="table table-striped table-condensed"> | ||
<% hit.sorted_response.each do |k,v| %> | ||
<tr><th><%= k %></th><td><code><%= v %></td></code></tr> | ||
<% end %> | ||
</table> | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>Body</th> | ||
<td> | ||
<code><%= hit.body %></code> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</dd> | ||
</div> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'erb' | ||
require 'thread' | ||
|
||
class Puma::Express | ||
class Status | ||
|
||
Puma::Express.add_plugin 'status', self | ||
|
||
TEMPLATE = File.expand_path "../status.erb", __FILE__ | ||
|
||
def initialize(express) | ||
@express = express | ||
@mutex = Mutex.new | ||
@hits = Hash.new { |h,k| h[k] = [] } | ||
@hit_window = 10 | ||
end | ||
|
||
def handle?(env) | ||
env['HTTP_HOST'].split(".").first == "status" | ||
end | ||
|
||
class Hit | ||
def initialize(req, res) | ||
@request = req | ||
@result = res | ||
end | ||
|
||
attr_reader :request, :result | ||
|
||
def path | ||
@request['PATH_INFO'] | ||
end | ||
|
||
def code | ||
@result.first | ||
end | ||
|
||
def body | ||
@result.last.join | ||
end | ||
|
||
def sorted_request | ||
headers = @request.find_all { |k,v| k[0,5] == "HTTP_" } | ||
headers.map! do |k,v| | ||
[k[5..-1].split("_").map { |x| x.downcase.capitalize }.join("-"), v] | ||
end | ||
|
||
headers.sort_by { |k,v| k } | ||
end | ||
|
||
def sorted_response | ||
@result[1].sort_by { |k,v| k } | ||
end | ||
end | ||
|
||
def add_result(app, req, res) | ||
p :res => res | ||
|
||
@mutex.synchronize do | ||
hits = @hits[app] | ||
if hits.size >= @hit_window | ||
hits.shift | ||
end | ||
|
||
hits.push Hit.new(req, res) | ||
end | ||
end | ||
|
||
def call(env) | ||
apps = @express.apps.sort_by { |h,a| h } | ||
hits = @hits | ||
|
||
body = ERB.new(File.read(TEMPLATE)).result(binding) | ||
[200, {}, body] | ||
end | ||
end | ||
end |