misosoup 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .buildpath
2
+ .project
3
+ *.gem
4
+ doc
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rake'
4
+ gem 'rack', '~> 1.2.0'
5
+ gem 'rspec', '~> 2.5.0'
6
+
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ rack (1.2.1)
6
+ rake (0.8.7)
7
+ rspec (2.5.0)
8
+ rspec-core (~> 2.5.0)
9
+ rspec-expectations (~> 2.5.0)
10
+ rspec-mocks (~> 2.5.0)
11
+ rspec-core (2.5.1)
12
+ rspec-expectations (2.5.0)
13
+ diff-lcs (~> 1.1.2)
14
+ rspec-mocks (2.5.0)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ rack (~> 1.2.0)
21
+ rake
22
+ rspec (~> 2.5.0)
data/README ADDED
@@ -0,0 +1,2 @@
1
+ Miso is a Rack application aggregator.
2
+ It allows running multiple Rack applications and a static site within the same server instance.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
data/bin/miso ADDED
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'getoptlong'
4
+
5
+ require File.expand_path('../lib/miso/commands', File.dirname(__FILE__))
6
+ require File.expand_path('../lib/miso/version', File.dirname(__FILE__))
7
+
8
+ module Miso
9
+ class Cli
10
+ def self.usage
11
+ puts <<EOF
12
+ USAGE: #{File.basename($0)} [GLOBAL OPTIONS] <command> [COMMAND OPTIONS]
13
+
14
+ GLOBAL OPTIONS
15
+ --help, -h Display this message.
16
+ --version, -v Display version number.
17
+
18
+ COMMANDS
19
+ new <path> Create a new Miso project.
20
+ EOF
21
+ exit 0
22
+ end
23
+
24
+ def self.version
25
+ puts "Miso #{Miso::VERSION}"
26
+ exit 0
27
+ end
28
+
29
+ def self.parse_command_line
30
+ opts = GetoptLong.new(
31
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
32
+ ['--version', '-v', GetoptLong::NO_ARGUMENT]
33
+ )
34
+ options = {}
35
+ opts.each do |opt, arg|
36
+ case opt
37
+ when '--help'
38
+ usage
39
+ when '--version'
40
+ version
41
+ else
42
+ options[opt.sub(/^--/, '')] = arg
43
+ end
44
+ end
45
+ options
46
+ end
47
+
48
+ def self.main(options)
49
+ command = ARGV.shift
50
+ command.nil? && usage
51
+ case command
52
+ when 'new'
53
+ Miso::Commands::New.new(ARGV[0], options).execute
54
+ else
55
+ usage
56
+ end
57
+ rescue StandardError => e
58
+ $stderr.puts "ERROR: #{e}"
59
+ usage
60
+ end
61
+ end
62
+ end
63
+
64
+ Miso::Cli.main(Miso::Cli.parse_command_line)
@@ -0,0 +1,45 @@
1
+ require 'fileutils'
2
+
3
+ module Miso
4
+ module Commands
5
+ class UsageError < RuntimeError; end
6
+
7
+ class New
8
+ def initialize(path, options = {})
9
+ path.nil? && (raise UsageError.new('path not specified'))
10
+ fail("#{path} already exists") if File.exist?(path)
11
+ @path = path
12
+ @options = options
13
+ end
14
+
15
+ def template_root
16
+ File.expand_path('../../template', File.dirname(__FILE__))
17
+ end
18
+
19
+ def copy_template(src, dest)
20
+ FileUtils.cp(File.join(template_root, src), dest)
21
+ end
22
+
23
+ def copy_templates(templates)
24
+ templates.each { |src, dest| copy_template(src, dest) }
25
+ end
26
+
27
+ def make_directories
28
+ %w[_app/miso _site].each do |dir|
29
+ FileUtils.mkdir_p(File.join(@path, dir))
30
+ end
31
+ end
32
+
33
+ def execute
34
+ make_directories
35
+ templates = {
36
+ 'config.ru' => "#{@path}/config.ru",
37
+ '_app/miso/config.ru' => "#{@path}/_app/miso/config.ru",
38
+ '_app/miso/miso.erb' => "#{@path}/_app/miso/miso.erb",
39
+ '_site/index.html' => "#{@path}/_site/index.html"
40
+ }
41
+ copy_templates(templates)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module Miso
2
+ # Load a Rack application from a config.ru file.
3
+ def load(app_path)
4
+ currentDir = Dir.getwd
5
+ Dir.chdir(currentDir+'/'+app_path)
6
+ rackup_code = ::File.read('config.ru')
7
+ app = eval("Rack::Builder.new {( #{rackup_code}\n )}.to_app")
8
+ Dir.chdir(currentDir)
9
+ app
10
+ end
11
+
12
+ module_function :load
13
+ end
@@ -0,0 +1,30 @@
1
+ module Miso
2
+ #Rack middleware that intercept request to the Rack application
3
+ #rewrite the path so that is seems to target the base URL.
4
+ class Rewrite
5
+ #Request env variable to rewrite.
6
+ @@REWRITE= ['REQUEST_PATH', 'PATH_INFO', 'REQUEST_URI']
7
+
8
+ #Receive the app and the option list (for now only the :app_path).
9
+ def initialize (app, option)
10
+ @app = app
11
+ @path = option[:app_path]
12
+ end
13
+
14
+ #Execute request.
15
+ def call(env)
16
+ rewritePath(env)
17
+ status, headers, response = @app.call(env)
18
+ [status, headers, response]
19
+ end
20
+
21
+ private
22
+
23
+ #Rewrite the path in the original request.
24
+ def rewritePath(env)
25
+ @@REWRITE.each do |key|
26
+ env[key] = env[key].gsub(@path, '')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ require 'rack'
2
+
3
+ module Miso
4
+ #Rack application to handle static site.
5
+ class StaticSite
6
+ @@NOT_FOUND_PAGE = '404.html'
7
+
8
+ #Receives static site base path.
9
+ def initialize(static_site_path)
10
+ @root = static_site_path
11
+ @static_page_server = Rack::Directory.new(@root)
12
+ end
13
+
14
+ #Execute request.
15
+ def call(env)
16
+ path = Rack::Utils.unescape(env['PATH_INFO'])
17
+ not_found = false
18
+
19
+ #if / then route to index.html
20
+ if path == "/"
21
+ # Return the index
22
+ env['PATH_INFO']="/index.html"
23
+ path = path+'index.html'
24
+ end
25
+
26
+ #if page does not exists then route to 404.html
27
+ if !::File.exists?(@root+path) and ::File.exists?(@root+'/'+@@NOT_FOUND_PAGE)
28
+ env['PATH_INFO']=@@NOT_FOUND_PAGE
29
+ not_found = true
30
+ end
31
+
32
+ #call Directory to resolve
33
+ code, headers, body = @static_page_server.call(env)
34
+
35
+ #override to 404 if page not found
36
+ [not_found ? 404 : code, headers, body]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module Miso
2
+ #Miso current version
3
+ VERSION = '0.1.0'
4
+ end
5
+
data/lib/miso.rb ADDED
@@ -0,0 +1,6 @@
1
+ ROOT = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require "#{ROOT}/miso/version"
4
+ require "#{ROOT}/miso/static"
5
+ require "#{ROOT}/miso/loader"
6
+ require "#{ROOT}/miso/rewrite"
data/misosoup.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "miso/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "misosoup"
7
+ s.version = Miso::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Felix Trepanier"]
10
+ s.email = ["felixtrepanier@gmail.com"]
11
+ s.homepage = "http://side-experiment.com"
12
+ s.summary = %q{Rack application aggregator}
13
+ s.description = <<-EOF
14
+ MisoSoup is a Rack application which can server any number of sub-Rack applications
15
+ and a static web site.
16
+ EOF
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency('rack', '~> 1.2.0')
24
+
25
+ # Test libraries
26
+ s.add_development_dependency('rspec', '2.5.0')
27
+ end
28
+
data/spec/404.html ADDED
@@ -0,0 +1 @@
1
+ 404 Error! Notfound...
data/spec/config.ru ADDED
@@ -0,0 +1,5 @@
1
+ @app = lambda do |env|
2
+ [200, {'Content-Type'=>'text/plain'}, 'test']
3
+ end
4
+
5
+ run @app
data/spec/index.html ADDED
@@ -0,0 +1 @@
1
+ for test
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Miso do
4
+ it 'should load config.ru' do
5
+ app = Miso.load 'spec'
6
+ code, headers, body = app.call({:dummy => 'dummy'})
7
+ code.should == 200
8
+ headers['Content-Type'].should == 'text/plain'
9
+ body.should == 'test'
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Miso::Rewrite do
4
+ before do
5
+ @initial_env = {'REQUEST_PATH' => '/spec/hello', 'PATH_INFO' => '/spec/hello', 'REQUEST_URI' => '/spec/hello'}
6
+ end
7
+
8
+ it 'should return app return value' do
9
+ mock_app = mock('mock_app')
10
+ mock_app.should_receive(:call).with(@initial_env).once.and_return([200, {'header' => 'value'}, 'body'])
11
+ app = Miso::Rewrite.new mock_app, :app_path => '/spec'
12
+ code, header, body = app.call(@initial_env)
13
+ code.should == 200
14
+ header['header'].should == 'value'
15
+ body.should == 'body'
16
+ end
17
+
18
+ it 'should rewrite path' do
19
+ expected_env = {'REQUEST_PATH' => '/hello', 'PATH_INFO' => '/hello', 'REQUEST_URI' => '/hello'}
20
+ mock_app = mock('mock_app')
21
+ mock_app.should_receive(:call).with(expected_env).once.and_return([200, {'header' => 'value'}, 'body'])
22
+ app = Miso::Rewrite.new mock_app, :app_path => '/spec'
23
+ code, header, body = app.call(@initial_env)
24
+ code.should == 200
25
+ header['header'].should == 'value'
26
+ body.should == 'body'
27
+ end
28
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib" #add lib to path
2
+ require "miso"
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Miso::StaticSite do
4
+ it 'should return serve files from directory' do
5
+ initial_env = {'PATH_INFO' => '/spec_helper.rb'}
6
+ app = Miso::StaticSite.new 'spec'
7
+ code, header, body = app.call(initial_env)
8
+ code.should == 200
9
+ body.instance_of? Rack::File
10
+ body.path.should match(/spec_helper.rb/)
11
+ end
12
+
13
+ it 'should map / to index.html' do
14
+ initial_env = {'PATH_INFO' => '/'}
15
+ app = Miso::StaticSite.new 'spec'
16
+ code, header, body = app.call(initial_env)
17
+ code.should == 200
18
+ body.instance_of? Rack::File
19
+ body.path.should match(/index.html/)
20
+ end
21
+
22
+ it 'should return 404.html is file do not exists' do
23
+ initial_env = {'PATH_INFO' => '/dummy.dumb'}
24
+ app = Miso::StaticSite.new 'spec'
25
+ code, header, body = app.call(initial_env)
26
+ code.should == 404
27
+ body.instance_of? Rack::File
28
+ body.path.should match(/404.html/)
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ require 'erb'
2
+ require 'miso'
3
+
4
+ $miso_app_folder = ::File.expand_path(::File.dirname(__FILE__))+'/..'
5
+ $miso_app_dir_list = ::Dir[$miso_app_folder+'/*'].reject{|o| not ::File.directory?(o)}
6
+ $miso_version = Miso::VERSION
7
+
8
+ $miso_page = ''
9
+ ::File.open("miso.erb", "r") do |infile|
10
+ infile.each do |line|
11
+ $miso_page<<line
12
+ end
13
+ end
14
+
15
+ @app = lambda do |env|
16
+ [200, {'Content-Type'=>'text/html'}, ERB.new($miso_page).result]
17
+ end
18
+
19
+ run @app
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html xmlns="http://www.w3.org/1999/xhtml\" xml:lang="en" lang="en-us">
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5
+ <title>miso</title>
6
+ <meta name="author" content="Felix Trepanier" />
7
+ </head>
8
+ <body>
9
+ <center>
10
+ <h2>running on miso version <%= Miso::VERSION %></h2>
11
+ <h3>app running:</h3>
12
+ <% $miso_app_dir_list.each do |app_name|%>
13
+ <p><%= app_name.split('/')[-1] %></p>
14
+ <% end %>
15
+ </center>
16
+ </body>
17
+ </html>
@@ -0,0 +1 @@
1
+ hello world
@@ -0,0 +1,18 @@
1
+ require 'miso'
2
+
3
+ $MISO_STATIC_ROOT = "_site"
4
+ $MISO_APP_ROOT = "_app"
5
+
6
+ app_dir_list = ::Dir[$MISO_APP_ROOT+'/*'].reject{|o| not ::File.directory?(o)}
7
+ app_dir_list.each do |app_folder|
8
+ app = Miso.load app_folder
9
+ app_name = app_folder.split('/')[-1]
10
+ map '/'+app_name do
11
+ use Miso::Rewrite, {:app_path => '/'+app_folder}
12
+ run app
13
+ end
14
+ end
15
+
16
+ map '/' do
17
+ run Miso::StaticSite.new $MISO_STATIC_ROOT
18
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: misosoup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Felix Trepanier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-03-19 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ requirement: &78646370 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *78646370
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &78646120 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - =
32
+ - !ruby/object:Gem::Version
33
+ version: 2.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *78646120
37
+ description: ! 'MisoSoup is a Rack application which can server any number of sub-Rack
38
+ applications
39
+
40
+ and a static web site.
41
+
42
+ '
43
+ email:
44
+ - felixtrepanier@gmail.com
45
+ executables:
46
+ - miso
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - README
54
+ - Rakefile
55
+ - bin/miso
56
+ - lib/miso.rb
57
+ - lib/miso/commands.rb
58
+ - lib/miso/loader.rb
59
+ - lib/miso/rewrite.rb
60
+ - lib/miso/static.rb
61
+ - lib/miso/version.rb
62
+ - misosoup.gemspec
63
+ - spec/404.html
64
+ - spec/config.ru
65
+ - spec/index.html
66
+ - spec/loader_spec.rb
67
+ - spec/rewrite_spec.rb
68
+ - spec/spec_helper.rb
69
+ - spec/static_spec.rb
70
+ - template/_app/miso/config.ru
71
+ - template/_app/miso/miso.erb
72
+ - template/_site/index.html
73
+ - template/config.ru
74
+ has_rdoc: true
75
+ homepage: http://side-experiment.com
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.6.2
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Rack application aggregator
99
+ test_files: []