local_pac 0.1.11 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +13 -0
  3. data/README.DEVELOPER.md +18 -0
  4. data/README.md +9 -1
  5. data/app/assets/fonts/open-sans/Apache License Version 2.txt +53 -0
  6. data/app/assets/fonts/open-sans/OpenSans-Bold-webfont.eot +0 -0
  7. data/app/assets/fonts/open-sans/OpenSans-Bold-webfont.svg +251 -0
  8. data/app/assets/fonts/open-sans/OpenSans-Bold-webfont.ttf +0 -0
  9. data/app/assets/fonts/open-sans/OpenSans-Bold-webfont.woff +0 -0
  10. data/app/assets/fonts/open-sans/OpenSans-BoldItalic-webfont.eot +0 -0
  11. data/app/assets/fonts/open-sans/OpenSans-BoldItalic-webfont.svg +251 -0
  12. data/app/assets/fonts/open-sans/OpenSans-BoldItalic-webfont.ttf +0 -0
  13. data/app/assets/fonts/open-sans/OpenSans-BoldItalic-webfont.woff +0 -0
  14. data/app/assets/fonts/open-sans/OpenSans-ExtraBold-webfont.eot +0 -0
  15. data/app/assets/fonts/open-sans/OpenSans-ExtraBold-webfont.svg +251 -0
  16. data/app/assets/fonts/open-sans/OpenSans-ExtraBold-webfont.ttf +0 -0
  17. data/app/assets/fonts/open-sans/OpenSans-ExtraBold-webfont.woff +0 -0
  18. data/app/assets/fonts/open-sans/OpenSans-ExtraBoldItalic-webfont.eot +0 -0
  19. data/app/assets/fonts/open-sans/OpenSans-ExtraBoldItalic-webfont.svg +251 -0
  20. data/app/assets/fonts/open-sans/OpenSans-ExtraBoldItalic-webfont.ttf +0 -0
  21. data/app/assets/fonts/open-sans/OpenSans-ExtraBoldItalic-webfont.woff +0 -0
  22. data/app/assets/fonts/open-sans/OpenSans-Italic-webfont.eot +0 -0
  23. data/app/assets/fonts/open-sans/OpenSans-Italic-webfont.svg +251 -0
  24. data/app/assets/fonts/open-sans/OpenSans-Italic-webfont.ttf +0 -0
  25. data/app/assets/fonts/open-sans/OpenSans-Italic-webfont.woff +0 -0
  26. data/app/assets/fonts/open-sans/OpenSans-Light-webfont.eot +0 -0
  27. data/app/assets/fonts/open-sans/OpenSans-Light-webfont.svg +252 -0
  28. data/app/assets/fonts/open-sans/OpenSans-Light-webfont.ttf +0 -0
  29. data/app/assets/fonts/open-sans/OpenSans-Light-webfont.woff +0 -0
  30. data/app/assets/fonts/open-sans/OpenSans-LightItalic-webfont.eot +0 -0
  31. data/app/assets/fonts/open-sans/OpenSans-LightItalic-webfont.svg +252 -0
  32. data/app/assets/fonts/open-sans/OpenSans-LightItalic-webfont.ttf +0 -0
  33. data/app/assets/fonts/open-sans/OpenSans-LightItalic-webfont.woff +0 -0
  34. data/app/assets/fonts/open-sans/OpenSans-Regular-webfont.eot +0 -0
  35. data/app/assets/fonts/open-sans/OpenSans-Regular-webfont.svg +252 -0
  36. data/app/assets/fonts/open-sans/OpenSans-Regular-webfont.ttf +0 -0
  37. data/app/assets/fonts/open-sans/OpenSans-Regular-webfont.woff +0 -0
  38. data/app/assets/fonts/open-sans/OpenSans-Semibold-webfont.eot +0 -0
  39. data/app/assets/fonts/open-sans/OpenSans-Semibold-webfont.svg +251 -0
  40. data/app/assets/fonts/open-sans/OpenSans-Semibold-webfont.ttf +0 -0
  41. data/app/assets/fonts/open-sans/OpenSans-Semibold-webfont.woff +0 -0
  42. data/app/assets/fonts/open-sans/OpenSans-SemiboldItalic-webfont.eot +0 -0
  43. data/app/assets/fonts/open-sans/OpenSans-SemiboldItalic-webfont.svg +251 -0
  44. data/app/assets/fonts/open-sans/OpenSans-SemiboldItalic-webfont.ttf +0 -0
  45. data/app/assets/fonts/open-sans/OpenSans-SemiboldItalic-webfont.woff +0 -0
  46. data/app/assets/javascripts/application.js +17 -0
  47. data/app/assets/javascripts/jquery-min.js +4 -0
  48. data/app/assets/stylesheets/application.css.scss +139 -0
  49. data/app/assets/stylesheets/open-sans.css.scss +109 -0
  50. data/app/controllers/application_controller.rb +90 -0
  51. data/app/controllers/assets_controller.rb +64 -0
  52. data/app/controllers/file_serve_controller.rb +22 -0
  53. data/app/controllers/lookup_controller.rb +34 -0
  54. data/app/locales/de.yml +2 -0
  55. data/app/locales/en.yml +31 -0
  56. data/{features/show_known_pacfiles.feature → app/public/favicon.ico} +0 -0
  57. data/app/views/application.haml +10 -0
  58. data/app/views/error.haml +6 -0
  59. data/app/views/form.haml +11 -0
  60. data/app/views/lookup.haml +3 -0
  61. data/app/views/lookup_result.haml +19 -0
  62. data/bin/local_pac +47 -27
  63. data/config.ru +24 -3
  64. data/features/fetch_proxy_pac.feature +1 -1
  65. data/features/initializer.feature +0 -1
  66. data/features/{show_config.feature → show_status.feature} +2 -2
  67. data/features/step_definitions.rb +14 -0
  68. data/features/support/env.rb +1 -0
  69. data/files/config.yaml +1 -0
  70. data/files/example-config.erb +16 -0
  71. data/lib/local_pac/{logger.rb → access_logger.rb} +1 -1
  72. data/lib/local_pac/actions/create_directory.rb +2 -2
  73. data/lib/local_pac/actions/create_file.rb +2 -2
  74. data/lib/local_pac/actions/create_output.rb +36 -0
  75. data/lib/local_pac/config.rb +15 -5
  76. data/lib/local_pac/exceptions.rb +12 -0
  77. data/lib/local_pac/initializer.rb +10 -1
  78. data/lib/local_pac/main.rb +39 -2
  79. data/lib/local_pac/null_access_logger.rb +6 -0
  80. data/lib/local_pac/proxy_pac/html_data.rb +17 -0
  81. data/lib/local_pac/proxy_pac/html_div_style.rb +48 -0
  82. data/lib/local_pac/proxy_pac/html_table_style.rb +57 -0
  83. data/lib/local_pac/proxy_pac/pac_engine.rb +28 -0
  84. data/lib/local_pac/proxy_pac/pac_parser.rb +33 -0
  85. data/lib/local_pac/proxy_pac/pac_result.rb +49 -0
  86. data/lib/local_pac/proxy_pac/pac_result_html_stylist.rb +34 -0
  87. data/lib/local_pac/proxy_pac/result.rb +40 -0
  88. data/lib/local_pac/proxy_pac/result_parser.rb +49 -0
  89. data/lib/local_pac/router.rb +7 -0
  90. data/lib/local_pac/server.rb +28 -0
  91. data/lib/local_pac/spec_helper_file_server.rb +1 -1
  92. data/lib/local_pac/ui_logger.rb +0 -1
  93. data/lib/local_pac/version.rb +1 -1
  94. data/lib/local_pac.rb +15 -4
  95. data/local_pac.gemspec +10 -0
  96. data/script/console +14 -0
  97. data/share/archlinux/PKGBUILD +4 -4
  98. data/spec/actions/create_directory_spec.rb +1 -1
  99. data/spec/actions/create_file_spec.rb +0 -2
  100. data/spec/actions/create_output_spec.rb +46 -0
  101. data/spec/config_spec.rb +11 -0
  102. data/spec/features/fetch_proxy_pac_spec.rb +49 -14
  103. data/spec/features/lookup_proxy_spec.rb +102 -0
  104. data/spec/initializer_spec.rb +7 -2
  105. data/spec/proxy_pac/html_div_style_spec.rb +45 -0
  106. data/spec/proxy_pac/html_table_style_spec.rb +54 -0
  107. data/spec/proxy_pac/pac_engine_spec.rb +33 -0
  108. data/spec/proxy_pac/pac_parser_spec.rb +32 -0
  109. data/spec/proxy_pac/pac_result_html_stylist_spec.rb +36 -0
  110. data/spec/proxy_pac/pac_result_spec.rb +54 -0
  111. data/spec/proxy_pac_result_parser_spec.rb +15 -0
  112. data/spec/server_spec.rb +18 -0
  113. data/spec/spec_helper.rb +2 -0
  114. data/spec/spec_helper_features.rb +3 -0
  115. data/spec/support/capybara.rb +6 -0
  116. metadata +264 -8
  117. data/lib/local_pac/file_server.rb +0 -37
