nephos-server 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0839af6ab21e208af170dba120d5bac00a34d287
4
+ data.tar.gz: 92b2d4b2760723ade7d820289c5c1c36c5a80c25
5
+ SHA512:
6
+ metadata.gz: 753a000e9e45bab9c728cc3345b9d81a692b564b84cfaeff4c371d951f19010d4c872de4c21e9522119a2c7d8e3cd42f20371273c7551235e1b9b2dad7898f76
7
+ data.tar.gz: b86e626ee836f1117c580ea4c26ec02ccbb1e5da83167212feed4dd99fb20e6aaa88c454072db1d5c171f63af29324377f3bc93650d2ee69b6cf9337deae9237
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rack'
4
+ gem 'nomorebeer'
data/Gemfile.lock ADDED
@@ -0,0 +1,15 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ nomorebeer (1.0)
5
+ rack (1.6.4)
6
+
7
+ PLATFORMS
8
+ ruby
9
+
10
+ DEPENDENCIES
11
+ nomorebeer
12
+ rack
13
+
14
+ BUNDLED WITH
15
+ 1.10.5
data/Procfile ADDED
@@ -0,0 +1 @@
1
+ web: bundle exec bin/server -p $PORT
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Nephos Ruby Server
2
+
3
+ This is a simple web server, based on rack and puma, with a minimal help:
4
+
5
+ - Controllers
6
+ - Rendering
7
+ - Routing
8
+
9
+
10
+ # TODO
11
+
12
+ - Improve rendering
13
+ - html support
14
+ - Routing
15
+ - improve get with arguments
16
+ - add post and put (like get)
17
+ - add ressource (elarge urls)
18
+ - Database connection
19
+
20
+
21
+ # Start
22
+
23
+ ```sh
24
+ gem install nephos-server
25
+ nephos-server -p 8080 # port is not required
26
+ ```
27
+
28
+
29
+ # Documentation
30
+
31
+ ## Controller
32
+
33
+ To create a controller, simply add it to ``src/``.
34
+ The basic code of a controller can be generated via ``bin/generate controller NAME``.
35
+
36
+ ```ruby
37
+ class Example < Nephos::Controller
38
+ def root
39
+ return {plain: "index"}
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Rendering
45
+
46
+ In a controller, use:
47
+
48
+ ```ruby
49
+ return {status: code}
50
+ return {json: {...}}
51
+ return {plain: "text"}
52
+ return :empty
53
+ ```
54
+
55
+ ## Routing
56
+
57
+ Like in ``/routes.rb``, you have to route manually the api.
58
+
59
+ ```ruby
60
+ get url: "/", controller: "MainController", method: "root"
61
+ get url: "/add", controller: "MainController", method: "add_url"
62
+ get url: "/rm", controller: "MainController", method: "rm_url"
63
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #encoding: utf-8
2
+
3
+ task :default => [:test]
4
+
5
+ task :test do
6
+ ruby "test/test.rb"
7
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ def generate_controller(name, file)
4
+ f = File.open(file, 'w')
5
+ f << <<EOF
6
+ class #{name} < Nephos::Controller
7
+ def root
8
+ return {plain: "index"}
9
+ end
10
+ end
11
+ EOF
12
+ end
13
+
14
+ case ARGV[0]
15
+ when "c", "controller"
16
+ if not ARGV[1].to_s.empty?
17
+ generate_controller("#{ARGV[1]}Controller", "src/#{ARGV[1]}.rb")
18
+ end
19
+ else
20
+ puts "help: generate c[ontroller] name"
21
+ end
data/bin/nephos-server ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # require_relative "../lib/nephos-server"
4
+ require 'nephos-server'
5
+
6
+ require 'optparse'
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: main.rb [update] [-t n] [-s n] [-i id]"
10
+
11
+ $server_port = ENV["SERVER_PORT"] || 8080
12
+ opts.on("-p=nb", "--port=nb", "Port") do |port|
13
+ $server_port = Integer(port)
14
+ end
15
+ end.parse!
16
+
17
+ Nephos::Server.start($server_port)
@@ -0,0 +1,16 @@
1
+ module Nephos
2
+ class Controller
3
+
4
+ attr_reader :env, :infos
5
+
6
+ def initialize env, parsed
7
+ @env= env
8
+ @infos= parsed
9
+ end
10
+
11
+ def arguments
12
+ @infos[:args]
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ require_relative 'controller'
2
+
3
+ Dir[File.expand_path "src/*.rb"].each do |f|
4
+ load(f) and puts "#{f} loaded"
5
+ end
6
+ puts
7
+
8
+ module Nephos
9
+ module Route
10
+
11
+ ALL = []
12
+
13
+ def self.render arg
14
+ Responder.render arg
15
+ end
16
+
17
+ def self.parse_path path
18
+ route = "/" + path.join("/")
19
+ return ALL.find{|e| e[:url] == route}
20
+ end
21
+
22
+ def self.execute(env)
23
+ route = URI.parse(env['REQUEST_URI']) rescue (puts "uri err"; return render(status: 500))
24
+ verb = env["REQUEST_METHOD"]
25
+ from = env["REMOTE_ADDR"]
26
+ path = route.path.split("/").select{|e|not e.to_s.empty?}
27
+ args = Hash[route.query.to_s.split("&").map{|e| e.split("=")}]
28
+ puts "#{from} [#{verb}] \t ---> \t #{route}"
29
+ parsed = {route: route, verb: verb, from: from, path: path, args: args}
30
+ call = parse_path(path)
31
+ return render status: 404 if call.nil?
32
+ call = parse_path(path)
33
+ begin
34
+ controller = Module.const_get(call[:controller]).new(env, parsed)
35
+ return render(controller.send(call[:method]) || {status: 500})
36
+ rescue => err
37
+ # require 'pry'; binding.pry
38
+ return render(status: 500)
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ require_relative 'load'
@@ -0,0 +1,18 @@
1
+ class RoutingError < Exception; end
2
+ class InvalidRoute < RoutingError; end
3
+ class InvalidGetRoute < InvalidRoute; end
4
+
5
+ # @param: what [Hash]
6
+ def get what
7
+ raise InvalidGetRoute if not what.keys.include? :url
8
+ raise InvalidGetRoute if not what.keys.include? :controller
9
+ raise InvalidGetRoute if not what.keys.include? :method
10
+
11
+ # TODO: more check to do
12
+
13
+ Nephos::Route::ALL << what
14
+ puts "get route: #{what}"
15
+ end
16
+
17
+ # require_relative '../../../routes.rb'
18
+ load 'routes.rb'
@@ -0,0 +1,29 @@
1
+ #coding: utf-8
2
+
3
+ # require 'json'
4
+ # require 'yaml'
5
+ require 'open-uri'
6
+ require 'rack'
7
+
8
+ require_relative 'responder'
9
+ require_relative '../routing/execute'
10
+
11
+ module Nephos
12
+ class Server
13
+
14
+ SERVER = lambda {|env| return Route.execute(env)}
15
+
16
+ def initialize port
17
+ @port = port
18
+ end
19
+
20
+ def start
21
+ Rack::Server.start :app => SERVER, :Port => @port
22
+ end
23
+
24
+ def self.start port
25
+ Server.new(port).start
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+
3
+ module Nephos
4
+ module Responder
5
+
6
+ CT_CHARSET_ = '; charset=UTF-8'
7
+ CT_TP = {'Content-type' => 'text/plain' + CT_CHARSET_}
8
+ CT_TJ = {'Content-type' => 'text/javascript' + CT_CHARSET_}
9
+ CT_TH = {'Content-type' => 'text/html' + CT_CHARSET_}
10
+ def self.render params
11
+ if params == :empty
12
+ return [204, CT_TP, [""]]
13
+ elsif params[:status] == 404
14
+ return [404, CT_TP, ['Error 404 : Not found !']]
15
+ elsif params[:status] == 500
16
+ return [500, CT_TP, ['Error 5OO : Internal Server Error !']]
17
+ elsif params[:status].is_a? Fixnum
18
+ return [params[:status], CT_TP, ["Error #{params[:status]}"]]
19
+ elsif params[:json]
20
+ return [200, CT_TJ, [params[:json].to_json]]
21
+ elsif params[:plain]
22
+ return [200, CT_TJ, [params[:plain].to_s]]
23
+ else
24
+ render(:empty)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ require_relative 'nephos-server/server/main'
@@ -0,0 +1,39 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'nephos-server'
3
+ s.version = File.read("version")
4
+ s.date = Time.now.getgm.to_s.split.first
5
+ s.summary = 'See Changelog'
6
+ s.description = ' a minimalist server, based on rack/puma, with routing, rendering, and controllers. Designed for quick api.'
7
+ s.authors = [
8
+ 'poulet_a'
9
+ ]
10
+ s.email = 'poulet_a@epitech.eu',
11
+ s.files = [
12
+ 'lib/nephos-server.rb',
13
+ 'lib/nephos-server/server/main.rb',
14
+ 'lib/nephos-server/server/responder.rb',
15
+ 'lib/nephos-server/routing/execute.rb',
16
+ 'lib/nephos-server/routing/load.rb',
17
+ 'lib/nephos-server/routing/controller.rb',
18
+ 'routes.rb.example',
19
+ 'README.md',
20
+ 'Rakefile',
21
+ 'Procfile',
22
+ 'Gemfile',
23
+ 'Gemfile.lock',
24
+ 'nephos-server.gemspec',
25
+ 'version',
26
+ 'test/test.rb',
27
+ 'bin/nephos-server',
28
+ 'bin/nephos-generator'
29
+ ]
30
+ s.executables = ['nephos-server', 'nephos-generator']
31
+ s.homepage = 'https://github.com/pouleta/NephosRubyServer'
32
+ s.license = 'GNU/GPLv3'
33
+ #s.cert_chain = ['certs/poulet_a.pem']
34
+ #s.signing_key = File.expand_path('~/.ssh/gem-private_key.pem') if $0 =~ /gem\z/
35
+
36
+ s.add_dependency 'nomorebeer', '~> 1.0'
37
+ s.add_dependency 'rack', '~> 1.6'
38
+
39
+ end
data/routes.rb.example ADDED
@@ -0,0 +1,3 @@
1
+ get url: "/", controller: "MainController", method: "root"
2
+ get url: "/add", controller: "MainController", method: "add_url"
3
+ get url: "/rm", controller: "MainController", method: "rm_url"
data/test/test.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "test/unit"
2
+ require_relative "../lib/nephos-server"
3
+
4
+ class TestNephosServer < Test::Unit::TestCase
5
+ end
data/version ADDED
@@ -0,0 +1 @@
1
+ 0.1
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nephos-server
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - poulet_a
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nomorebeer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ description: " a minimalist server, based on rack/puma, with routing, rendering, and
42
+ controllers. Designed for quick api."
43
+ email:
44
+ - poulet_a@epitech.eu
45
+ - - lib/nephos-server.rb
46
+ - lib/nephos-server/server/main.rb
47
+ - lib/nephos-server/server/responder.rb
48
+ - lib/nephos-server/routing/execute.rb
49
+ - lib/nephos-server/routing/load.rb
50
+ - lib/nephos-server/routing/controller.rb
51
+ - routes.rb.example
52
+ - README.md
53
+ - Rakefile
54
+ - Procfile
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - nephos-server.gemspec
58
+ - version
59
+ - test/test.rb
60
+ - bin/nephos-server
61
+ - bin/nephos-generator
62
+ executables:
63
+ - nephos-server
64
+ - nephos-generator
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - Procfile
71
+ - README.md
72
+ - Rakefile
73
+ - bin/nephos-generator
74
+ - bin/nephos-server
75
+ - lib/nephos-server.rb
76
+ - lib/nephos-server/routing/controller.rb
77
+ - lib/nephos-server/routing/execute.rb
78
+ - lib/nephos-server/routing/load.rb
79
+ - lib/nephos-server/server/main.rb
80
+ - lib/nephos-server/server/responder.rb
81
+ - nephos-server.gemspec
82
+ - routes.rb.example
83
+ - test/test.rb
84
+ - version
85
+ homepage: https://github.com/pouleta/NephosRubyServer
86
+ licenses:
87
+ - GNU/GPLv3
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.8
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: See Changelog
109
+ test_files: []