servel 0.32.0 → 0.33.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 283ef8ff838b1a149aee4379a02233d006d5dfec7887de4791e20c2e61cbe86e
4
- data.tar.gz: b6fa99503294d5bba7b338c86fd0eb9bb46b06944de3fe2b10b225a6120b6794
3
+ metadata.gz: 36dac91c1fba1bbaa2ab70a7dc39404996216867968c93438f503ccc8dd15631
4
+ data.tar.gz: fedf2fb5c899e69de6715c40b64c4d7c652f6f01c6bec40c99cea1780ce6c16a
5
5
  SHA512:
6
- metadata.gz: d44b0fef9dac7e95483e13856f4b030813019c26ed7676a8154cdad88eef8b9db9b5075c4a463dc374353c0e73a09b9c408aa069b79f80da58c221451a227bc3
7
- data.tar.gz: d7a4126762ab129714b328aaf1dd492a17d1354870f4f943f799e2816f490de1ef85af253f8f4fcb96bd1d879fff5a134a7043f48112f2fe00bce7eac4af237a
6
+ metadata.gz: 6b52f97da999645b081ad452866fb032e841f073dbb9e3be5c28b4501062cca015d131872c237fc14cf5920421a0671ef8114d984c7bc320c4183f407da66cb3
7
+ data.tar.gz: 3d621057dd5499b01a69251a08e3d18c3dab29435fde99313f43484d0ae53ba443ca9aec6c16b71369a8e0a25dd5319bf029104cf7c385bf32913cc4d928cc92
data/lib/servel.rb CHANGED
@@ -9,18 +9,51 @@ require 'tty-config'
9
9
  require 'thread'
10
10
  require 'pathname'
11
11
  require 'json'
12
+ require 'digest'
12
13
 
13
14
  module Servel
14
- def self.build_app(path_map)
15
+ def self.build_app(listings:, username: nil, password: nil)
16
+ app = build_listings_app(build_path_map(listings))
17
+
18
+ if username && username != ""
19
+ build_authentication_app(app: app, username: username, password: password)
20
+ else
21
+ app
22
+ end
23
+ end
24
+
25
+ def self.build_authentication_app(app:, username:, password:)
26
+ hashed_username = Digest::SHA512.digest(username)
27
+ hashed_password = Digest::SHA512.digest(password)
28
+
29
+ Rack::Auth::Basic.new(app) do |submitted_username, submitted_password|
30
+ hashed_submitted_username = Digest::SHA512.digest(submitted_username)
31
+ hashed_submitted_password = Digest::SHA512.digest(submitted_password)
32
+
33
+ Rack::Utils.secure_compare(
34
+ "#{hashed_username}#{hashed_password}",
35
+ "#{hashed_submitted_username}#{hashed_submitted_password}"
36
+ )
37
+ end
38
+ end
39
+
40
+ def self.build_path_map(listings)
41
+ listings.map do |listing|
42
+ listing = { listing => nil } if listing.is_a?(String)
43
+
44
+ root, url_root = listing.keys.first, listing.values.first || "/"
45
+ [Pathname.new(root).realpath, url_root]
46
+ end.to_h
47
+ end
48
+
49
+ def self.build_listings_app(path_map)
15
50
  url_map = path_map.map { |root, url_root| [url_root, Servel::App.new(root)] }.to_h
16
51
  url_map["/"] = Servel::HomeApp.new(path_map.values) unless url_map.keys.include?("/")
17
52
 
18
53
  Rack::URLMap.new(url_map)
19
54
  end
20
55
 
21
- def self.config
22
- @config ||= Servel::ConfigParser.new.config
23
- end
56
+
24
57
  end
25
58
 
26
59
  require "servel/version"
data/lib/servel/cli.rb CHANGED
@@ -5,20 +5,12 @@ class Servel::CLI
5
5
  :binds
6
6
  ]
7
7
 
8
- def start
9
- Rack::Handler::Puma.run(Servel.build_app(path_map), **puma_options)
8
+ def initialize
9
+ @config = Servel::ConfigParser.new.config
10
10
  end
11
11
 
