nephos-server 0.5.0 → 0.5.2
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/CHANGELOG +8 -0
- data/README.md +7 -3
- data/app/main.rb +3 -0
- data/bin/nephos-server +4 -1
- data/bin/nephos-status +3 -1
- data/lib/nephos-server/bin-helpers.rb +9 -0
- data/lib/nephos-server/controller.rb +7 -1
- data/lib/nephos-server/params.rb +12 -0
- data/lib/nephos-server/router/helpers.rb +21 -3
- data/lib/nephos-server/router/load.rb +23 -0
- data/lib/nephos-server/router/main.rb +9 -2
- data/lib/nephos-server/server/responder.rb +8 -7
- data/test/responder.rb +1 -1
- data/version +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0339bf695959f0e5b19b9b73609e561dfb496f23
|
4
|
+
data.tar.gz: ed4d9148e39b2540a88d5160d38dfe8d36499d3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db650e2505b3f6a3b54efdfc5d1dfa4da006bed596f54a2b615928c0f676dd626da8a2c6a49073914080566254ddc325cc6d27291c4d09f8e19a82a94fdc1c4a
|
7
|
+
data.tar.gz: 6a05e3f8d9d0ece9e41faf43218e6741f30b29246add62032094a7650217a68f5186736e8d358a1e0e8ae52374cf566c4c23259c29919c72440f9e7d8491039a
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
v0.5.2
|
2
|
+
* fix regression on content-type from v0.4
|
3
|
+
|
4
|
+
v0.5.1
|
5
|
+
* fix nephos gem (binaries can be used out of an application)
|
6
|
+
* improve code documentation (annotations above the methods and classes)
|
7
|
+
* fix content type for json to application/json
|
8
|
+
|
1
9
|
v0.5.0
|
2
10
|
* improve executables: version taking in count
|
3
11
|
|
data/README.md
CHANGED
@@ -2,11 +2,14 @@
|
|
2
2
|
|
3
3
|
[](http://badge.fury.io/gh/pouleta%2FNephosRubyServer)
|
4
4
|
|
5
|
-
[](http://badge.fury.io/rb/nephos-server)
|
6
6
|
|
7
7
|
[](https://codeclimate.com/github/pouleta/NephosRubyServer)
|
8
8
|
|
9
|
-
|
9
|
+
[](http://badge.fury.io/rb/nephos)
|
10
|
+
|
11
|
+
|
12
|
+
This is a minimal web server, based on [rack](https://github.com/rack/rack) and [puma](https://github.com/puma/puma).
|
10
13
|
It is written in ruby. It also gives you a minimal architecture
|
11
14
|
to speed up your application bootstrap.
|
12
15
|
|
@@ -48,6 +51,7 @@ Theses guides will provide you knowlegde about everything you can use in the app
|
|
48
51
|
- [Generator GUIDE](DOCUMENTATION/GUIDE_GENERATOR.md)
|
49
52
|
- [Render API](DOCUMENTATION/API_RENDER.md)
|
50
53
|
- [Router GUIDE](DOCUMENTATION/GUIDE_ROUTER.md)
|
54
|
+
- [Code documentation on RubyDoc.info](http://www.rubydoc.info/gems/nephos-server/toplevel) -> **Note: you can also generate local documentation via yard**
|
51
55
|
|
52
56
|
## Examples
|
53
57
|
|
@@ -110,9 +114,9 @@ end
|
|
110
114
|
# Developers: Roadmap
|
111
115
|
|
112
116
|
## TODO v0.5
|
113
|
-
- executables with version
|
114
117
|
- cookies, ...
|
115
118
|
- usage of rack parsers (Rack::Request.new(env) etc.)
|
119
|
+
- improved code documentation
|
116
120
|
|
117
121
|
## TODO v0.6
|
118
122
|
- startable as daemon
|
data/app/main.rb
CHANGED
data/bin/nephos-server
CHANGED
@@ -9,7 +9,7 @@ require 'nephos-server/bin-helpers'
|
|
9
9
|
class RoutingError < StandardError; end
|
10
10
|
|
11
11
|
begin
|
12
|
-
OptionParser.new do |opts|
|
12
|
+
opts = OptionParser.new do |opts|
|
13
13
|
opts.banner = "Usage<#{Nephos::VERSION}>: nephos-server <options>"
|
14
14
|
|
15
15
|
$server_port = ENV["SERVER_PORT"] || 8080
|
@@ -34,8 +34,11 @@ begin
|
|
34
34
|
opts.on("--test", "Enable testing mode (for nephos developpers)") do
|
35
35
|
$test = true
|
36
36
|
end
|
37
|
+
|
37
38
|
end.parse!
|
38
39
|
|
40
|
+
Dir.chdir(opts[0]) if not opts.empty?
|
41
|
+
|
39
42
|
if $test
|
40
43
|
require_relative "../lib/nephos-server"
|
41
44
|
else
|
data/bin/nephos-status
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
module Nephos
|
2
2
|
module Bin
|
3
3
|
|
4
|
+
# @param dir [String]
|
5
|
+
#
|
6
|
+
# The method check in the parameter directory:
|
7
|
+
# - if the directory exists
|
8
|
+
# - if a Gemfile.lock has been generated
|
9
|
+
# - if it contain nephos-server dependency
|
10
|
+
#
|
11
|
+
# note: if the Gemfile includes nephos and not nephos-server,
|
12
|
+
# it will work anyway, because nephos require nephos-server
|
4
13
|
def self.is_a_valid_application? dir="."
|
5
14
|
return false if not Dir.exists? dir
|
6
15
|
gfl = File.expand_path "Gemfile.lock", dir
|
@@ -1,7 +1,12 @@
|
|
1
1
|
module Nephos
|
2
|
+
|
3
|
+
# This class must be inherited by the other Controllers.
|
4
|
+
# It contains a constructor (you should not rewrite it)
|
5
|
+
# It contains some helpers too, like an access to the environment,
|
6
|
+
# and the parameters.
|
2
7
|
class Controller
|
3
8
|
|
4
|
-
attr_reader :env, :infos, :callpath, :params
|
9
|
+
attr_reader :env, :infos, :callpath, :params, :cookies
|
5
10
|
|
6
11
|
# @param env [Hash] env extracted from the http request
|
7
12
|
# @param parsed [Hash] pre-parsed env with parameters, ...
|
@@ -19,6 +24,7 @@ module Nephos
|
|
19
24
|
@params.merge! Hash[callpath[:params].zip @infos[:path]]
|
20
25
|
@params.select!{|k,v| not k.to_s.empty?}
|
21
26
|
@params = Params.new(@params)
|
27
|
+
@cookies = Params.new()
|
22
28
|
end
|
23
29
|
|
24
30
|
end
|
data/lib/nephos-server/params.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
module Nephos
|
4
|
+
|
5
|
+
# Params to a hash, where every elements are accessibles via a stringified key
|
6
|
+
# so, even if the entry was added with the key :KEY, it will be accessible via
|
7
|
+
# a string equivalent to :key.to_s
|
8
|
+
# param["key"] == param[:key]
|
9
|
+
#
|
10
|
+
# Every methods present in {Hash} are usable in {Param}.
|
4
11
|
class Params
|
5
12
|
|
13
|
+
# @param hash [Hash] hash containing the parameters
|
6
14
|
def initialize(hash={})
|
7
15
|
raise ArgumentError, "the first argument must be a Hash" unless hash.is_a? Hash
|
8
16
|
@hash = Hash[hash.map{|k,v| [k.to_s, v]}]
|
@@ -21,5 +29,9 @@ module Nephos
|
|
21
29
|
@hash[i.to_s] = v.to_s
|
22
30
|
end
|
23
31
|
|
32
|
+
def to_h
|
33
|
+
return @hash
|
34
|
+
end
|
35
|
+
|
24
36
|
end
|
25
37
|
end
|
@@ -3,6 +3,17 @@ def route_prefix
|
|
3
3
|
File.join(["/"] + @route_prefix)
|
4
4
|
end
|
5
5
|
|
6
|
+
# @param verb [String] has to be a valid http verb, so a string uppercase
|
7
|
+
# @param what [Hash] has to contain the following keys:
|
8
|
+
# - :url
|
9
|
+
# - :controller
|
10
|
+
# - :method
|
11
|
+
#
|
12
|
+
# The method create a valid route, set in Nephos::Router::ROUTES
|
13
|
+
# it will call the method from the controller, based on the parameters
|
14
|
+
# if the client request match with the verb and the url provided.
|
15
|
+
#
|
16
|
+
# Checkout the documentation about the parameters and API for more informations
|
6
17
|
def add_route(verb, what)
|
7
18
|
raise InvalidRoute, "what must be a hash" unless what.is_a? Hash
|
8
19
|
what[:url] = File.expand_path File.join(route_prefix, what[:url])
|
@@ -11,21 +22,28 @@ def add_route(verb, what)
|
|
11
22
|
Nephos::Router.add(what, verb)
|
12
23
|
end
|
13
24
|
|
14
|
-
# @param what [Hash]
|
25
|
+
# @param what [Hash] see {#add_route}
|
15
26
|
def get what
|
16
27
|
add_route "GET", what
|
17
28
|
end
|
18
29
|
|
19
|
-
# @param what [Hash]
|
30
|
+
# @param what [Hash] see {#add_route}
|
20
31
|
def post what
|
21
32
|
add_route "POST", what
|
22
33
|
end
|
23
34
|
|
24
|
-
# @param what [Hash]
|
35
|
+
# @param what [Hash] see {#add_route}
|
25
36
|
def put what
|
26
37
|
add_route "PUT", what
|
27
38
|
end
|
28
39
|
|
40
|
+
# @param name [String]
|
41
|
+
# @param block [Bloc]
|
42
|
+
#
|
43
|
+
# Create a resource named based on the parameter name
|
44
|
+
# Every call of {#add_route} {#get} {#post} {#put} in the bloc
|
45
|
+
# will have a modified url, working with the following schema:
|
46
|
+
# "/name/" + url
|
29
47
|
def resource(name, &block)
|
30
48
|
@route_prefix ||= []
|
31
49
|
@route_prefix << name
|
@@ -8,6 +8,9 @@ module Nephos
|
|
8
8
|
return display
|
9
9
|
end
|
10
10
|
|
11
|
+
# @param what [Hash]
|
12
|
+
#
|
13
|
+
# TODO: doc
|
11
14
|
def self.add_params!(what)
|
12
15
|
params = what[:url].split('/').map do |p|
|
13
16
|
p.match(/:\w+/) ? {p: "[^\/]+", name: p} : {p: p, name: nil}
|
@@ -18,12 +21,28 @@ module Nephos
|
|
18
21
|
what[:params] = params.map{|e| e[:name] && e[:name][1..-1]}[1..-1] || []
|
19
22
|
end
|
20
23
|
|
24
|
+
# @param what [Hash]
|
25
|
+
#
|
26
|
+
# Check if the what parameter contains the needed keys
|
27
|
+
# - :url
|
28
|
+
# - :controller
|
29
|
+
# - :method
|
21
30
|
def self.check_keys! what
|
22
31
|
raise InvalidRouteUrl, "Missing URL" unless what.keys.include? :url
|
23
32
|
raise InvalidRouteController, "Missing Controller" unless what.keys.include? :controller
|
24
33
|
raise InvalidRouteMethod, "Missing Method" unless what.keys.include? :method
|
25
34
|
end
|
26
35
|
|
36
|
+
# @param what [Hash]
|
37
|
+
#
|
38
|
+
# TODO:
|
39
|
+
# - Improve instanciation test
|
40
|
+
#
|
41
|
+
# Check if:
|
42
|
+
# - the what parameter contains a :controller
|
43
|
+
# - this controller exists
|
44
|
+
# - if the controller is a child of the {Controller} class
|
45
|
+
# - if the controller is instanciable
|
27
46
|
def self.check_controller! what
|
28
47
|
begin
|
29
48
|
controller = Module.const_get(what[:controller])
|
@@ -41,6 +60,10 @@ module Nephos
|
|
41
60
|
return instance
|
42
61
|
end
|
43
62
|
|
63
|
+
# @param what [Hash]
|
64
|
+
# @param instance [Controller]
|
65
|
+
#
|
66
|
+
# Check if the param instance has a method named what[:method]
|
44
67
|
def self.check_method! what, instance
|
45
68
|
if not instance.respond_to? what[:method]
|
46
69
|
raise InvalidRouteMethod, "No method named \"#{what[:method]}\""
|
@@ -4,18 +4,23 @@ end
|
|
4
4
|
puts
|
5
5
|
|
6
6
|
module Nephos
|
7
|
+
|
8
|
+
# The {Router} provides an interface between the {Controller} and the client
|
9
|
+
# queries.
|
7
10
|
module Router
|
8
11
|
|
9
12
|
ROUTES = []
|
10
13
|
|
11
14
|
# @param arg [Hash or Symbol]
|
12
|
-
#
|
15
|
+
#
|
16
|
+
# Shortcut to #{Nephos::Responder.render}
|
13
17
|
def self.render arg
|
14
18
|
Responder.render arg
|
15
19
|
end
|
16
20
|
|
17
21
|
# @param path [Array]
|
18
|
-
#
|
22
|
+
#
|
23
|
+
# Find the right route to use from the url
|
19
24
|
def self.parse_path path, verb
|
20
25
|
route = File.join(["/"] + path)
|
21
26
|
return ROUTES.find{|e| e[:match] =~ route and e[:verb] == verb}
|
@@ -41,6 +46,8 @@ module Nephos
|
|
41
46
|
end
|
42
47
|
end
|
43
48
|
|
49
|
+
# Interface which handle the client query (stored in env), calls the
|
50
|
+
# {Controller} method (using the routes) and render it's return
|
44
51
|
def self.execute(env)
|
45
52
|
begin
|
46
53
|
route = URI.parse(env['REQUEST_URI'])
|
@@ -6,7 +6,7 @@ module Nephos
|
|
6
6
|
CT_CHARSET_PREFIX = '; charset='
|
7
7
|
|
8
8
|
def self.content_type(kind, type, charset='UTF-8')
|
9
|
-
|
9
|
+
"#{kind}/#{type}" + CT_CHARSET_PREFIX + charset
|
10
10
|
end
|
11
11
|
|
12
12
|
# @param params [Hash] containing :type => "kind/type", example: "text/html"
|
@@ -21,7 +21,7 @@ module Nephos
|
|
21
21
|
PRESET_CT = {
|
22
22
|
plain: "text/plain",
|
23
23
|
html: "text/html",
|
24
|
-
json: "
|
24
|
+
json: "application/json",
|
25
25
|
}
|
26
26
|
|
27
27
|
private
|
@@ -67,11 +67,12 @@ module Nephos
|
|
67
67
|
return [204, ct_specific({type: PRESET_CT[:plain]}), [""]] if params == :empty
|
68
68
|
return render(status: params) if params.is_a? Integer
|
69
69
|
params = set_default_params(params)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
resp = Rack::Response.new
|
71
|
+
resp.status = params[:status]
|
72
|
+
resp["Content-Type"] = params[:type]
|
73
|
+
resp.body = [params[:content]]
|
74
|
+
#resp.set_cookies ...
|
75
|
+
return resp
|
75
76
|
end
|
76
77
|
|
77
78
|
end
|
data/test/responder.rb
CHANGED
@@ -70,7 +70,7 @@ class TestNephosServerResponder < Test::Unit::TestCase
|
|
70
70
|
Nephos::Responder.set_default_params_type(p4)
|
71
71
|
Nephos::Responder.set_default_params_type(p5)
|
72
72
|
plain = Nephos::Responder.ct_specific({type: "text/plain"})
|
73
|
-
json = Nephos::Responder.ct_specific({type: "
|
73
|
+
json = Nephos::Responder.ct_specific({type: "application/json"})
|
74
74
|
html = Nephos::Responder.ct_specific({type: "text/html"})
|
75
75
|
assert_equal plain, p1[:type]
|
76
76
|
assert_equal html, p2[:type]
|
data/version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.2
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nephos-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- poulet_a
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nomorebeer
|
@@ -164,5 +164,6 @@ rubyforge_project:
|
|
164
164
|
rubygems_version: 2.4.8
|
165
165
|
signing_key:
|
166
166
|
specification_version: 4
|
167
|
-
summary: "*
|
167
|
+
summary: "* fix regression on content-type from v0.4"
|
168
168
|
test_files: []
|
169
|
+
has_rdoc:
|