require 'p4_web_api_client' require 'sinatra/base' require 'rack/session/file' module Pms # Handles the login cases of the web application class Login < Sinatra::Base use Rack::Session::File, :driver => :YAML, :storage => (ENV['SESSION_DIR'] || Dir.tmpdir) # TODO Allow this registration to be included in any app register do def auth(type) condition do redirect '/login' unless send('is_#{type}?') end end end configure do set :p4_web_api_url, 'http://example.com' end helpers do # TODO share this helper def is_user? session[:user] != nil end def log_out session[:user] = nil session[:session_token] = nil end def log_in(user, password) uri = URI(settings.p4_web_api_url) conn_opts = { :url => uri.to_s, :user => user, :password => password } if uri.path conn_opts[:prefix] = uri.path end client = nil begin client = P4WebApiClient::Client.new(conn_opts) rescue P4WebApiClient::Errors::Unauthenticated client = nil end if client session[:user] = client.connection.user session[:session_token] = client.connection.session_token end return !client.nil? end end # We share the existing user name to the session if it exists get '/login' do haml :login, :locals => { :user => session[''] } end post '/login' do if log_in(params[:user], params[:password]) redirect to('/') else redirect to('/login') end end get '/logout' do log_out redirect to('/login') end end end
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 13707 | tjuricek |
Infrastructure for including a "project management" React application. This attempts to create a fairly simple installer that creates a 'static' folder based on ui/static that gets hosted by the nginx front end. Right now, it's the only app, so the default page is this application. It was called "pws2" during a prototyping phase. Another prototype, "pws" and the related "project" module, is removed since that was a Sinatra-based approach that will be much more difficult to integrate into anything else. I'm running into a couple of issues with notifications setup, it's still not 100%, so I'm disabling this for now from the default 'god' configuration. (The service isn't 100% functional yet, anyway.) |
||
#1 | 13635 | tjuricek |
Exploring modular web applications with Sinatra. If the app has fairly unique content per page, Sinatra works really well actually. (Mostly because it's a thin layer over Rack.) Sharing anything though, basically means implementing a module or base class and implementing it everywhere. An added benefit is that these applications could be used in Rails apps as well. I'd probably think hard about this, because Rails tends to be a resource hog. Here's an example config.ru I used for running against the dev system: require 'pms' app = Pms::App.new Pms::Login.settings.p4_web_api_url = http://172.16.100.20/p4_web_api/v1 ProjectApp.settings.phoenix_services_url = http://172.16.100.20/p4_phoenix_services/v1 app.settings.static = :true app.settings.public_folder = File.absolute_path('../../ui/pms', __FILE__) puts "public_folder is #{app.settings.public_folder}" run app |