@@ -0,0 +1,34 @@
1
+ module LocalPac
2
+ module App
3
+ class LookupController < ApplicationController
4
+
5
+ get '/' do
6
+ redirect to('/proxy.pac')
7
+ end
8
+
9
+ get '/:name' do
10
+ haml :lookup, layout: :application
11
+ end
12
+
13
+ post '/:name' do
14
+ @file = local_storage.find(params[:name].to_s)
15
+ @uri = Addressable::URI.heuristic_parse(params[:url].to_s)
16
+
17
+ if @file.nil?
18
+ fail Sinatra::NotFound, params[:name]
19
+ else
20
+ parser = LocalPac::ProxyPac::PacParser.new(file: @file)
21
+
22
+ begin
23
+ @result = parser.find(@uri)
24
+ rescue Exceptions::PacFileInvalid
25
+ fail I18n.t('errors.invalid_proxy_pac', name: params[:name] )
26
+ end
27
+
28
+ end
29
+
30
+ haml :lookup_result, layout: :application
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,2 @@
1
+ ---
2
+ :de:
@@ -0,0 +1,31 @@
1
+ ---
2
+ en:
3
+ errors:
4
+ invalid_proxy_pac: The requested proxy.pac-file "%{name}" is invalid.
5
+ not_found: Sorry, but I cannot find proxy.pac-file "%{name}".
6
+ invalid_url:
7
+ summary: 'Invalid URL...'
8
+ details: 'Sorry, but your request is invalid. The URL "%{url}" is not a correct one. Allowed are URLs like "http://www.example.org" or "www.example.org".'
9
+ default: Sorry, but I cannot full your request. %{message}. Please ask your administrator for support.
10
+ models:
11
+ data:
12
+ request_type: Request Type
13
+ proxy: Proxy
14
+ proxy_port: Proxy Port
15
+ views:
16
+ application:
17
+ buttons:
18
+ search: Search
19
+ reset: Clear
20
+ lookup:
21
+ headline: Lookup
22
+ lookup_result:
23
+ subtitle: You asked me to look up "%{url}" for you.
24
+ headline: Lookup
25
+ buttons:
26
+ styled: Styled Output
27
+ verbatim: Verbatim Output
28
+ form:
29
+ search_field:
30
+ label: URL
31
+ placeholder: Please enter fully quallified url here, e.g. "http://www.example.org"...
@@ -0,0 +1,10 @@
1
+ !!!
2
+ %html
3
+ %head
4
+ %meta{:charset => "utf-8"}
5
+ %link{ rel: 'stylesheet', href: stylesheet_path('application.css') }
6
+ %script{ type: 'text/javascript', src: javascript_path('application.js') }
7
+ %title Lookup url in proxy.pac
8
+ %body
9
+ .lp_container
10
+ = yield
@@ -0,0 +1,6 @@
1
+ .lp_warning_container
2
+ .lp_warning_summary
3
+ %i.fa.fa-exclamation-circle
4
+ = @error_summary
5
+ .lp_warning_details
6
+ = @error_details
@@ -0,0 +1,11 @@
1
+ %form#search.lp_form{ role: 'form', method: 'post'}
2
+ .lp_form_controls
3
+ .lp_search_group
4
+ %label.sr-only{for: 'url'}
5
+ = t('views.lookup_result.form.search_field.label')
6
+ %input#url.lp_field_search{name: 'url', placeholder: t('views.lookup_result.form.search_field.placeholder'), autofocus: true, value: @uri }
7
+ .lp_buttons
8
+ %button.lp_button_search{type: 'submit', value: t('views.application.buttons.search')}
9
+ = t('views.application.buttons.search')
10
+ %button#reset.lp_button_reset{type: 'submit', value: t('views.application.buttons.reset')}
11
+ = t('views.application.buttons.reset')
@@ -0,0 +1,3 @@
1
+ %h1.text-center= t('views.lookup.headline')
2
+
3
+ = haml :form, layout: false
@@ -0,0 +1,19 @@
1
+ %h1.text-center= t('views.lookup_result.headline')
2
+
3
+ = haml :form, layout: false
4
+
5
+ %ul.lp_navigation#navigation
6
+ %li.active
7
+ %a{ href: "#styled", :"data-toggle" => "tab" }
8
+ = t('views.lookup_result.buttons.styled')
9
+ %li
10
+ %a{ href: "#verbatim", :"data-toggle" => "tab" }
11
+ = t('views.lookup_result.buttons.verbatim')
12
+
13
+ .tab-content
14
+ #styled.tab-pane.active
15
+ .lp_requested_uri= t('views.lookup_result.subtitle', url: h(@uri))
16
+ = @result.styled_content
17
+ #verbatim.tab-pane
18
+ .lp_verbatim
19
+ = @result.verbatim_content
data/bin/local_pac CHANGED
@@ -9,44 +9,59 @@ require 'local_pac'
9
9
  class Default < Thor
