adsf 1.4.8 → 1.5.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: d47c094094044d3f804bfb77bfd56b43fb6705bbccafb7e33085a4d3bd8fdcf1
4
- data.tar.gz: 5b5a8708d1cf40b11975c668c2caef9635148eb3b3622479e3564ecc4a757871
3
+ metadata.gz: ed9bf6e0feaf4c359961448ad08440e789abdd038f0ec6df25ca94ce95692968
4
+ data.tar.gz: 6e32f017cf662136d6473ec9dafbefc83499cacaf0ead96b16a6dd4d12af3374
5
5
  SHA512:
6
- metadata.gz: b95fd1886ed101dc6b1a7ebce8fd8b5251849b261553a0f196462e9e2d372de673144c45683c180bbe279c00cc711ebab9480d8da038ca6c618b1fd5d96f2b11
7
- data.tar.gz: 0422a6745355809dd5b37018447175905fd2c8fca5495d0c5c92a284a09033d46ed580cc585830823435ef2af65faa0c665934186add0bec42a00d39a5a03923
6
+ metadata.gz: 1126dbb61b6a3e326faa194afc2d2a314ffbacdcccb9ff77f01e1e8fcc28aa3af5f381476fbed1235fcc40bc2c53e8145d96be2f1cb837ae52cdc7dfeb493fb8
7
+ data.tar.gz: 2383433bbb5d8b86851ae3d4d2143a18a88e3e5ef38bd6afdbf30462e9c8210f5cfa972281b127bf3e257e05d44f3d6ea7a75f5a40daf72aa3706474daddb395
data/NEWS.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Release notes for adsf
2
2
 
3
+ ## 1.5.0 (2024-01-08)
4
+
5
+ Features:
6
+
7
+ * Add `-x`/`--auto-extensions` option (#33) [Paul Cantrell]
8
+
9
+ Enhancements:
10
+
11
+ * Add `-V`/`--version` CLI option (#35)
12
+
3
13
  ## 1.4.8 (2023-10-14)
4
14
 
5
15
  Fixes:
data/README.md CHANGED
@@ -29,9 +29,32 @@ To use `adsf --live-reload`, please install the separate `adsf-live` gem. (The l
29
29
 
30
30
  ## Using adsf programmatically
31
31
 
32
+ ### Server
33
+
34
+ `Adsf::Server` runs a web server programmatically. For example:
35
+
36
+ ```ruby
37
+ server = Adsf::Server.new(root: 'public')
38
+
39
+ %w[INT TERM].each do |s|
40
+ Signal.trap(s) { server.stop }
41
+ end
42
+
43
+ server.run
44
+ ```
45
+
46
+ It takes the following options:
47
+
48
+ - `root` (required): the path to the web root
49
+ - `index_filenames` (optional; defaults to `['index.html']`): (see below)
50
+ - `auto_extensions` (optional; defaults to `[]`; can be a string or an array of strings): If present, the server will automatically append the given extensions when searching for files. For example, `auto_extensions: ".html"` would cause a request for `/foo` to serve `/foo.html` if there is no file or directory named `/foo`.
51
+ - `host` (optional; defaults to `'127.0.0.1'`): the address of the network interface to listen on
52
+ - `port` (optional; defaults to `3000`): the port to listen on
53
+ - `handler` (optional): the Rack handler to use
54
+
32
55
  ### IndexFileFinder
33
56
 
34
- The `Adsf::Rack::IndexFileFinder` middleware makes Rack load an index file (e.g. `index.html`) when requesting a directory. For example, the following runs a web server with the 'public' directory as its web root:
57
+ If you are assembling your own Rack configuration, you can use adsf’s `Adsf::Rack::IndexFileFinder` middleware to make Rack load an index file (e.g. `index.html`) when requesting a directory. For example, the following runs a web server with the 'public' directory as its web root:
35
58
 
36
59
  ```ruby
37
60
  use Adsf::Rack::IndexFileFinder, root: 'public'
@@ -61,31 +84,10 @@ It takes the following options:
61
84
 
62
85
  - When requesting a directory without specifying the trailing slash, `Adsf::Rack::IndexFileFinder` will redirect to the URL with a trailing slash, unlike `Rack::Static`. This mimics the behavior of typical HTTP servers. For example, when requesting `/foo`, when a `foo` directory exists and it contains `index.html`, `IndexFileFinder` will redirect to `/foo/`.
63
86
 
64
- ### Server
65
-
66
- `Adsf::Server` runs a web server programmatically. For example:
67
-
68
- ```ruby
69
- server = Adsf::Server.new(root: 'public')
70
-
71
- %w[INT TERM].each do |s|
72
- Signal.trap(s) { server.stop }
73
- end
74
-
75
- server.run
76
- ```
77
-
78
- It takes the following options:
79
-
80
- - `root` (required): the path to the web root
81
- - `index_filenames` (optional; defaults to `['index.html']`): (see above)
82
- - `host` (optional; defaults to `'127.0.0.1'`): the address of the network interface to listen on
83
- - `port` (optional; defaults to `3000`): the port ot listen on
84
- - `handler` (optional): the Rack handler to use
85
-
86
87
  ## Contributors
87
88
 
88
89
  - Ed Brannin
89
90
  - Larissa Reis
90
91
  - Mark Meves
91
92
  - Vipul Amler
93
+ - Paul Cantrell
data/bin/adsf CHANGED
@@ -25,10 +25,19 @@ OptionParser.new do |opts|
25
25
  exit
26
26
  end
27
27
 
28
+ opts.on('-V', '--version', 'Show version') do |_o|
29
+ puts "adsf version #{Adsf::VERSION} © 2009–… Denis Defreyne."
30
+ exit
31
+ end
32
+
28
33
  opts.on('-i', '--index-filenames [index-filenames]', 'Specify index filenames (comma-separated)') do |o|
29
34
  options[:index_filenames] = o.split(',')
30
35
  end
31
36
 
37
+ opts.on('-x', '--auto-extensions [extensions]', 'Specify suffixes to automatically apply to requests (comma-separated, including dot)') do |o|
38
+ options[:auto_extensions] = o.split(',')
39
+ end
40
+
32
41
  opts.on('-p', '--port [port]', Integer, 'Specify the port number to use') do |o|
33
42
  options[:port] = o
34
43
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Adsf::Rack
4
+ class AutoFileExtensions
5
+ def initialize(app, root:, extensions:)
6
+ @app = app
7
+ @root = root
8
+ # Search list starts with '' so that we first look for file as requested
9
+ @search_suffixes = [''] + Array(extensions).map { |ext| ".#{ext}" }
10
+ end
11
+
12
+ def call(env)
13
+ path_info = ::Rack::Utils.unescape(env['PATH_INFO'])
14
+ path = ::File.join(@root, path_info)
15
+
16
+ new_env = env
17
+ @search_suffixes.each do |suffix|
18
+ new_path = path + suffix
19
+ next unless ::File.exist?(new_path)
20
+
21
+ new_env = env.dup # only dup if needed
22
+ new_env['PATH_INFO'] += suffix
23
+ break
24
+ end
25
+
26
+ @app.call(new_env)
27
+ end
28
+ end
29
+ end
data/lib/adsf/rack.rb CHANGED
@@ -8,3 +8,4 @@ end
8
8
  require 'adsf/rack/index_file_finder'
9
9
  require 'adsf/rack/caching'
10
10
  require 'adsf/rack/cors'
11
+ require 'adsf/rack/auto_file_extensions'
data/lib/adsf/server.rb CHANGED
@@ -2,10 +2,19 @@
2
2
 
3
3
  module Adsf
4
4
  class Server
5
- def initialize(root:, live: false, index_filenames: ['index.html'], host: '127.0.0.1', port: 3000, handler: nil)
5
+ def initialize(
6
+ root:,
7
+ live: false,
8
+ host: '127.0.0.1',
9
+ port: 3000,
10
+ index_filenames: ['index.html'],
11
+ auto_extensions: [],
12
+ handler: nil
13
+ )
6
14
  @root = root
7
15
  @live = live
8
16
  @index_filenames = index_filenames
17
+ @auto_extensions = auto_extensions
9
18
  @host = host
10
19
  @port = port
11
20
  @handler = handler
@@ -15,7 +24,11 @@ module Adsf
15
24
 
16
25
  def run
17
26
  handler = build_handler
18
- app = build_app(root: @root, index_filenames: @index_filenames)
27
+ app = build_app(
28
+ root: @root,
29
+ index_filenames: @index_filenames,
30
+ auto_extensions: @auto_extensions,
31
+ )
19
32
  start_watcher if @live
20
33
 
21
34
  url = "http://#{@host}:#{@port}/"
@@ -47,7 +60,7 @@ module Adsf
47
60
  server.stop
48
61
  end
49
62
 
50
- def build_app(root:, index_filenames:)
63
+ def build_app(root:, index_filenames:, auto_extensions:)
51
64
  is_live = @live
52
65
 
53
66
  ::Rack::Builder.new do
@@ -60,6 +73,9 @@ module Adsf
60
73
  use Adsf::Rack::IndexFileFinder,
61
74
  root: root,
62
75
  index_filenames: index_filenames
76
+ use Adsf::Rack::AutoFileExtensions,
77
+ root: root,
78
+ extensions: auto_extensions
63
79
 
64
80
  if is_live
65
81
  require 'adsf/live'
data/lib/adsf/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Adsf
4
- VERSION = '1.4.8'
4
+ VERSION = '1.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adsf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.8
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Defreyne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-14 00:00:00.000000000 Z
11
+ date: 2024-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -56,6 +56,7 @@ files:
56
56
  - bin/adsf
57
57
  - lib/adsf.rb
58
58
  - lib/adsf/rack.rb
59
+ - lib/adsf/rack/auto_file_extensions.rb
59
60
  - lib/adsf/rack/caching.rb
60
61
  - lib/adsf/rack/cors.rb
61
62
  - lib/adsf/rack/index_file_finder.rb
@@ -81,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubygems_version: 3.4.20
85
+ rubygems_version: 3.5.4
85
86
  signing_key:
86
87
  specification_version: 4
87
88
  summary: a tiny static file server