Last active
April 21, 2018 19:52
-
-
Save glurp/face5c9dc2729b1dcc2efbe0ead940ce to your computer and use it in GitHub Desktop.
router test : little http server for get local data (dir,file,proc) with Crystal lang and 'router' http server
This file contains hidden or 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
require "router" | |
require "io/memory" | |
############################################################################ | |
# Tools | |
############################################################################ | |
def to_table(ll) | |
ll.map {|l| | |
"<tr><td>#{l.join("</td><td>")}</td></tr>" | |
}.join("\n") | |
end | |
def execute_cmd_to_table(cmd,config={} of Symbol => Int32) | |
mincol = config[:mincol]? || 100 | |
io = IO::Memory.new | |
Process.run(cmd, shell: true, output: io) | |
output = io.to_s | |
to_table( output.split(/\r?\n/).map {|line| | |
l= yield(line) | |
l[0...mincol]+(l.size>mincol ? [l[mincol..-1].join(" ")] : [] of String ) | |
}) | |
end | |
def head | |
"<style> | |
body {margin: 0px;} | |
div {border: 2px solid black;margin-left: 20px;} | |
h1,h2,h3 {color: #BBBBDD; background-color: #004455; text-align: center; padding: 10px;} | |
table {border-collapse: collapse;border: 2px solid black;margin-left: 20px;} | |
th,td {border: 1px solid black;padding: 3px 5px 2px 3px} | |
table td, table td * { vertical-align: top;} | |
</style>" | |
end | |
class WebServer | |
include Router | |
def dec(href) href.gsub("%2F","/") end | |
def enc(href) href.gsub("/","%2F") end | |
def flat(txt) txt.gsub("&","&").gsub("<","<").gsub(">",">") end | |
def draw_routes | |
get "/" do |context, params| | |
content="<html><body><h3>Hello CRRouter!</h3> | |
<a href='/time'>Time</a> | |
<a href='/ls/.'>Directory</a> | |
<a href=/proc>Process</a> | |
<a href=/partitions>Partitions</a> | |
<a href=/ipv4>Net Ipv4</a> | |
<a href=/interfaces>Interfaces</a> | |
<a href=/memory>RAM</a> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/time" do |context, params| | |
context.response.print Time.now.to_s | |
context | |
end | |
get "/ls/*dir" do |context, q| | |
dir=(q["dir"]? !=nil ? q["dir"] : ".").gsub("%2F","/") | |
content=to_table( Dir.glob("#{dir}/*").map {|line| | |
stat=File.stat(line) | |
[ | |
line, | |
File.file?(line) ? stat.size : "<a href=/ls/#{enc(line)}>Go</a>", | |
File.file?(line) ? "<a href='/file/#{enc(line)}'>See</a>" : "", | |
stat.mtime() | |
] | |
}) | |
title="Directory of #{dir}" | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<code><pre><table><tr><th>Name</th><th>Size</th><th>url</th><th>Time</th></tr>#{content}</table></pre></code> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/file/*file" do |context, q| | |
file=dec( q["file"]? == nil ? "" : q["file"] ).gsub("././","./") | |
title="File #{file}" | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<div><code><pre>#{flat(File.read(file))}</pre></code></div> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/proc" do |context, q| | |
table=execute_cmd_to_table("ps -ef",{mincol: 7 }) {|line| line.split(/\s+/)} | |
title="Process running..." | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<code><pre><table>#{table}</table></pre></code> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/partitions" do |context, q| | |
table=execute_cmd_to_table("df -h") {|line| line.split(/\s+/)} | |
title="Partitions" | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<code><pre><table>#{table}</table></pre></code> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/ipv4" do |context, q| | |
table1=execute_cmd_to_table("echo IpV4-Key:Value; grep '' /proc/sys/net/ipv4/*",) {|line| | |
line.split(":",2) | |
} | |
table2=execute_cmd_to_table("echo Net-Core-Key:Value; grep '' /proc/sys/net/core/*",) {|line| | |
a=line.split(":",2) | |
a[1]=a.last.scan(/.{40,40}/).map {|md| md[0]}.join("<br/>") if a.last.size> 50 | |
p a | |
a | |
} | |
title="Kernel tuning IPV4" | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<table><tr> | |
<td><code><pre><table>#{table1}</table></pre></code></td> | |
<td><code><pre><table>#{table2}</table></pre></code></td> | |
</tr></table> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/interfaces" do |context, q| | |
table=execute_cmd_to_table("netstat -i") {|line| line.split(/\s+/)} | |
title="IP Interfaces" | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<code><pre><table>#{table}</table></pre></code> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/memory" do |context, q| | |
table=execute_cmd_to_table("free -h") {|line| | |
a=line.split(":",2) | |
l=a.last.split(/\s+/) | |
[a.first] + l | |
} | |
title="IP Interfaces" | |
content="<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<code><pre><table>#{table}</table></pre></code> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
context.response.print content | |
context | |
end | |
get "/bidon" do |context, params| | |
content="" | |
context.response.print content | |
context | |
end | |
end | |
def run | |
puts "Start on :3000 ?..." | |
server = HTTP::Server.new(3000, route_handler) | |
puts "Ready on :3000" | |
server.listen | |
end | |
end | |
web_server = WebServer.new | |
web_server.draw_routes | |
web_server.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Performance with my (intel core i5 2.5Hz) Laptop, on Windows 7 => Virtualbox => Ubuntu 17 : 11000 requests/secs ! (with ab near 100% cpu) : 11000 Request/seconds ! :