Modernized the app with app structure from Stage Manager

This commit is contained in:
Gregory Ballantine 2023-03-31 11:52:37 -04:00
parent 4568733655
commit 72bb25a6ad
16 changed files with 285 additions and 78 deletions

21
.rubocop.yml Normal file
View File

@ -0,0 +1,21 @@
AllCops:
NewCops: enable
Layout/EmptyLinesAroundClassBody:
EnforcedStyle: 'empty_lines_except_namespace'
Layout/EmptyLinesAroundModuleBody:
EnforcedStyle: 'empty_lines_except_namespace'
Metrics/ClassLength:
Max: 150
Style/ClassVars:
Enabled: false
Style/GlobalVars:
Enabled: false
Style/MethodCallWithoutArgsParentheses:
Enabled: false
Style/MethodCallWithArgsParentheses:
Enabled: true
Style/RedundantReturn:
Enabled: false
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma

14
Gemfile
View File

@ -1,8 +1,16 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'sinatra', '~> 3.0' gem 'sinatra', '~> 3.0'
gem 'puma', '~> 6.0' gem 'puma', '~> 6.1'
gem 'rerun'
gem 'net-ldap', '~> 0.17' gem 'net-ldap', '~> 0.17'
group :development, :test do
# Use guard-rack gem to auto-reload app
gem 'guard-rack'
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
# rubocop and extensions for code style
gem 'rubocop'
end

View File

@ -1,42 +1,93 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
ast (2.4.2)
coderay (1.1.3)
ffi (1.15.5) ffi (1.15.5)
ffi (1.15.5-x64-mingw-ucrt) ffi (1.15.5-x64-mingw-ucrt)
listen (3.7.1) formatador (1.1.0)
guard (2.18.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (>= 1.0.12, < 2.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.13.0)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-rack (2.2.1)
ffi
guard (~> 2.3)
spoon
json (2.6.3)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3) rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10) rb-inotify (~> 0.9, >= 0.9.10)
lumberjack (1.2.8)
method_source (1.0.0)
mustermann (3.0.0) mustermann (3.0.0)
ruby2_keywords (~> 0.0.1) ruby2_keywords (~> 0.0.1)
nenv (0.3.0)
net-ldap (0.17.1) net-ldap (0.17.1)
nio4r (2.5.8) nio4r (2.5.8)
puma (6.0.0) notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
parallel (1.22.1)
parser (3.2.1.1)
ast (~> 2.4.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
puma (6.2.1)
nio4r (~> 2.0) nio4r (~> 2.0)
rack (2.2.4) rack (2.2.4)
rack-protection (3.0.4) rack-protection (3.0.4)
rack rack
rainbow (3.1.1)
rb-fsevent (0.11.2) rb-fsevent (0.11.2)
rb-inotify (0.10.1) rb-inotify (0.10.1)
ffi (~> 1.0) ffi (~> 1.0)
rerun (0.13.1) regexp_parser (2.7.0)
listen (~> 3.0) rexml (3.2.5)
rubocop (1.48.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.26.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5) ruby2_keywords (0.0.5)
shellany (0.0.1)
sinatra (3.0.4) sinatra (3.0.4)
mustermann (~> 3.0) mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4) rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.0.4) rack-protection (= 3.0.4)
tilt (~> 2.0) tilt (~> 2.0)
spoon (0.0.6)
ffi
thor (1.2.1)
tilt (2.0.11) tilt (2.0.11)
unicode-display_width (2.4.2)
wdm (0.1.1)
PLATFORMS PLATFORMS
x64-mingw-ucrt x64-mingw-ucrt
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
guard-rack
net-ldap (~> 0.17) net-ldap (~> 0.17)
puma (~> 6.0) puma (~> 6.1)
rerun rubocop
sinatra (~> 3.0) sinatra (~> 3.0)
wdm (>= 0.1.0)
BUNDLED WITH BUNDLED WITH
2.3.5 2.3.5

6
Guardfile Normal file
View File

@ -0,0 +1,6 @@
guard 'rack' do
watch('Gemfile.lock')
watch('config.ru')
watch('server.rb')
watch(%r{^(app)/.*})
end

View File

@ -1,11 +1,17 @@
require 'bundler/setup' require 'bundler/setup'
namespace :server do namespace :server do
task :dev do task :start do
%x{puma -C puma.rb} system('bundle exec puma -C config/puma.rb')
end end
task :reload do task :reload do
%x{rerun --no-notify 'puma -C puma.rb'} system('guard')
end
end
namespace :test do
task :lint do
system("rubocop app/ server.rb")
end end
end end

