Last active
February 29, 2016 13:47
-
-
Save jvmvik/9d5ae6b6a03487ba6f47 to your computer and use it in GitHub Desktop.
Ruby Webrick: Example of Virtual Host.
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
| # Ruby Webrick example of Virtual Host | |
| # | |
| # Your directory must contain a public/index.html file. | |
| # | |
| require 'webrick' | |
| require 'json' | |
| # Daemonize (production) | |
| #WEBrick::Daemon.start | |
| # Start frontend | |
| server = WEBrick::HTTPServer.new({ | |
| :Port => 9000, | |
| :BindAddress => '0.0.0.0' | |
| }) | |
| # Start virtual host | |
| host = WEBrick::HTTPServer.new({ | |
| :DoNotListen => true, | |
| :Port => nil, | |
| :DocumentRoot => __dir__ + '/public', | |
| :ServerName => 'localhost' # Change this to your server in production. | |
| }) | |
| # Example of query. | |
| def ok(obj, res) | |
| res.status = 200 | |
| res['Content-Type'] = 'application/json' | |
| res.body = JSON.pretty_generate(obj).to_s | |
| end | |
| host.mount_proc '/hello' do |req, res| | |
| obj = { | |
| :status => 'ok', | |
| :msg => 'server is online' | |
| } | |
| ok(obj, res) | |
| end | |
| # Stop server on interrupt | |
| trap 'INT' do | |
| server.shutdown | |
| end | |
| # Connect virtual host to frontend | |
| server.virtual_host(host) | |
| # Start frontend | |
| server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment