Last active
August 29, 2015 13:57
-
-
Save LutherBaker/9862813 to your computer and use it in GitHub Desktop.
Localhost development against a Sinatra server simulating a full blown stack
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
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