simple-httpd 0.0.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.rubocop.yml +9 -0
- data/.tm_properties +1 -0
- data/Gemfile +21 -1
- data/Makefile +9 -0
- data/README.md +87 -2
- data/Rakefile +5 -0
- data/VERSION +1 -1
- data/bin/simple-httpd +13 -0
- data/examples/README.md +41 -0
- data/examples/ex1/ex1_helpers.rb +5 -0
- data/examples/ex1/root.rb +11 -0
- data/examples/ex2/README.txt +1 -0
- data/examples/ex2/ex2_helpers.rb +5 -0
- data/examples/ex2/helpers.rb +15 -0
- data/examples/ex2/info.rb +4 -0
- data/examples/ex2/root.rb +3 -0
- data/examples/ex3/example_service.rb +13 -0
- data/examples/services/example_service.rb +25 -0
- data/examples/services/explicit_example_service.rb +18 -0
- data/examples/v2/api.js +1 -0
- data/examples/v2/jobs.rb +13 -0
- data/examples/v2/root.rb +3 -0
- data/examples/v2/v2_helpers.rb +5 -0
- data/lib/simple-service.rb +3 -0
- data/lib/simple/httpd.rb +99 -25
- data/lib/simple/httpd/base_controller.rb +2 -2
- data/lib/simple/httpd/base_controller/error_handling.rb +45 -17
- data/lib/simple/httpd/base_controller/json.rb +15 -8
- data/lib/simple/httpd/cli.rb +99 -0
- data/lib/simple/httpd/helpers.rb +54 -0
- data/lib/simple/httpd/mount_spec.rb +106 -0
- data/lib/simple/httpd/rack.rb +17 -0
- data/lib/simple/httpd/rack/dynamic_mount.rb +66 -0
- data/lib/simple/httpd/rack/merger.rb +28 -0
- data/lib/simple/httpd/rack/static_mount.rb +50 -0
- data/lib/simple/httpd/server.rb +69 -0
- data/lib/simple/httpd/service.rb +70 -0
- data/lib/simple/httpd/version.rb +1 -1
- data/lib/simple/service.rb +69 -0
- data/lib/simple/service/action.rb +78 -0
- data/lib/simple/service/context.rb +46 -0
- data/scripts/release +2 -0
- data/scripts/release.rb +91 -0
- data/simple-httpd.gemspec +9 -19
- data/spec/simple/httpd/base_controller/httpd_cors_spec.rb +15 -0
- data/spec/simple/httpd/base_controller/httpd_debug_spec.rb +11 -0
- data/spec/simple/httpd/base_controller/httpd_x_processing_copy.rb +15 -0
- data/spec/simple/httpd/base_spec.rb +16 -0
- data/spec/simple/httpd/dynamic_mounting_spec.rb +33 -0
- data/spec/simple/httpd/helpers_spec.rb +15 -0
- data/spec/simple/httpd/rspec_httpd_spec.rb +17 -0
- data/spec/simple/httpd/services/service_explicit_spec.rb +34 -0
- data/spec/simple/httpd/services/service_spec.rb +34 -0
- data/spec/simple/httpd/static_mounting_spec.rb +13 -0
- data/spec/spec_helper.rb +30 -6
- data/spec/support/004_simplecov.rb +3 -12
- metadata +61 -84
- data/lib/simple/httpd/app.rb +0 -84
- data/lib/simple/httpd/app/file_server.rb +0 -19
- data/spec/simple/httpd/version_spec.rb +0 -10
- data/tasks/release.rake +0 -104
data/lib/simple/httpd/app.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
class Simple::Httpd::App
|
2
|
-
end
|
3
|
-
|
4
|
-
require_relative "app/file_server"
|
5
|
-
|
6
|
-
# The Simple::Httpd::App implements a Rack compatible app, which serves
|
7
|
-
# HTTP requests via a set of controllers inherited from a base controller
|
8
|
-
# class.
|
9
|
-
#
|
10
|
-
# The individual routes are determined from the controller class names:
|
11
|
-
# assuming we have a controller class Foo::Bar::BazController inheriting
|
12
|
-
# from Foo::BaseController will serve the /bar/baz routes.
|
13
|
-
#
|
14
|
-
# Additional static routes can be configured by calling "mount_directory!!"
|
15
|
-
# on the app object.
|
16
|
-
class Simple::Httpd::App
|
17
|
-
extend Forwardable
|
18
|
-
delegate call: :@app
|
19
|
-
|
20
|
-
attr_reader :logger
|
21
|
-
|
22
|
-
#
|
23
|
-
# Builds a App object
|
24
|
-
def initialize(base_controller, logger:)
|
25
|
-
raise unless logger
|
26
|
-
|
27
|
-
@base_controller = base_controller
|
28
|
-
@logger = logger
|
29
|
-
@file_mounts = []
|
30
|
-
|
31
|
-
@app = Rack::URLMap.new(controllers_url_map)
|
32
|
-
end
|
33
|
-
|
34
|
-
def mount_directory!(url:, path:)
|
35
|
-
@logger.debug "#{path}: mount directory #{path}"
|
36
|
-
@app = FileServer.new(@app, url_prefix: url, root: path)
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
# Find all controllers inheriting off base_controller and return
|
42
|
-
# a URL map, based on the names of the controllers.
|
43
|
-
def controllers_url_map
|
44
|
-
controller_by_mountpoint = ObjectSpace
|
45
|
-
.each_object(Class)
|
46
|
-
.select { |klass| klass < @base_controller }
|
47
|
-
.map { |controller| [mountpoint(controller), controller] }
|
48
|
-
.reject { |mountpoint, _controller| mountpoint.nil? }
|
49
|
-
|
50
|
-
controller_by_mountpoint
|
51
|
-
.sort_by { |path, _controller| path }
|
52
|
-
.each { |path, controller| logger.debug "#{path}: mount #{controller}" }
|
53
|
-
|
54
|
-
Hash[controller_by_mountpoint]
|
55
|
-
end
|
56
|
-
|
57
|
-
def mountpoint(controller)
|
58
|
-
return unless controller.name.end_with?("Controller")
|
59
|
-
|
60
|
-
relative_controller_name = relative_controller_name(controller)
|
61
|
-
return "/" if relative_controller_name == "RootController"
|
62
|
-
|
63
|
-
"/" + relative_controller_name.underscore.gsub(/_controller$/, "")
|
64
|
-
end
|
65
|
-
|
66
|
-
# With a controller of Postjob::FooBarController returns FooBarController
|
67
|
-
# (depending on the base_controller)
|
68
|
-
def relative_controller_name(controller)
|
69
|
-
controller_name = controller.name
|
70
|
-
if controller_name.start_with?(base_controller_namespace)
|
71
|
-
controller_name[base_controller_namespace.length..-1]
|
72
|
-
else
|
73
|
-
controller_name
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
# With a base_controller of Postjob::BaseController this returns "Postjob"
|
78
|
-
def base_controller_namespace
|
79
|
-
@base_controller_namespace ||= begin
|
80
|
-
base_controller_name = @base_controller.name
|
81
|
-
base_controller_name.gsub(/::BaseController$/, "::")
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class Simple::Httpd::App::FileServer
|
2
|
-
# A simple file server middleware
|
3
|
-
def initialize(app, url_prefix:, root:)
|
4
|
-
@app = app
|
5
|
-
@url_prefix = File.join("/", url_prefix, "/")
|
6
|
-
@file_server = Rack::File.new(root)
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(env)
|
10
|
-
request_path = env["PATH_INFO"]
|
11
|
-
if request_path.start_with?(@url_prefix)
|
12
|
-
file_path = request_path[@url_prefix.length..-1]
|
13
|
-
env["PATH_INFO"] = file_path
|
14
|
-
@file_server.call(env)
|
15
|
-
else
|
16
|
-
@app.call(env)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/tasks/release.rake
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
Bundler.setup
|
3
|
-
|
4
|
-
GEM_ROOT = File.expand_path('../../', __FILE__)
|
5
|
-
GEM_SPEC, more = Dir.glob("*.gemspec")
|
6
|
-
raise "Cannot determine gemspec" if more || !GEM_SPEC
|
7
|
-
|
8
|
-
VERSION_FILE_PATH = 'VERSION'
|
9
|
-
|
10
|
-
class VersionNumberTracker
|
11
|
-
class << self
|
12
|
-
def update_version_file(new_version_number)
|
13
|
-
File.open(VERSION_FILE_PATH, 'w') { |file| file.puts new_version_number }
|
14
|
-
new_version_number
|
15
|
-
end
|
16
|
-
|
17
|
-
def auto_version_bump
|
18
|
-
old_version_number = File.read("VERSION").chomp
|
19
|
-
old = old_version_number.split('.')
|
20
|
-
current = old[0..-2] << old[-1].next
|
21
|
-
new_version_number = current.join('.')
|
22
|
-
|
23
|
-
update_version_file(new_version_number)
|
24
|
-
end
|
25
|
-
|
26
|
-
def manual_version_bump
|
27
|
-
update_version_file(ENV['VERSION'])
|
28
|
-
end
|
29
|
-
|
30
|
-
def update_version_number
|
31
|
-
@version = ENV['VERSION'] ? manual_version_bump : auto_version_bump
|
32
|
-
end
|
33
|
-
|
34
|
-
attr_reader :version
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
namespace :release do
|
39
|
-
task :version do
|
40
|
-
VersionNumberTracker.update_version_number
|
41
|
-
end
|
42
|
-
|
43
|
-
task :build do
|
44
|
-
Dir.chdir(GEM_ROOT) do
|
45
|
-
sh("gem build #{GEM_SPEC}")
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
desc "Commit changes"
|
50
|
-
task :commit do
|
51
|
-
Dir.chdir(GEM_ROOT) do
|
52
|
-
version = VersionNumberTracker.version
|
53
|
-
sh("git add #{VERSION_FILE_PATH}")
|
54
|
-
sh("git commit -m \"bump to v#{version}\"")
|
55
|
-
sh("git tag -a v#{version} -m \"Tag\"")
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
desc "Push code and tags"
|
60
|
-
task :push do
|
61
|
-
sh("git push origin #{$TARGET_BRANCH}")
|
62
|
-
sh('git push --tags')
|
63
|
-
end
|
64
|
-
|
65
|
-
desc "Cleanup"
|
66
|
-
task :clean do
|
67
|
-
Dir.glob(File.join(GEM_ROOT, '*.gem')).each { |f| FileUtils.rm_rf(f) }
|
68
|
-
end
|
69
|
-
|
70
|
-
desc "Push Gem to gemfury"
|
71
|
-
task :publish do
|
72
|
-
Dir.chdir(GEM_ROOT) { sh("gem push #{Dir.glob('*.gem').first}") }
|
73
|
-
end
|
74
|
-
|
75
|
-
task :target_master do
|
76
|
-
$TARGET_BRANCH = 'master'
|
77
|
-
end
|
78
|
-
|
79
|
-
task :target_stable do
|
80
|
-
$TARGET_BRANCH = 'stable'
|
81
|
-
end
|
82
|
-
|
83
|
-
task :checkout do
|
84
|
-
sh "git status --untracked-files=no --porcelain > /dev/null || (echo '*** working dir not clean' && false)"
|
85
|
-
sh "git checkout #{$TARGET_BRANCH}"
|
86
|
-
sh "git pull"
|
87
|
-
end
|
88
|
-
|
89
|
-
task default: [
|
90
|
-
'checkout',
|
91
|
-
'version',
|
92
|
-
'clean',
|
93
|
-
'build',
|
94
|
-
'commit',
|
95
|
-
'push',
|
96
|
-
'publish'
|
97
|
-
]
|
98
|
-
|
99
|
-
task master: %w(target_master default)
|
100
|
-
task stable: %w(target_stable default)
|
101
|
-
end
|
102
|
-
|
103
|
-
desc "Clean, build, commit and push"
|
104
|
-
task release: 'release:master'
|