wildsonet-server 0.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gem "rack"
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Marek Jelen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ = WildSoNet::Server
2
+
3
+ Server backend for WildSoNet.
4
+
5
+ == Implementation
6
+
7
+ Embeds jetty web server and implements Java's servlet with simple Rack interface.
8
+
9
+ == Status
10
+
11
+ * No tests
12
+
13
+ Not production ready ... experimental ... proof of concept.
14
+
15
+ == Usage
16
+
17
+ This server should be used behind front-end server.
18
+
19
+ === nginx
20
+
21
+ server {
22
+
23
+ listen <public_port>;
24
+ server_name <public_address>;
25
+
26
+ location /static/ {
27
+ internal;
28
+ alias <path_to_rails_application>/public/;
29
+ }
30
+
31
+ location / {
32
+ proxy_pass http://<rails_address>:<address_port>;
33
+ }
34
+
35
+ }
36
+
37
+ This template may be used to configure front-end nginx server to work with this server. <public_port> and
38
+ <public_address> is your front facing domain and port. <path_to_rails_application> points to location of your
39
+ rails application, this server uses nginx to serve static content. <rails_address> and <address_port> is address
40
+ where was your rails/rack application started.
41
+
42
+ === Rails
43
+
44
+ Start rails with the following command
45
+
46
+ rails server wildsonet -p <port> -b <hostname> -e <environment>
47
+
48
+ === Rack
49
+
50
+ Add this to your config.ru
51
+
52
+ WildSoNet::Server::Handler.run(App)
53
+
54
+ where App is your Rack application.
55
+
56
+ == To be done
57
+
58
+ * Configuration (threadpool)
59
+ * Lots of other features
60
+
61
+ == Contributing to wildsonet-server
62
+
63
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
64
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
65
+ * Fork the project
66
+ * Start a feature/bugfix branch
67
+ * Commit and push until you are happy with your contribution
68
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
69
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
70
+
71
+ == Copyright
72
+
73
+ Copyright (c) 2010 Marek Jelen. See LICENSE.txt for further details.
74
+
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,172 @@
1
+ require "java"
2
+
3
+ Dir.glob(File.join(File.dirname(__FILE__), "..", "jars", "*")) do |file|
4
+ require file
5
+ end
6
+
7
+ require "wildsonet-server-version"
8
+
9
+ # WildSoNet namespace
10
+ module WildSoNet
11
+
12
+ # Rack extensions from WildSoNet
13
+ module Server
14
+
15
+ java_import "org.eclipse.jetty.server.Server"
16
+ java_import "org.eclipse.jetty.server.nio.SelectChannelConnector"
17
+ java_import "org.eclipse.jetty.servlet.ServletContextHandler"
18
+ java_import "org.eclipse.jetty.servlet.ServletHolder"
19
+
20
+ java_import "org.jruby.embed.PathType"
21
+ java_import "org.jruby.embed.ScriptingContainer"
22
+ java_import "javax.servlet.http.HttpServlet"
23
+
24
+ # Rack handler utilizing Jetty web server. Works with nginx as frontend server to proxy the requests.
25
+ # Jetty handles only dynamic requests. Static requests are handled by nginx proxy.
26
+
27
+ class Handler < HttpServlet
28
+
29
+ # Starts the server.
30
+ #
31
+ # @param app Rack application to start
32
+ # @param options Options for server
33
+ def self.run app, options = {}
34
+ # Set default options
35
+ options[:Port] ||= 3000
36
+ options[:Host] ||= "0.0.0.0"
37
+
38
+ # Create new server
39
+ @@server = Server.new
40
+
41
+ # Create new connector and configure it according to options
42
+ connector = SelectChannelConnector.new
43
+ connector.port = options[:Port]
44
+ connector.host = options[:Host]
45
+
46
+ # Add connector to server
47
+ @@server.connectors = [connector]
48
+
49
+ # Create new server context and configure it ad root
50
+ context = ServletContextHandler.new(ServletContextHandler::NO_SESSIONS)
51
+ context.contextPath = "/"
52
+
53
+ # Add context to server
54
+ @@server.handler = context
55
+
56
+ # Create new handler
57
+ servlet = Handler.new
58
+ servlet.setup(app, options)
59
+
60
+ # Connect the handler with context
61
+ context.addServlet(ServletHolder.new(servlet), "/*")
62
+
63
+ # Start server
64
+ @@server.start
65
+ @@server.join
66
+
67
+ end
68
+
69
+ # Stops the server
70
+ def self.shutdown
71
+ # Stop server
72
+ @@server.stop
73
+ @@server = nil
74
+ end
75
+
76
+ # Setup the server.
77
+ #
78
+ # @param app Rack application to start
79
+ # @param options Options for server
80
+ def setup(app, options)
81
+ @app = app
82
+ @options = options
83
+
84
+ # By default serve static files from directory "public" in current directory
85
+ @options[:Public] ||= File.expand_path("public")
86
+ end
87
+
88
+ # Handles the request. Servlet method.
89
+ #
90
+ # @param request Request to process
91
+ # @param response Response to the request
92
+ def service request, response
93
+ # "Compute" the path to request file
94
+ file = File.join(@options[:Public], request.getRequestURI())
95
+ # Check file existence
96
+ if File.exists?(file) and File.file?(file)
97
+ # Tell the proxy server(nginx) to serve this request from static files
98
+ response.setHeader("X-Accel-Redirect", "/static" + request.getRequestURI())
99
+ else
100
+ # Process the request
101
+ self.call(request, response)
102
+ end
103
+ end
104
+
105
+ # Process the request
106
+ #
107
+ # @param request Request to be processed
108
+ # @param response Response to the request
109
+ def call request, response
110
+
111
+ # Prepare basic Rack environment
112
+ env = {
113
+ "REQUEST_METHOD" => request.getMethod(),
114
+ "SCRIPT_NAME" => "",
115
+ "PATH_INFO" => request.getPathInfo() =~ /^\// ? request.getPathInfo() : "/" + request.getPathInfo(),
116
+ "QUERY_STRING" => request.getQueryString(),
117
+ "SERVER_NAME" => request.getServerName(),
118
+ "SERVER_PORT" => request.getServerPort(),
119
+
120
+ "rack.version" => [1, 1],
121
+ "rack.url_scheme" => request.isSecure() ? "https" : "http",
122
+ "rack.input" => request.getInputStream().to_io,
123
+ "rack.errors" => $stderr,
124
+ "rack.multithread" => true,
125
+ "rack.multiprocess" => false,
126
+ "rack.run_once" => false,
127
+
128
+ # If this environment parameter is true after request processing, returned body will not be written to response
129
+ "wildsonet.written" => false
130
+ }
131
+
132
+ # Process HTTP headers from request; transforms header names into Rack format, if nil returned, the header is skipped
133
+ request.getHeaderNames().each do |name|
134
+ key = self.header_handler(name)
135
+ if key
136
+ env[key] = request.getHeader(name)
137
+ end
138
+ end
139
+
140
+ # Process the request
141
+ status, headers, content = @app.call(env)
142
+
143
+ # Copy headers from Rack response to Servlet response
144
+ headers.each_pair do |key, value|
145
+ response.addHeader(key, value)
146
+ end
147
+
148
+ # Set response status
149
+ response.setStatus(status)
150
+
151
+ # Copy the content of request into Servlet output stream(writer)
152
+ # Skips this step if environment parameter was set to true
153
+ content.each do |part|
154
+ response.writer.write(part)
155
+ end unless env["wildsonet.written"]
156
+
157
+ end
158
+
159
+ # Processes header names to Rack format
160
+ #
161
+ # @param name Header name to transform
162
+ # @return Rack formatted header name or nil to skip the header
163
+ def header_handler name
164
+ "HTTP_" + name.to_s.sub("-", "_").upcase
165
+ end
166
+
167
+ end
168
+ end
169
+ end
170
+
171
+ # Register the handler with Rack
172
+ Rack::Handler.register "wildsonet", "WildSoNet::Server::Handler"
@@ -0,0 +1,49 @@
1
+ lib = File.join(File.dirname(__FILE__), "lib")
2
+ $: << lib unless $:.include?(lib)
3
+
4
+ require "wildsonet-server-version"
5
+
6
+ Gem::Specification.new do |s|
7
+
8
+ s.name = "wildsonet-server"
9
+ s.version = WildSoNet::Server::VERSION
10
+ s.authors = ["Marek Jelen"]
11
+ s.summary = "Server backends for WildSoNet"
12
+ s.description = "Server backends for WildSoNet"
13
+ s.email = "marek@jelen.biz"
14
+ s.homepage = "http://github.com/marekjelen/wildsonet-server"
15
+ s.licenses = ["MIT"]
16
+
17
+ s.platform = "java"
18
+ s.required_rubygems_version = ">= 1.3.6"
19
+
20
+ s.extra_rdoc_files = [
21
+ "LICENSE.txt",
22
+ "README.rdoc"
23
+ ]
24
+
25
+ s.files = [
26
+ "Gemfile",
27
+ "LICENSE.txt",
28
+ "README.rdoc",
29
+ "jars/jetty-continuation-7.2.0.v20101020.jar",
30
+ "jars/jetty-http-7.2.0.v20101020.jar",
31
+ "jars/jetty-io-7.2.0.v20101020.jar",
32
+ "jars/jetty-security-7.2.0.v20101020.jar",
33
+ "jars/jetty-server-7.2.0.v20101020.jar",
34
+ "jars/jetty-servlet-7.2.0.v20101020.jar",
35
+ "jars/jetty-util-7.2.0.v20101020.jar",
36
+ "jars/servlet-api-2.5.jar",
37
+ "lib/wildsonet-server.rb",
38
+ "wildsonet-server.gemspec"
39
+ ]
40
+
41
+ s.require_paths = ["lib"]
42
+
43
+ s.test_files = [
44
+ ]
45
+
46
+ s.add_runtime_dependency("rack", ["> 1.0"])
47
+
48
+ end
49
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wildsonet-server
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: java
11
+ authors:
12
+ - Marek Jelen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-24 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">"
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ version: "1.0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Server backends for WildSoNet
34
+ email: marek@jelen.biz
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE.txt
41
+ - README.rdoc
42
+ files:
43
+ - Gemfile
44
+ - LICENSE.txt
45
+ - README.rdoc
46
+ - jars/jetty-continuation-7.2.0.v20101020.jar
47
+ - jars/jetty-http-7.2.0.v20101020.jar
48
+ - jars/jetty-io-7.2.0.v20101020.jar
49
+ - jars/jetty-security-7.2.0.v20101020.jar
50
+ - jars/jetty-server-7.2.0.v20101020.jar
51
+ - jars/jetty-servlet-7.2.0.v20101020.jar
52
+ - jars/jetty-util-7.2.0.v20101020.jar
53
+ - jars/servlet-api-2.5.jar
54
+ - lib/wildsonet-server.rb
55
+ - wildsonet-server.gemspec
56
+ has_rdoc: true
57
+ homepage: http://github.com/marekjelen/wildsonet-server
58
+ licenses:
59
+ - MIT
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 1
78
+ - 3
79
+ - 6
80
+ version: 1.3.6
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Server backends for WildSoNet
88
+ test_files: []
89
+