10
10
  class_option :config_file, type: :string, desc: 'Config file'
11
11
  class_option :log_level, type: :string, desc: 'Log level for ui logging'
12
+ class_option :debug, type: :boolean, default: false, desc: 'Run application in debug mode'
13
+
14
+ no_commands {
15
+ def set_log_level(requested_level)
16
+ case requested_level.to_s.to_sym
17
+ when :info
18
+ LocalPac.ui_logger.level = ::Logger::INFO
19
+ when :debug
20
+ LocalPac.ui_logger.level = ::Logger::DEBUG
21
+ else
22
+ LocalPac.ui_logger.level = ::Logger::WARN
23
+ end
24
+ end
25
+
26
+ def set_debug(request)
27
+ if request
28
+ LocalPac.ui_logger.info "Activating debug mode."
29
+
30
+ require 'pry'
31
+ require 'debugger'
32
+ end
33
+ rescue LoadError
34
+ LocalPac.ui_logger.error "You tried to enable debug-mode, but either 'pry'- or 'debugger'-gem are not installed. Please fix that before using the debug-switch again."
35
+ end
36
+ }
12
37
 
13
38
  desc 'serve', 'Serve pacfiles'
14
39
  option :access_log, type: :string, desc: 'File to write access log to'
15
- option :port, type: :numeric, default: 8000, desc: 'The port the server listens on'
40
+ option :listen, type: :string, default: 'tcp://localhost:8000', desc: 'Listen for requests'
41
+ option :rack_environment, type: :string, default: 'production', desc: 'Rack environment for application'
16
42
  def serve
