lane_groove 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/lane_groove +18 -0
- data/bin/lane_groove_test +16 -0
- data/lib/app.rb +86 -0
- data/test/lane_groove_test.rb +163 -0
- metadata +104 -0
data/bin/lane_groove
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'forever'
|
3
|
+
|
4
|
+
WORKING_DIR = Dir.getwd
|
5
|
+
puts "Working directory is #{WORKING_DIR}"
|
6
|
+
puts "WARNING: Working directory is empty!" if Dir.glob(File.join(WORKING_DIR, '*.yaml')).empty?
|
7
|
+
|
8
|
+
Forever.run do
|
9
|
+
dir WORKING_DIR
|
10
|
+
log File.join(WORKING_DIR, 'lane_groove.log')
|
11
|
+
pid File.join(WORKING_DIR, 'lane_groove.pid')
|
12
|
+
|
13
|
+
on_ready do
|
14
|
+
at_exit { stop! }
|
15
|
+
require '../lib/app.rb'
|
16
|
+
LaneGroove.run! :port => 6666
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
WORKING_DIR = Dir.getwd
|
6
|
+
|
7
|
+
config_files = Dir.glob File.join(WORKING_DIR, '*.yaml')
|
8
|
+
|
9
|
+
config = {}
|
10
|
+
|
11
|
+
config_files.each do |file|
|
12
|
+
puts "Loading #{file}"
|
13
|
+
config[File.basename(file, '.yaml').to_sym] = YAML.load_file(file)
|
14
|
+
end
|
15
|
+
|
16
|
+
puts config.to_yaml
|
data/lib/app.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
require 'xmlsimple'
|
4
|
+
require 'sinatra/base'
|
5
|
+
require 'rack/contrib'
|
6
|
+
|
7
|
+
class Hash
|
8
|
+
def upcase_keys
|
9
|
+
self.inject({}){ |new_hash, key_value|
|
10
|
+
key, value = key_value
|
11
|
+
value = value.upcase_keys if value.is_a?(Hash)
|
12
|
+
new_hash[key.upcase] = value
|
13
|
+
new_hash
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class LaneGroove < Sinatra::Base
|
19
|
+
use Rack::CommonLogger
|
20
|
+
use Rack::Access, '/' => %w{ 127.0.0.1 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 }
|
21
|
+
|
22
|
+
set :config, nil
|
23
|
+
|
24
|
+
static_dir = File.join(WORKING_DIR, 'static')
|
25
|
+
|
26
|
+
if File.exists? static_dir
|
27
|
+
puts "Serving static files from #{static_dir}"
|
28
|
+
set :static, true
|
29
|
+
set :public, static_dir
|
30
|
+
end
|
31
|
+
|
32
|
+
helpers do
|
33
|
+
def config_files
|
34
|
+
Dir.glob File.join(WORKING_DIR, '*.yaml')
|
35
|
+
end
|
36
|
+
|
37
|
+
def content_type_for(format)
|
38
|
+
{
|
39
|
+
:json => {'Content-Type' => 'application/json; charset=utf-8'},
|
40
|
+
:xml => {'Content-Type' => 'application/xml; charset=utf-8'},
|
41
|
+
:yaml => {'Content-Type' => 'application/x-yaml; charset=utf-8'},
|
42
|
+
:rb => {'Content-Type' => 'application/x-ruby; charset=utf-8'}
|
43
|
+
}[format]
|
44
|
+
end
|
45
|
+
|
46
|
+
def reload_yaml
|
47
|
+
self.class.config = {}
|
48
|
+
|
49
|
+
config_files.each do |file|
|
50
|
+
puts "Loading #{file}"
|
51
|
+
self.class.config[File.basename(file, '.yaml').to_sym] = YAML.load_file(file)
|
52
|
+
end
|
53
|
+
|
54
|
+
config.to_yaml
|
55
|
+
end
|
56
|
+
|
57
|
+
def config(*path)
|
58
|
+
reload_yaml if self.class.config.nil?
|
59
|
+
cfg = self.class.config
|
60
|
+
|
61
|
+
path.each do |node|
|
62
|
+
cfg = cfg[node]
|
63
|
+
break if cfg.nil?
|
64
|
+
end
|
65
|
+
|
66
|
+
return cfg
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
get /^(.*)\.([\w\d]{2,4})$/ do |path, ext|
|
71
|
+
reload_yaml if params['reload']
|
72
|
+
|
73
|
+
ext = ext.to_sym
|
74
|
+
path = path.split('/').delete_if{|n| n.empty?}.compact.map{|n| n.to_sym}
|
75
|
+
|
76
|
+
case ext
|
77
|
+
when :yaml then [200, content_type_for(:yaml), config(*path).to_yaml]
|
78
|
+
when :json then [200, content_type_for(:json), config(*path).to_json]
|
79
|
+
when :xml then [200, content_type_for(:xml), XmlSimple.xml_out(config(*path), 'RootName' => nil, 'NoAttr' => true)]
|
80
|
+
when :XML then [200, content_type_for(:xml), XmlSimple.xml_out(config(*path).upcase_keys, 'RootName' => nil, 'NoAttr' => true)]
|
81
|
+
when :rb then [200, content_type_for(:rb), config(*path).inspect]
|
82
|
+
else ; [404, {}, "unknown format #{ext}"]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
WORKING_DIR = File.join(Dir.getwd, 'config_files')
|
2
|
+
|
3
|
+
require '../lib/app.rb'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
class LaneGrooveTest < Test::Unit::TestCase
|
9
|
+
include Rack::Test::Methods
|
10
|
+
|
11
|
+
def app
|
12
|
+
LaneGroove
|
13
|
+
end
|
14
|
+
|
15
|
+
def config
|
16
|
+
{:test=>{:db=>{:host=>"my-db-server", :user=>"root", :pass=>"none"}, :redis=>{:host=>"my-redis-server"}}}
|
17
|
+
end
|
18
|
+
|
19
|
+
def config_all_xml
|
20
|
+
<<-EOX
|
21
|
+
<test>
|
22
|
+
<db>
|
23
|
+
<host>my-db-server</host>
|
24
|
+
<user>root</user>
|
25
|
+
<pass>none</pass>
|
26
|
+
</db>
|
27
|
+
<redis>
|
28
|
+
<host>my-redis-server</host>
|
29
|
+
</redis>
|
30
|
+
</test>
|
31
|
+
EOX
|
32
|
+
end
|
33
|
+
|
34
|
+
def config_db_xml
|
35
|
+
<<-EOX
|
36
|
+
<host>my-db-server</host>
|
37
|
+
<user>root</user>
|
38
|
+
<pass>none</pass>
|
39
|
+
EOX
|
40
|
+
end
|
41
|
+
|
42
|
+
def config_all_XML
|
43
|
+
<<-EOX
|
44
|
+
<TEST>
|
45
|
+
<DB>
|
46
|
+
<HOST>my-db-server</HOST>
|
47
|
+
<USER>root</USER>
|
48
|
+
<PASS>none</PASS>
|
49
|
+
</DB>
|
50
|
+
<REDIS>
|
51
|
+
<HOST>my-redis-server</HOST>
|
52
|
+
</REDIS>
|
53
|
+
</TEST>
|
54
|
+
EOX
|
55
|
+
end
|
56
|
+
|
57
|
+
def config_db_XML
|
58
|
+
<<-EOX
|
59
|
+
<HOST>my-db-server</HOST>
|
60
|
+
<USER>root</USER>
|
61
|
+
<PASS>none</PASS>
|
62
|
+
EOX
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_all_rb
|
66
|
+
get '/.rb'
|
67
|
+
assert_equal 'application/x-ruby; charset=utf-8', last_response.content_type
|
68
|
+
assert_equal config.inspect, last_response.body
|
69
|
+
end
|
70
|
+
def test_db_rb
|
71
|
+
get '/test/db.rb'
|
72
|
+
assert_equal 'application/x-ruby; charset=utf-8', last_response.content_type
|
73
|
+
assert_equal config[:test][:db].inspect, last_response.body
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_all_yaml
|
77
|
+
get '/.yaml'
|
78
|
+
assert_equal 'application/x-yaml; charset=utf-8', last_response.content_type
|
79
|
+
assert_equal config.to_yaml, last_response.body
|
80
|
+
end
|
81
|
+
def test_db_yaml
|
82
|
+
get '/test/db.yaml'
|
83
|
+
assert_equal 'application/x-yaml; charset=utf-8', last_response.content_type
|
84
|
+
assert_equal config[:test][:db].to_yaml, last_response.body
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_all_json
|
88
|
+
get '/.json'
|
89
|
+
assert_equal 'application/json; charset=utf-8', last_response.content_type
|
90
|
+
assert_equal config.to_json, last_response.body
|
91
|
+
end
|
92
|
+
def test_db_json
|
93
|
+
get '/test/db.json'
|
94
|
+
assert_equal 'application/json; charset=utf-8', last_response.content_type
|
95
|
+
assert_equal config[:test][:db].to_json, last_response.body
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_all_xml
|
99
|
+
get '/.xml'
|
100
|
+
assert_equal 'application/xml; charset=utf-8', last_response.content_type
|
101
|
+
assert_equal config_all_xml, last_response.body
|
102
|
+
end
|
103
|
+
def test_db_xml
|
104
|
+
get '/test/db.xml'
|
105
|
+
assert_equal 'application/xml; charset=utf-8', last_response.content_type
|
106
|
+
assert_equal config_db_xml, last_response.body
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_all_XML
|
110
|
+
get '/.XML'
|
111
|
+
assert_equal 'application/xml; charset=utf-8', last_response.content_type
|
112
|
+
assert_equal config_all_XML, last_response.body
|
113
|
+
end
|
114
|
+
def test_db_XML
|
115
|
+
get '/test/db.XML'
|
116
|
+
assert_equal 'application/xml; charset=utf-8', last_response.content_type
|
117
|
+
assert_equal config_db_XML, last_response.body
|
118
|
+
end
|
119
|
+
|
120
|
+
def reset_assets
|
121
|
+
FileUtils.mv('config_files/more_test.yaml', 'config_files/test.yaml') if File.exists?('config_files/more_test.yaml')
|
122
|
+
FileUtils.mv('config_files/no_static', 'config_files/static') if File.exists?('config_files/no_static')
|
123
|
+
end
|
124
|
+
|
125
|
+
def setup
|
126
|
+
reset_assets
|
127
|
+
end
|
128
|
+
|
129
|
+
def teardown
|
130
|
+
reset_assets
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_reloading
|
134
|
+
get '/test/db.rb'
|
135
|
+
assert_equal config[:test][:db].inspect, last_response.body
|
136
|
+
|
137
|
+
FileUtils.mv('config_files/test.yaml', 'config_files/more_test.yaml')
|
138
|
+
|
139
|
+
get '/test/db.rb'
|
140
|
+
assert_equal config[:test][:db].inspect, last_response.body
|
141
|
+
|
142
|
+
get '/more_test/db.rb?reload=true'
|
143
|
+
assert_equal config[:test][:db].inspect, last_response.body
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_network_restrictions
|
147
|
+
get '/.yaml', {}, {'REMOTE_ADDR' => '134.34.3.2'}
|
148
|
+
assert_equal 403, last_response.status
|
149
|
+
assert_equal '', last_response.body
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_static_files
|
153
|
+
get '/some.txt'
|
154
|
+
assert_equal 'some text', last_response.body
|
155
|
+
end
|
156
|
+
def test_static_files_no_dir
|
157
|
+
FileUtils.mv('config_files/static', 'config_files/no_static')
|
158
|
+
get '/some.txt'
|
159
|
+
assert_equal 404, last_response.status
|
160
|
+
assert_equal 'unknown format txt', last_response.body
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lane_groove
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Niko
|
9
|
+
- Dittmann
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2011-07-26 00:00:00 +02:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: xml-simple
|
19
|
+
prerelease: false
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: "0"
|
26
|
+
type: :runtime
|
27
|
+
version_requirements: *id001
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: sinatra
|
30
|
+
prerelease: false
|
31
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: "0"
|
37
|
+
type: :runtime
|
38
|
+
version_requirements: *id002
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: rack-contrib
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id003
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rack-test
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id004
|
61
|
+
description: A small HTTP configuration server. Eats YAML, returns JSON, XML, YAML and rb. Restricts requests to local subnet.
|
62
|
+
email: mail@niko-dittmann.com
|
63
|
+
executables:
|
64
|
+
- lane_groove
|
65
|
+
- lane_groove_test
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files: []
|
69
|
+
|
70
|
+
files:
|
71
|
+
- lib/app.rb
|
72
|
+
- test/lane_groove_test.rb
|
73
|
+
- bin/lane_groove
|
74
|
+
- bin/lane_groove_test
|
75
|
+
has_rdoc: true
|
76
|
+
homepage: http://github.com/niko/lane_groove
|
77
|
+
licenses: []
|
78
|
+
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: "0"
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: nowarning
|
99
|
+
rubygems_version: 1.5.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: A small HTTP configuration server. Eats YAML, returns JSON, XML, YAML and rb. Restricts requests to local subnet.
|
103
|
+
test_files:
|
104
|
+
- test/lane_groove_test.rb
|