32
app/config.rb Normal file
View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'yaml'
# Configuration loader - loads config defaults and an optional config file
class Config
DEFAULT_CONFIG = 'config/defaults.yaml'
def initialize(config_path)
# Load the default config
@data = YAML.load_file(DEFAULT_CONFIG)
# End if the optional config file doesn't exist
return unless File.exist?(config_path)
# If the optional config exists, load it up!
@data.merge!(YAML.load_file(config_path))
end
def get(key)
bits = key.split('.')
value = @data
bits.each do |bit|
value = value[bit]
end
return value
end
end

View File

@ -1,8 +1,14 @@
class AuthController < Sinatra::Base # frozen_string_literal: true
require 'net/ldap'
class Webdap
# Handles /auth routes
class AuthController
get '/login' do get '/login' do
erb :'auth/login', :locals => { erb :'auth/login', locals: {
:title => 'Login to your account' title: 'Login to your account',
} }
end end
@ -16,11 +22,12 @@ class AuthController < Sinatra::Base
redirect '/account/view' redirect '/account/view'
else else
# Authentication failure # Authentication failure
erb :'auth/login', :locals => { erb :'auth/login', locals: {
:title => 'Login to your account', title: 'Login to your account',
:fail => true fail: true,
} }
end end
end end
end end
end

View File

@ -1,9 +1,14 @@
class IndexController < Sinatra::Base # frozen_string_literal: true
class Webdap
# Handles top-level routes
class IndexController
get '/' do get '/' do
erb :index, :locals => { erb :index, locals: {
:title => 'Home' title: 'Home',
} }
end end
end end
end

24
app/helpers.rb Normal file
View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
# ERB view helper functions
module Helpers
def nullable(value)
# Returns the value if it actually exists
return value if value && (value != '')
# Returns a default 'N/a' string
return 'N/a'
end
def date_format(date)
dt = date.to_datetime
return dt.strftime('%B %d, %Y, %I:%M:%S %p')
end
def date_format_input(date)
dt = date.to_datetime
return dt.strftime('%Y-%m-%dT%H:%M:%S')
end
end

View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
# Handles LDAP accounts
class Account
def name
return 'N/a'
end
end

View File

@ -1,10 +0,0 @@
class Sinatra::Base
configure do
enable :sessions
set :views, './views'
set :public_folder, './public'
end
end

View File

@ -1,22 +1,10 @@
require 'rubygems' # Load application config
require 'sinatra/base' require_relative 'app/config.rb'
require 'yaml' $conf = Config.new(File.join(__dir__, 'config/config.yaml'))
require_relative 'app/settings.rb' # Load Sinatra server
require_relative './server.rb'
defaultCnf = YAML::load_file(File.join(__dir__, 'config/defaults.yaml')) # Run application
cnf = YAML::load_file(File.join(__dir__, 'config/local.yaml')) run Webdap
cnf = defaultCnf.merge(cnf)
require 'net/ldap'
require_relative 'app/controllers/index.rb'
require_relative 'app/controllers/auth.rb'
map "/" do
run IndexController
end
map "/auth" do
run AuthController
end

View File

@ -1,3 +1,7 @@
ldap: ldap:
server_url: 'ldap://ldap.example.com' server_url: 'ldap://ldap.example.com'
port: 389 port: 389
server:
address: '127.0.0.1'
port: 4567

6
config/puma.rb Normal file
View File

@ -0,0 +1,6 @@
# Load application config
require './app/config.rb'
$conf = Config.new(File.join(__dir__, 'config/config.yaml'))
bind_address = "tcp://#{$conf.get('server.address')}:#{$conf.get('server.port')}"
bind bind_address

11
puma.rb
View File

@ -1,11 +0,0 @@
root = Dir.getwd.to_s
bind_address = '0.0.0.0'
bind_port = 3108
bind "tcp://#{bind_address}:#{bind_port}"
pidfile './tmp/puma.pid'
state_path './tmp/puma.state'
rackup root.to_s + '/config.ru'
threads 4, 8

60
server.rb Normal file
View File

@ -0,0 +1,60 @@
# frozen_string_literal: true
require 'logger'
require 'sinatra/base'
require 'rack/protection'
# Base Sinatra app
class Webdap < Sinatra::Base
@@my_app = {}
def self.new(*)
self < Webdap ? super : Rack::URLMap.new(@@my_app)
end
def
self.map(url) @@my_app[url] = self
end
configure do
enable :sessions
# Enable rack protection middleware
use Rack::Protection
# Set up static file serving
enable :static
set :public_folder, File.join(settings.root, '/public')
# Set up our view engine
set :views, File.join(settings.root, '/views')
end
# Initialize logging
logger = Logger.new($stdout)
logger.level = Logger::INFO
# Load helper functions
require_relative 'app/helpers'
helpers Helpers
## Map controllers
# Top-level routes controller
class IndexController < Webdap
map '/'
end
# Authentication routes controller
class AuthController < Webdap
map '/auth'
end
end
# Load controllers
Dir.glob('./app/controllers/*.rb').sort().each { |f| require f }