17
- LocalPac.config(LocalPac::Config.new(options[:config_file])) if options[:config_file]
18
- access_log = options[:access_log] || ::File.join(LocalPac.config.log_sink, 'access.log')
19
-
20
- case options[:log_level]
21
- when 'info'
22
- LocalPac.ui_logger.level = Logger::INFO
23
- when 'debug'
24
- LocalPac.ui_logger.level = Logger::DEBUG
25
- else
26
- LocalPac.ui_logger.level = Logger::WARN
27
- end
43
+ LocalPac.config = LocalPac::Config.new(options[:config_file]) if options[:config_file]
44
+ LocalPac.config.access_log = options[:access_log] if options[:access_log]
45
+
46
+ LocalPac.config.lock
47
+
48
+ set_log_level(options[:log_level])
49
+ set_debug(options[:debug])
28
50
 
29
51
  LocalPac.ui_logger.debug('Options: ' + options.to_s)
30
52
  LocalPac.ui_logger.debug("Config:\n" + LocalPac.config.to_s)
31
53
 
32
- LocalPac::FileServer.use Rack::CommonLogger, LocalPac::Logger.new(access_log)
33
- LocalPac::FileServer.set :port, options[:port]
34
- LocalPac::FileServer.run!
54
+ LocalPac::Server.new(options[:listen], options[:rack_environment]).start
35
55
  end
36
56
 
37
57
  desc 'init', 'Create files/directories to use local_pac in dir or $PWD'
38
58
  option :force, type: :boolean, default: false, desc: 'Overwrite existing files?'
39
59
  def init