12
- def path_map
13
- Servel.config.fetch(:listings).map do |listing|
14
- listing = { listing => nil } if listing.is_a?(String)
15
-
16
- root, url_root = listing.keys.first, listing.values.first || "/"
17
- [Pathname.new(root).realpath, url_root]
18
- end.to_h
19
- end
20
-
21
- def puma_options
22
- Servel.config.to_h.transform_keys(&:to_sym).slice(*ALLOWED_PUMA_OPTIONS)
12
+ def start
13
+ app = Servel.build_app(**@config.slice(:listings, :username, :password))
14
+ Rack::Handler::Puma.run(app, **@config.slice(*ALLOWED_PUMA_OPTIONS))
23
15
  end
24
16
  end
@@ -2,17 +2,19 @@ class Servel::ConfigParser
2
2
  attr_reader :config
3
3
 
4
4
  def initialize
5
- @config = TTY::Config.new
6
- @config.filename = "servel"
7
- @config.append_path(Dir.pwd)
8
- @config.append_path((Pathname.new(Dir.home) + ".servel").to_s)
9
- @config.append_path(Dir.home)
10
- @config.env_prefix = "servel"
11
- @config.autoload_env
12
- @config.read if @config.exist?
13
-
14
- @config.append(*parse_argv, to: :listings)
5
+ @tty_config = TTY::Config.new
6
+ @tty_config.filename = "servel"
7
+ @tty_config.append_path(Dir.pwd)
8
+ @tty_config.append_path((Pathname.new(Dir.home) + ".servel").to_s)
9
+ @tty_config.append_path(Dir.home)
10
+ @tty_config.env_prefix = "servel"
11
+ @tty_config.autoload_env
12
+ @tty_config.read if @tty_config.exist?
13
+
14
+ @tty_config.append(*parse_argv, to: :listings)
15
15
  parse_binds
16
+
17
+ @config = @tty_config.to_h.transform_keys(&:to_sym)
16
18
  end
17
19
 
18
20
  def parse_argv
@@ -23,21 +25,21 @@ class Servel::ConfigParser
23
25
  end
24
26
 
25
27
  def parse_binds
26
- return parse_ssl_bind if @config.fetch(:cert) && @config.fetch(:key)
28
+ return parse_ssl_bind if @tty_config.fetch(:cert) && @tty_config.fetch(:key)
27
29
 
28
- host = @config.fetch(:host)
29
- port = @config.fetch(:port)
30
- @config.set(:Host, value: host) if host
31
- @config.set(:Port, value: port) if port
30
+ host = @tty_config.fetch(:host)
31
+ port = @tty_config.fetch(:port)
32
+ @tty_config.set(:Host, value: host) if host
33
+ @tty_config.set(:Port, value: port) if port
32
34
  end
33
35
 
34
36
  def parse_ssl_bind
35
- host = @config.fetch(:host, default: ::Puma::ConfigDefault::DefaultTCPHost)
36
- port = @config.fetch(:port, default: ::Puma::ConfigDefault::DefaultTCPPort)
37
- key = Pathname.new(@config.fetch(:key)).expand_path
38
- cert = Pathname.new(@config.fetch(:cert)).expand_path
37
+ host = @tty_config.fetch(:host, default: ::Puma::ConfigDefault::DefaultTCPHost)
38
+ port = @tty_config.fetch(:port, default: ::Puma::ConfigDefault::DefaultTCPPort)
39
+ key = Pathname.new(@tty_config.fetch(:key)).expand_path
40
+ cert = Pathname.new(@tty_config.fetch(:cert)).expand_path
39
41
 
40
42
  query = URI.encode_www_form(key: key, cert: cert)
41
- @config.append("ssl://#{host}:#{port}?#{query}", to: :binds)
43
+ @tty_config.append("ssl://#{host}:#{port}?#{query}", to: :binds)
42
44
  end
43
45
  end
@@ -1,3 +1,3 @@
1
1
  module Servel
2
- VERSION = "0.32.0"
2
+ VERSION = "0.33.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.32.0
4
+ version: 0.33.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brenton "B-Train" Fletcher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-16 00:00:00.000000000 Z
11
+ date: 2021-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler