Sinatra - How to Get the Server's Domain Name

Sinatra - how do I get the server's domain name

simply use request.host inside your code.

get  "/" do
puts request.host #=> localhost
end

Cross domain session with Sinatra and AngularJS

on the sinatra app

before do
headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Origin'] = 'http://localhost:4567'
headers['Access-Control-Allow-Headers'] = 'accept, authorization, origin'
headers['Access-Control-Allow-Credentials'] = 'true'
end

angularjs app

host='http://127.0.0.1:5445/'
@viewController = ($scope,$http)->
$scope.getCui = ()->
$http.get(host+'cui',{ withCredentials: true}).success (data)->
$scope.cui=data
console.log data

Explanation:
AngularJS uses his own cookie system, so we need to specify that we can pass the cookies trough the $http.get call using the {withCredentials:true} configuration object. Sinatra needs to accept the cross domain cookies so we need the headers mentioned above.
Note: 'Access-Control-Allow-Origin' header cannot be wildcard.

Why isn't my Sinatra app working with SSL?

Generally you don't want any ruby webservers actually handling SSL. You make them serve plain HTTP (that is accessible only via localhost). Then you install a reverse proxy that handles all of the SSL communicate.

For example

  1. Install nginx (reverse proxy) and configure it to listen on port 443.
  2. Set your
    ruby app server to listen on port 127.0.0.1:80 (accept local
    connections only)
  3. All requests hit nginx, which strips the SSL,
    and send the plain HTTP request to your ruby webserver.

A very simple nginx config to get you started:

ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/your.key;
ssl on;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

server {
listen 443 ssl;
server_name you.example.com;

location / {
proxy_pass http://localhost:8080; # your ruby appserver
}
}

How to get the computers domain name in matlab?

How about this:

[s,cout] = system('net config workstation | findstr /C:"Full Computer name"');
FQDN= strtrim(strrep(cout,'Full Computer name',''))
FQDN=
XXXX.YYYY.com

This returns the fully qualified domain name (FQDN) of the computer . Where XXXX is your PC Name and YYYY is the domain.

Similarly if you just want the domain name:

[s,cout] = system('systeminfo | findstr /C:"Domain:"');
Domain = strtrim(strrep(cout,'Domain:',''))
Domain =
YYYY.com

EDIT: You can also get the FQDN using java in matlab like this:

FQDN = java.net.InetAddress.getLocalHost.getCanonicalHostName 
FQDN =
XXXX.YYYY.com

Sinatra + Fibers + EventMachine

get '/some/route/' do
fib = Fiber.current
req = EM::SomeNonBlokingLib.request
req.callback do |response|
fib.resume(response)
end
req.errback do |err|
fib.resume(err)
end
Fiber.yield
end

EDIT

In your case you should spawn a Fiber for each request. So. Firstly create Rack config file and add some magick:

# config.ru
BOOT_PATH = File.expand_path('../http.rb', __FILE__)
require BOOT_PATH

class FiberSpawn
def initialize(app)
@app = app
end

def call(env)
fib = Fiber.new do
res = @app.call(env)
env['async.callback'].call(res)
end
EM.next_tick{ fib.resume }
throw :async
end
end

use FiberSpawn
run Http

Then your http Sinatra application:

# http.rb
require 'sinatra'
require 'fiber'

class Http < Sinatra::Base
get '/' do
f = Fiber.current
EM.add_timer(1) do
f.resume("Hello World")
end
Fiber.yield
end
end

Now you could run it under thin for example:

> thin start -R config.ru

Then if you will visit locakhost:3000 you'll see your Hello World message

Cannot access sinatra app through the local network

Try ruby app.rb -o 0.0.0.0 or ruby app.rb -e production. Either should work.



Related Topics



Leave a reply



Submit