Skip to content

Instantly share code, notes, and snippets.

@greyblake
Created October 2, 2012 18:22
Show Gist options
  • Save greyblake/3822028 to your computer and use it in GitHub Desktop.
Save greyblake/3822028 to your computer and use it in GitHub Desktop.
Serg server
#!/usr/bin/env ruby
require 'cgi'
# Refresh period
PERIOD = 1 # seconds
# Calculate random value from 1 to 10
value = rand(10) + 1
# Create CGI object
cgi = CGI.new('html4')
# Build HTML ouput with CGI DSL.
cgi.out do
cgi.head do
cgi.meta('http-equiv' => 'refresh', 'content' => PERIOD.to_s)
end +
cgi.html do
cgi.body do
cgi.p { "value = #{value}" }
end
end
end
require 'webrick'
# Current directory
root = File.dirname(__FILE__)
# Initialize server with port and DocumentRoot dir
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
# Set signal handler to shutdown server when you press Ctrl+C
trap 'INT' do server.shutdown end
# Run server on 8000 port
server.start
@greyblake
Copy link
Author

  • Place those both files in same directory
  • Make index.cgi be executable: run chmod +x ./index.cgi
  • Start server ruby ./server.rb
  • Open browser and visit http://localhost:8000

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