40
- LocalPac.config(LocalPac::Config.new(options[:config_file])) if options[:config_file]
41
-
42
- case options[:log_level]
43
- when 'info'
44
- LocalPac.ui_logger.level = Logger::INFO
45
- when 'debug'
46
- LocalPac.ui_logger.level = Logger::DEBUG
47
- else
48
- LocalPac.ui_logger.level = Logger::WARN
49
- end
60
+ LocalPac.config = LocalPac::Config.new(options[:config_file]) if options[:config_file]
61
+ LocalPac.config.lock
62
+
63
+ set_log_level(options[:log_level])
64
+ set_debug(options[:debug])
50
65
 
51
66
  LocalPac.ui_logger.debug('Options: ' + options.to_s)
52
67
  LocalPac.ui_logger.debug("Config:\n" + LocalPac.config.to_s)
@@ -54,9 +69,14 @@ class Default < Thor
54
69
  LocalPac::Initializer.new(force: options[:force]).run
55
70
  end
56
71
 
57
- desc 'config', 'Show configuration'
72
+ desc 'status', 'Show status of local_pac: configuration, known proxy pacs, server running etc.'
58
73
  def config
59
- LocalPac.config(LocalPac::Config.new(options[:config_file])) if options[:config_file]
74
+ LocalPac.config = LocalPac::Config.new(options[:config_file]) if options[:config_file]
75
+ LocalPac.config.lock
76
+
77
+ set_log_level(options[:log_level])
78
+ set_debug(options[:debug])
79
+
60
80
  puts LocalPac.config
61
81
  end
62
82
  end
data/config.ru CHANGED
@@ -1,6 +1,27 @@
1
1
  $LOAD_PATH << File.expand_path('../lib/', __FILE__)
2
+
3
+ #require 'rack/contrib'
4
+
2
5
  require 'local_pac'
3
6
 
4
- logger = LocalPac::Logger.new ENV['LOGFILE'] || File.expand_path(File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'access.log'))
5
- use Rack::CommonLogger, logger
6
- run LocalPac::FileServer.new
7
+ require File.expand_path('../app/controllers/application_controller.rb', __FILE__)
8
+ Dir.glob(File.expand_path('../app/controllers/*.rb', __FILE__)).each { |f| require f }
9
+ #
10
+ #logger = LocalPac::AccessLogger.new ENV['ACCESS_LOG'] || File.expand_path(File.join(LocalPac.config.access_log))
11
+ #use Rack::CommonLogger, logger
12
+
13
+ map '/' do
14
+ run LocalPac::App::FileServeController
15
+ end
16
+
17
+ map '/v1/pac/' do
18
+ run LocalPac::App::FileServeController
19
+ end
20
+
21
+ map '/v1/lookup/' do
22
+ run LocalPac::App::LookupController
23
+ end
24
+
25
+ map LocalPac::App::AssetsController.assets_prefix do
26
+ run LocalPac::App::AssetsController.assets
27
+ end
@@ -5,7 +5,7 @@ Feature: Fetch proxy pac
5
5
  In order to reach different proxy servers
6
6
 
7
7
  Scenario: Default
8
- Given an empty environment
8
+ Given no proxy urls in environment
9
9
  When I sucessfully fetch the proxy pac
10
10
  Then I got
11
11
  """
@@ -3,7 +3,6 @@ Feature: Initialize environment
3
3
  I want to have utility to init environment for local_pac
4
4
  In order to make less mistakes
5
5
 
6
- @wip
7
6
  Scenario: Not initialized
8
7
  Given a file named "config.yaml" with:
9
8
  """
@@ -1,4 +1,4 @@
1
- Feature: Show config
1
+ Feature: Show status
2
2
  As a administrator
3
3
  I want to have utility to show config of local pac
4
4
  In order to make less mistakes
@@ -12,7 +12,7 @@ Feature: Show config
12
12
  log_sink: 'log'
13
13
  executable: 'bin/local_pac'
14
14
  """
15
- When I successfully run `local_pac config`
15
+ When I successfully run `local_pac status`
16
16
  Then the stdout should contain:
17
17
  """
18
18
  pid_file: 'run/pid'
@@ -0,0 +1,14 @@
1
+ Given(/^no proxy urls in environment$/) do
2
+ ENV.delete('http_proxy')
3
+ ENV.delete('https_proxy')
4
+ ENV.delete('HTTP_PROXY')
5
+ ENV.delete('HTTPS_PROXY')
6
+ end
7
+
8
+ When(/^I sucessfully fetch the proxy pac$/) do
9
+ @result = Excon.get('http://localhost:8000/v1/pac/proxy.pac').body
10
+ end
11
+
12
+ Then(/^I got$/) do |string|
13
+ expect(@result).to eq(string)
14
+ end
@@ -1 +1,2 @@
1
1
  require 'aruba/cucumber'
2
+ require 'excon'
data/files/config.yaml CHANGED
@@ -2,3 +2,4 @@
2
2
  :log_sink: ~/.local/share/local_pac/log
3
3
  :local_storage: ~/.local/share/local_pac/data
4
4
  :pid_file: ~/.local/share/local_pac/run/pid
5
+ :access_log: ~/.local/share/local_pac/log/access.log
@@ -0,0 +1,16 @@
1
+
2
+ ---
3
+ # You might want to copy the following output to one of the searched config
4
+ # file paths - in the given order.
5
+ <% Array(lookup('allowed_config_file_paths')).each do |p| -%>
6
+ # <%= p %>
7
+ <% end -%>
8
+ :gem_path:
9
+ <% Array(lookup('gem_path')).collect { |p| File.expand_path(p) }.each do |p| -%>
10
+ - <%= p %>
11
+ <% end -%>
12
+ :log_sink: <%= File.expand_path(lookup('log_sink')) %>
13
+ :local_storage: <%= File.expand_path(lookup('local_storage')) %>
14
+ :executable: <%= File.expand_path(lookup('executable')) %>
15
+ :pid_file: <%= File.expand_path(lookup('pid_file')) %>
16
+
@@ -1,5 +1,5 @@
1
1
  module LocalPac
2
- class Logger
2
+ class AccessLogger
3
3
  def initialize(path)
4
4
  @logger = ::Logger.new(::File.expand_path(path))
5
5
  end
@@ -16,10 +16,10 @@ module LocalPac
16
16
 
17
17
  def run
18
18
  if need_to_run? || options[:force] == true
19
- LocalPac.ui_logger.warn "Creating repository \"#{path}\"."
19
+ LocalPac.ui_logger.warn "Creating directory \"#{path}\"."
20
20
  fs_engine.mkdir_p(path)
21
21
  else
22
- LocalPac.ui_logger.warn "Repository \"#{path}\" already exists. Do not create it again!."
22
+ LocalPac.ui_logger.warn "Directory \"#{path}\" already exists. Do not create it again!."
23
23
  end
24
24
  end
25
25
 
@@ -21,7 +21,7 @@ module LocalPac
21
21
  if need_to_run? || options[:force] == true
22
22
  LocalPac.ui_logger.warn "Creating file \"#{destination}\"."
23
23
 
24
- file = template(name, destination, data)
24
+ file = template(name, ::File.new(destination, 'w'), data)
25
25
  FileUtils.chmod('+x', file) if options[:executable] == true
26
26
  else
27
27
  LocalPac.ui_logger.warn "File \"#{destination}\" already exists. Do not create it again!."
@@ -34,7 +34,7 @@ module LocalPac
34
34
  template = repository.find(local_name)
35
35
 
36
36
  generator = engine.new(local_data)
37
- generator.compile(template, ::File.new(local_destination, 'w'))
37
+ generator.compile(template, local_destination)
38
38
 
39
39
  local_destination
40
40
  rescue Errno::ENOENT
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ module Actions
4
+ class CreateOutput
5
+ private
6
+
7
+ attr_reader :name, :output, :engine, :repository, :data
8
+
9
+ public
10
+
11
+ def initialize(name, output, data, engine = ErbGenerator, repository = TemplateRepository.new)
12
+ @name = name
13
+ @output = output
14
+ @data = data
15
+ @engine = engine
16
+ @repository = repository
17
+ end
18
+
19
+ def run
20
+ LocalPac.ui_logger.info "Creating example configuration:"
21
+ template(name, output, data)
22
+ end
23
+
24
+ private
25
+
26
+ def template(local_name, local_destination, local_data)
27
+ template = repository.find(local_name)
28
+
29
+ generator = engine.new(local_data)
30
+ generator.compile(template, local_destination)
31
+ rescue Errno::ENOENT
32
+ fail Exceptions::ErbTemplateIsUnknown, "Unknown erb template \"#{template_path}\"."
33
+ end
34
+ end
35
+ end
36
+ end
@@ -16,6 +16,10 @@ module LocalPac
16
16
  config.fetch(option.to_sym, default_value)
17
17
  end
18
18
 
19
+ define_method "#{option}=".to_sym do |value|
20
+ config[option.to_sym] = value
21
+ end
22
+
19
23
  @options << option
20
24
  end
21
25
  end
@@ -30,18 +34,24 @@ module LocalPac
30
34
  if yaml.respond_to? :[]
31
35
  @config = yaml.symbolize_keys.freeze
32
36
  else
33
- @config = {}.freeze
37
+ @config = {}
34
38
  end
35
39
  end
36
40
  rescue => e
37
41
  fail Exceptions::ConfigFileNotReadable, "Sorry, but there was a problem reading the config file: #{e.message}."
38
42
  end
39
43
 
44
+ def lock
45
+ config.freeze
46
+ end
47
+
40
48
  option :log_sink, File.expand_path(File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'log'))
41
49
  option :local_storage, File.expand_path(File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'data'))
42
50
  option :executable, File.expand_path(File.expand_path('../../../bin/local_pac', __FILE__))
43
51
  option :pid_file, File.expand_path(File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'run', 'pid'))
44
52
  option :gem_path, Gem.path.collect {|p| File.expand_path(p) }
53
+ option :access_log, File.expand_path(File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'log', 'access.log'))
54
+ option :sass_cache, File.expand_path(File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'cache', 'sass'))
45
55
 
46
56
  def to_s
47
57
  result = []
@@ -55,9 +65,7 @@ module LocalPac
55
65
  result.join("\n")
56
66
  end
57
67
 
58
- private
59
-
60
- def candiate_files
68
+ def allowed_config_file_paths
61
69
  [
62
70
  File.expand_path(File.join(ENV['HOME'], '.config', 'local_pac', 'config.yaml')),
63
71
  File.expand_path(File.join(ENV['HOME'], '.local_pac', 'config.yaml')),
@@ -66,8 +74,10 @@ module LocalPac
66
74
  ]
67
75
  end
68
76
 
77
+ private
78
+
69
79
  def available_config_file
70
- candiate_files.find { |f| File.exists? f }
80
+ allowed_config_file_paths.find { |f| File.exists? f }
71
81
  end
72
82
  end
73
83
  end
@@ -8,5 +8,17 @@ module LocalPac
8
8
 
9
9
  # raised if Template does not exist
10
10
  class ConfigFileNotReadable < Exception; end
11
+
12
+ # raised if listen statement is invalid
13
+ class ServerListenStatementInvalid < Exception; end
14
+
15
+ # raised if entered url is invalid
16
+ class URLToSearchForInvalid < Exception; end
17
+
18
+ # raised if pac file is invalid
19
+ class PacFileInvalid < Exception; end
20
+
21
+ # raised if request is invalid
22
+ class GivenUrlInvalid < Exception; end
11
23
  end
12
24
  end
@@ -15,12 +15,21 @@ module LocalPac
15
15
  def run
16
16
  LocalPac.ui_logger.info "Creating pid directory: #{::File.dirname(config.pid_file)}"
17
17
  Actions::CreateDirectory.new(::File.dirname(config.pid_file), force: options[:force]).run
18
+
18
19
  LocalPac.ui_logger.info "Creating log sink: #{config.log_sink}"
19
20
  Actions::CreateDirectory.new(config.log_sink, force: options[:force]).run
21
+
22
+ LocalPac.ui_logger.info "Creating sass cache #{config.sass_cache}"
23
+ Actions::CreateDirectory.new(config.sass_cache, force: options[:force]).run
24
+
20
25
  LocalPac.ui_logger.info "Creating local storage: #{config.local_storage}"
21
26
  Actions::CreateRepository.new(config.local_storage, bare: true, force: options[:force]).run
27
+
22
28
  LocalPac.ui_logger.info "Creating pre-receive hook in local storage \"#{config.local_storage}\"."
23
- Actions::CreateFile.new(:'git-hook', File.join(config.local_storage, 'hooks', 'pre-receive'), Data.new(config), force: options[:force]).run
29
+ Actions::CreateFile.new(:'git-hook', File.join(config.local_storage, 'hooks', 'pre-receive'), Data.new(config), force: options[:force], executable: true).run
30
+
31
+ LocalPac.ui_logger.info "Creating example configuration."
32
+ Actions::CreateOutput.new(:'example-config', $stdout, Data.new(LocalPac::Config.new)).run
24
33
  end
25
34
  end
26
35
  end
@@ -2,22 +2,59 @@
2
2
  module LocalPac
3
3
  @config_semaphore = Mutex.new
4
4
  @logger_semaphore = Mutex.new
5
+ @routing_semaphore = Mutex.new
5
6
 
6
7
  class << self
7
8
  attr_reader :config_semaphore
8
9
  attr_reader :logger_semaphore
10
+ attr_reader :routing_semaphore
9
11
  attr_accessor :debug_mode
10
12
 
11
- def config(local_config = LocalPac::Config.new)
13
+ def config
12
14
  config_semaphore.synchronize do
13
- @config ||= local_config
15
+ @config ||= LocalPac::Config.new
14
16
  end
15
17
  end
16
18
 
19
+ def config=(config)
20
+ @config = config
21
+ end
22
+
17
23
  def ui_logger(local_logger = LocalPac::UiLogger.new)
18
24
  logger_semaphore.synchronize do
19
25
  @ui_logger ||= local_logger
20
26
  end
21
27
  end
28
+
29
+ def error_id
30
+ SecureRandom.hex
31
+ end
32
+
33
+ def routing
34
+ routing_semaphore.synchronize do
35
+ @routing ||= Rack::Builder.new do
36
+ require_relative File.expand_path('../../../app/controllers/application_controller.rb', __FILE__)
37
+ Dir.glob(File.expand_path('../../../app/controllers/*.rb', __FILE__)).each { |f| require_relative f }
38
+
39
+ map '/' do
40
+ run LocalPac::App::FileServeController
41
+ end
42
+
43
+ map '/v1/pac/' do
44
+ run LocalPac::App::FileServeController
45
+ end
46
+
47
+ map '/v1/lookup/' do
48
+ run LocalPac::App::LookupController
49
+ end
50
+
51
+ map LocalPac::App::ApplicationController.assets_prefix do
52
+ run LocalPac::App::ApplicationController.assets
53
+ end
54
+ end
55
+ end
56
+ end
22
57
  end
58
+
59
+
23
60
  end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ class NullAccessLogger
4
+ def write(*args, &block); end
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ module ProxyPac
4
+ class HTMLData
5
+ attr_reader :header_css_class, :header, :element_css_class, :element, :html_style, :html_data
6
+
7
+ def initialize(options = {})
8
+ @header_css_class = options.fetch(:header_css_class, nil)
9
+ @element_css_class = options.fetch(:element_css_class)
10
+ @element = options.fetch(:element)
11
+ @header = options.fetch(:header, nil)
12
+ rescue KeyError => e
13
+ raise ArgumentError, e.message
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ module ProxyPac
4
+ class HTMLDivStyle
5
+ private
6
+
7
+ attr_reader :data, :main_container_css_class, :element_container_css_class
8
+
9
+ public
10
+
11
+ def initialize(options = {})
12
+ @main_container_css_class = options.fetch(:main_container_css_class)
13
+ @element_container_css_class = options.fetch(:element_container_css_class)
14
+ @data = []
15
+ rescue KeyError => e
16
+ raise ArgumentError, e.message
17
+ end
18
+
19
+ def add_data(data)
20
+ @data << data
21
+ end
22
+
23
+ def to_s
24
+ data_row = data.reduce([]) do |memo, element|
25
+ memo << " <div class=\"#{element_container_css_class}\">"
26
+
27
+ if element.header
28
+ memo << " <div class=\"#{element.header_css_class}\">" if element.header_css_class
29
+ memo << " #{element.header}"
30
+ memo << ' </div>' if element.header_css_class
31
+ end
32
+
33
+ memo << " <div class=\"#{element.element_css_class}\">" if element.element_css_class
34
+ memo << " #{element.element}"
35
+ memo << ' </div>' if element.element_css_class
36
+
37
+ memo << ' </div>'
38
+ end
39
+
40
+ content = ["<div class=\"#{main_container_css_class}\">"]
41
+ content.concat data_row
42
+ content << '</div>'
43
+
44
+ content.join("\n")
45
+ end
46
+ end
47
+ end
48
+ end