Last active
April 19, 2018 01:24
-
-
Save glurp/066e7b6984c258197541a41fdc8d3fe1 to your computer and use it in GitHub Desktop.
kemal test : little http server for get local data (dir,file,proc) with Crystal lang
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
require "../src/kemal.cr" | |
require "io/memory" | |
############################################################################ | |
# Tools | |
############################################################################ | |
def to_table(ll) | |
ll.map {|l| | |
"<tr><td>#{l.join("</td><td>")}</td></tr>" | |
}.join("\n") | |
end | |
def head | |
"<style> | |
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} | |
</style>" | |
end | |
############################################################################ | |
# Routes | |
############################################################################ | |
get "/" do | |
"Hello Kemal! | |
<a href=time>Time</a> | |
<a href=ls>Directory</a> | |
<a href=proc>Process</a> | |
" | |
end | |
get "/time" do | |
"It is exactly #{Time.now}" | |
end | |
get "/ls" do |env| | |
q=env.params.query | |
dir=q["dir"]? !=nil ? q.["dir"] : "." | |
content=to_table( Dir.glob("#{dir}/*").map {|line| | |
stat=File.stat(line) | |
[ | |
line, | |
File.file?(line) ? stat.size : "<a href=/ls?dir=#{dir}/#{line}>Go</a>", | |
File.file?(line) ? "<a href=/file?file=#{line}>See</a>" : "", | |
stat.mtime() | |
] | |
}) | |
title="Directory of #{Dir.current}" | |
"<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>" | |
end | |
get "/file" do |env| | |
q=env.params.query | |
file=( q["file"]? == nil ? "" : q["file"] ).gsub("././","./") | |
puts file | |
title="File #{file}" | |
"<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<div><code><pre>#{File.read(file)}</pre></code></div> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
end | |
get "/proc" do |env| | |
command = "ps -ef" | |
io = IO::Memory.new | |
Process.run(command, shell: true, output: io) | |
output = io.to_s | |
content=to_table( output.split(/\r?\n/).map {|line| | |
l=line.split(/\s+/) +[""] | |
l[0..6]+(l.size>7 ? [l[7..-1].join(" ")] : [] of String ) | |
}) | |
title="Process running..." | |
"<html><head>#{head}</head> | |
<body><h2>#{title}</h2><br/> | |
<code><pre><table>#{content}</table></pre></code> | |
<br><h3>#{Time.now}</h3> | |
</body></html>" | |
end | |
Kemal.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Performance with my (intel 2 cores) Laptop, on Ubuntu / Virtualbox (under Windows 7) : 5000 requests/secs