Skip to content

Commit

Permalink
adds a sessions storage system
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Jan 6, 2016
1 parent 4c86805 commit f08dd59
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Common < Sinatra::Base
set :config, Hieracles::Config.new({ config: configfile })
enable :session
enable :logging
set :store, Hieraviz::Store.new
end

end
Expand Down
1 change: 1 addition & 0 deletions app/config/hieraviz.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ app_name: HieraViz
basepath: "../puppet"
classpath: "farm_modules/%s/manifests/init.pp"
hierafile: "dev/hiera-local.yaml"
tmpdir: "/tmp"
session_seed: "xxx"
usedb: true
puppetdb:
Expand Down
4 changes: 4 additions & 0 deletions app/views/data.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="meat text">
<h1>Raw Data</h1>
<pre><%= @data %></pre>
</div>
2 changes: 1 addition & 1 deletion app/views/home.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="meat text">
Welcome to hieraviz<br>
<%= session['access_token'] %><br>
<pre><%= settings.store.get session['access_token'] if session['access_token'] %></pre>
<pre><%= Hieraviz::Store.get session['access_token'] if session['access_token'] %></pre>
</div>
3 changes: 2 additions & 1 deletion app/views/store.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="meat text">
<h1>Store</h1>
<%= session['access_token'] %><br>
<pre><%= settings.store.dump %></pre>
<pre><%= Hieraviz::Store.dump %></pre>
<pre><%= Hieraviz::Store.tmpdir %></pre>
</div>
14 changes: 11 additions & 3 deletions app/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'sinatra/flash'

require 'better_errors'
require 'digest/sha1'
require 'dotenv'
require 'oauth2'

Expand All @@ -20,7 +19,6 @@ class Web < Common
set :session_secret, settings.configdata['session_seed']
set :public_folder, Proc.new { File.join(root, "public") }
set :views_folder, Proc.new { File.join(root, "views") }
set :store, Hieraviz::Store.new
set :erb, layout: :_layout
enable :sessions
end
Expand Down Expand Up @@ -70,7 +68,7 @@ def check_authorization
get '/logged-in' do
access_token = settings.oauth.access_token(request, params[:code])
session[:access_token] = access_token.token
settings.store.set access_token.token, settings.oauth.user_info(access_token.token)
Hieraviz::Store.set access_token.token, settings.oauth.user_info(access_token.token)
flash['info'] = "Successfully authenticated with the server"
redirect '/'
end
Expand Down Expand Up @@ -111,9 +109,19 @@ def check_authorization
end

get '/store' do
# Hieraviz::Store.set 'woot', 'nada'
erb :store
end

get '/user' do
if session[:access_token]
@data = settings.oauth.user_info(session[:access_token])
else
@data = 'nada'
end
erb :data
end

not_found do
erb :not_found, layout: :_layout
end
Expand Down
37 changes: 31 additions & 6 deletions lib/hieraviz/store.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
module Hieraviz
class Store
module Store
extend self

def initialize
@data = {}
def data
@_data ||= {}
end

def set(key, value)
@data[key.to_sym] = value
File.open(tmpfile(key), 'w') do |f|
f.print Marshal::dump(value)
end
data[key] = value
end

def get(key)
@data[key.to_sym]
data[key] ||= Marshal::load(File.read(tmpfile(key)).chomp)
end

def dump
@data
data
end

def tmpfile(name)
File.join tmpdir, name.gsub(/[^a-z0-9]/,'')
end

def tmpdir
@_tmpdir ||= init_tmpdir
end

def init_tmpdir
configfile = ENV['HIERAVIZ_CONFIG_FILE'] || File.join("config", "hieraviz.yml")
configfile = File.expand_path(File.join('../../../app', configfile), __FILE__) unless configfile[0] == '/'
config = YAML.load_file(configfile)
tmp = config['tmpdir'] || '/tmp'
begin
FileUtils.mkdir_p(tmp) unless Dir.exist?(tmp)
rescue Exception => e
tmp = '/tmp'
end
tmp
end

end
Expand Down

0 comments on commit f08dd59

Please sign in to comment.