Skip to content

Instantly share code, notes, and snippets.

@LutherBaker
Last active August 29, 2015 13:57
Show Gist options
  • Save LutherBaker/9862813 to your computer and use it in GitHub Desktop.
Save LutherBaker/9862813 to your computer and use it in GitHub Desktop.
Localhost development against a Sinatra server simulating a full blown stack
require 'sinatra'
require 'json'
require "sinatra/cookies"
session_timeout_seconds = 30
before do
content_type 'application/json'
end
post '/authentication' do
logger.info "Trying to authenticate ..."
data = JSON.parse(request.body.read)
if (data['username'] == 'luther' && data['password'] == 'baker')
now = Time.now
sessionValue = now.to_i + session_timeout_seconds
"{ \"session\" : \"SESSION=#{sessionValue}\" }"
else
logger.info "Authentication Failed: username='" + data['username'] +
"' password='" + data['password'] + "'"
status 500
end
end
get '/example' do
now = Time.now
session_timeout = cookies[:SESSION].to_i
current_time = now.to_i
if session_timeout > current_time
File.read(File.join('data', 'example.json'))
else
logger.info "Session Timed Out!"
status 500
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment