Skip to content

Instantly share code, notes, and snippets.

@DouglasAllen
Forked from ramn/webrick_and_erb.rb
Last active August 29, 2015 14:27
Show Gist options
  • Save DouglasAllen/b0b5c254c60eec44b7d9 to your computer and use it in GitHub Desktop.
Save DouglasAllen/b0b5c254c60eec44b7d9 to your computer and use it in GitHub Desktop.
Simple ruby webserver
require 'webrick'
require 'erb'
template = <<TEMPLATE
<html>
<head>
<title>Ruby as PHP</title>
</head>
<body>
<h1>Loop</h1>
<% 1.upto(5).each do |i| %>
<p>Number <%= i %></p>
<% end %>
</body>
TEMPLATE
server = WEBrick::HTTPServer.new :Port => 8081
server.mount_proc '/' do |req, res|
res.body = ERB.new(template).result(binding)
end
trap 'INT' do server.shutdown end
server.start
@DouglasAllen
Copy link
Author

My Webrick session in irb is denying me any access. see output below:

[2015-08-15 08:40:05] INFO  WEBrick 1.3.1
[2015-08-15 08:40:05] INFO  ruby 2.2.2 (2015-04-13) [i686-linux]
Errno::EACCES: Permission denied - bind(2) for [::]:80

Obviously my options didn't through.

@DouglasAllen
Copy link
Author

The error was simple. I did not capitalize port.

@DouglasAllen
Copy link
Author

 server = WEBrick::HTTPServer.new Port: 8081
[2015-08-15 08:47:56] INFO  WEBrick 1.3.1
[2015-08-15 08:47:56] INFO  ruby 2.2.2 (2015-04-13) [i686-linux]
=> 
#<WEBrick::HTTPServer:0xb7ec8df0 
@config={
  :ServerName=>"swancygnet",
  :BindAddress=>nil, 
  :Port=>8081,
  :MaxClients=>100, 
  :ServerType=>nil, 
  :Logger=>#<WEBrick::Log:0xb7ec8db4 
    @level=4, 
    @log=#<IO:<STDERR>>, 
    @time_format="[%Y-%m-%d %H:%M:%S]">, 
  :ServerSoftware=>"WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13)", 
  :TempDir=>"/tmp", 
  :DoNotListen=>false, 
  :StartCallback=>nil, 
  :StopCallback=>nil,
  :AcceptCallback=>nil, 
  :DoNotReverseLookup=>nil, 
  :ShutdownSocketWithoutClose=>false,
  :RequestTimeout=>30,  
  :HTTPVersion=>#<WEBrick::HTTPVersion:0xb80a60dc 
    @minor=1, 
    @major=1>, 
  :AccessLog=>[[#<IO:<STDERR>>, 
    "%h %l %u %t \"%r\" %s %b"], [#<IO:<STDERR>>, 
    "%{Referer}i -> %U"]], 
  :MimeTypes=>{
    "ai"=>"application/postscript", "asc"=>"text/plain", "avi"=>"video/x-msvideo",
    "bin"=>"application/octet-stream", "bmp"=>"image/bmp",
    "class"=>"application/octet-stream", "cer"=>"application/pkix-cert",
    "crl"=>"application/pkix-crl", "crt"=>"application/x-x509-ca-cert", 
    "css"=>"text/css", "dms"=>"application/octet-stream", 
    "doc"=>"application/msword", "dvi"=>"application/x-dvi", 
    "eps"=>"application/postscript", "etx"=>"text/x-setext", 
    "exe"=>"application/octet-stream", "gif"=>"image/gif", "htm"=>"text/html", 
    "html"=>"text/html", "jpe"=>"image/jpeg", "jpeg"=>"image/jpeg", 
    "jpg"=>"image/jpeg", "js"=>"application/javascript", 
    "lha"=>"application/octet-stream", "lzh"=>"application/octet-stream", 
    "mov"=>"video/quicktime", "mpe"=>"video/mpeg", "mpeg"=>"video/mpeg", 
    "mpg"=>"video/mpeg", "pbm"=>"image/x-portable-bitmap", 
    "pdf"=>"application/pdf", "pgm"=>"image/x-portable-graymap", 
    "png"=>"image/png", "pnm"=>"image/x-portable-anymap", 
    "ppm"=>"image/x-portable-pixmap", 
    "ppt"=>"application/vnd.ms-powerpoint", "ps"=>"application/postscript", 
    "qt"=>"video/quicktime", "ras"=>"image/x-cmu-raster", "rb"=>"text/plain", 
    "rd"=>"text/plain", "rtf"=>"application/rtf", "sgm"=>"text/sgml", 
    "sgml"=>"text/sgml", "svg"=>"image/svg+xml", "tif"=>"image/tiff", 
    "tiff"=>"image/tiff", "txt"=>"text/plain", "xbm"=>"image/x-xbitmap", 
    "xhtml"=>"text/html", "xls"=>"application/vnd.ms-excel", "xml"=>"text/xml", 
    "xpm"=>"image/x-xpixmap", "xwd"=>"image/x-xwindowdump", 
    "zip"=>"application/zip"},

  :DirectoryIndex=>["index.html", "index.htm", "index.cgi", "index.rhtml"], 
  :DocumentRoot=>nil, 
  :DocumentRootOptions=>{:FancyIndexing=>true}, 
  :RequestCallback=>nil, 
  :ServerAlias=>nil, 
  :InputBufferSize=>65536, 
  :OutputBufferSize=>65536, 
  :ProxyAuthProc=>nil, 
  :ProxyContentHandler=>nil, 
  :ProxyVia=>true, 
  :ProxyTimeout=>true, 
  :ProxyURI=>nil, 
  :CGIInterpreter=>nil, 
  :CGIPathEnv=>nil, 
  :Escape8bitURI=>false}, 
@status=:Stop, 
@logger=#<WEBrick::Log:0xb7ec8db4 
  @level=4, 
  @log=#<IO:<STDERR>>, 
  @time_format="[%Y-%m-%d %H:%M:%S]">, 
@tokens=#<Thread::SizedQueue:0xb7ec8d64>, 
@listeners=[#<TCPServer:fd 9>, #<TCPServer:fd 10>], 
@shutdown_pipe=[#<IO:fd 11>, #<IO:fd 12>], 
@http_version=#<WEBrick::HTTPVersion:0xb80a60dc 
  @minor=1, 
  @major=1>, 
@mount_tab=#<WEBrick::HTTPServer::MountTable:0xb7ec9a34 
  @tab={}, 
  @scanner=/^()(?=\/|$)/>, 
@virtual_hosts=[]>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment