adelnor 0.0.5 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f634fb86e2eac72d5c27144656ee18cc978787a7afdfce6dcef3f1d16f9c0f6
4
- data.tar.gz: 8a0f3fbbc2aa790adf6ffe23a3917755aac17137dd582df726d3310dadcf3215
3
+ metadata.gz: 5bbe7f5e96936b78536738d717ea5babeb525aec4ecc202774e6564095d21663
4
+ data.tar.gz: 8da6cf7523a6d8f5d520fe6eb1c8e02320b53e79f631b0e36d0401a29f093f47
5
5
  SHA512:
6
- metadata.gz: 9dba8f7beb9c87a4a85bfbc873238fdb9e5da4303a1a4a29f520762dc9e975d53bc971d867cfe41b9fd9c30aea697706bf0bb7310d6cf988f9a0c4fdcfa3a53d
7
- data.tar.gz: 49c096d7c9aefcfd4ca6f0798a7d619b36c27a7b4f2d5ddeffac2c68188365a265126b7b58ca7422679029a90d81a7c0834ab8e777fb0be471d53c2a330bab1b
6
+ metadata.gz: b46502cfb371089998df7cf3d98221224df23ebb77cfd6f368d9d3b21df8ef8244a8cde14207c8b511146011fcd6766498483288ade45f263fdb00b4c7ed3927
7
+ data.tar.gz: d7fdfbe381e3c56c4c6bfdc7109afdd10ee9bd05c7d95d5da012fe671f3b547763a3bb916e519c2228038a1a6be7c473236c5bb623662e0ff7b8f7672209349b
data/README.md CHANGED
@@ -1,18 +1,19 @@
1
- # adelnor
1
+ # adelnor
2
2
 
3
- ![rubygems](https://badgen.net/rubygems/n/adelnor)
4
- ![rubygems](https://badgen.net/rubygems/v/adelnor)
5
- ![rubygems](https://badgen.net/rubygems/dt/adelnor)
3
+ ![rubygems](https://badgen.net/rubygems/n/adelnor)
4
+ ![rubygems](https://badgen.net/rubygems/v/adelnor/latest)
5
+ ![rubygems](https://badgen.net/rubygems/dt/adelnor)
6
6
 
7
- ![Build](https://github.com/leandronsp/adelnor/actions/workflows/build.yml/badge.svg)
8
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
7
+ ![Build](https://github.com/leandronsp/adelnor/actions/workflows/build.yml/badge.svg)
8
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
9
9
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rubystyle.guide)
10
- ``` .___ .__
11
- _____ __| _/____ | | ____ ___________
12
- \__ \ / __ |/ __ \| | / \ / _ \_ __ \
13
- / __ \_/ /_/ \ ___/| |_| | ( <_> ) | \/
14
- (____ /\____ |\___ >____/___| /\____/|__|
15
- \/ \/ \/ \/
10
+ ```
11
+ ___ _______ _______ __ .__ __. ______ .______
12
+ / \ | \ | ____|| | | \ | | / __ \ | _ \
13
+ / ^ \ | .--. || |__ | | | \| | | | | | | |_) |
14
+ / /_\ \ | | | || __| | | | . ` | | | | | | /
15
+ / _____ \ | '--' || |____ | `----.| |\ | | `--' | | |\ \----.
16
+ /__/ \__\ |_______/ |_______||_______||__| \__| \______/ | _| `._____|
16
17
  ```
17
18
 
18
19
  [adelnor](https://rubygems.org/gems/adelnor) is a dead simple, yet Rack-compatible, HTTP server written in Ruby.
@@ -56,4 +57,16 @@ $ make bundle.install
56
57
  $ make sample.server
57
58
  ```
58
59
 
60
+ ## Using Thread Pool
61
+
62
+ ```ruby
63
+ require './lib/adelnor/server'
64
+
65
+ app = -> (env) do
66
+ [200, { 'Content-Type' => 'text/html' }, 'Hello world!']
67
+ end
68
+
69
+ Adelnor::Server.run app, 3000, thread_pool: 5
70
+ ```
71
+
59
72
  Open `http://localhost:3000`
data/adelnor.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'adelnor'
5
- spec.version = '0.0.5'
5
+ spec.version = '0.0.7'
6
6
  spec.summary = 'Adelnor HTTP server'
7
7
  spec.description = 'A dead simple, yet Rack-compatible, HTTP server written in Ruby'
8
8
  spec.authors = ['Leandro Proença']
@@ -11,5 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.homepage = 'https://github.com/leandronsp/adelnor'
12
12
  spec.license = 'MIT'
13
13
 
14
+ spec.add_runtime_dependency 'rack'
15
+
14
16
  spec.required_ruby_version = '~> 3.0'
15
17
  end
@@ -21,6 +21,8 @@ module Adelnor
21
21
  lines = @message.split(/\r\n/)
22
22
  headline = lines.shift
23
23
 
24
+ return self unless headline
25
+
24
26
  parse_headline!(headline)
25
27
  parse_headers!(lines)
26
28
 
@@ -5,7 +5,7 @@ module Adelnor
5
5
  def initialize(status, headers, body)
6
6
  @status = status
7
7
  @headers = headers
8
- @body = body
8
+ @body = body.respond_to?(:first) ? body.first : body
9
9
  end
10
10
 
11
11
  def self.build(*args)
@@ -13,20 +13,12 @@ module Adelnor
13
13
  end
14
14
 
15
15
  def build
16
- "HTTP/2.0 #{@status}\r\n#{headers_as_string}\r\n#{body_as_string}"
16
+ "HTTP/2.0 #{@status}\r\n#{headers_as_string}\r\n#{@body}"
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- def body_as_string = @body.respond_to?(:first) ? @body.first : @body
22
-
23
- def headers_as_string
24
- @headers.reduce('') do |acc, (key, value)|
25
- acc += header_as_string(key, value)
26
- acc
27
- end
28
- end
29
-
30
- def header_as_string(key, value) = "#{key}: #{value}\r\n"
21
+ def headers_as_string = @headers.reduce('', &method(:acc_header_string))
22
+ def acc_header_string(acc, (key, value)) = acc + "#{key}: #{value}\r\n"
31
23
  end
32
24
  end
@@ -9,15 +9,17 @@ require_relative './response'
9
9
 
10
10
  module Adelnor
11
11
  class Server
12
- def initialize(rack_app, port)
12
+ def initialize(rack_app, port, options = {})
13
13
  @rack_app = rack_app
14
14
  @port = port
15
+ @options = options
15
16
  @socket = Socket.new(:INET, :STREAM)
16
17
  addr = Socket.sockaddr_in(@port, '0.0.0.0')
17
18
 
18
19
  @socket.bind(addr)
19
20
  @socket.listen(2)
20
21
 
22
+ @thread_queue = Queue.new if @options[:thread_pool]
21
23
  puts welcome_message
22
24
  end
23
25
 
@@ -39,15 +41,44 @@ module Adelnor
39
41
  end
40
42
 
41
43
  def run
44
+ if (pool_size = @options[:thread_pool])
45
+ return run_with_thread_pool(pool_size)
46
+ end
47
+
42
48
  loop do
43
49
  client, = @socket.accept
44
50
 
45
51
  handle(client)
46
-
47
52
  client.close
48
53
  end
49
54
  end
50
55
 
56
+ def run_with_thread_pool(pool_size)
57
+ pool_size.times do
58
+ handle_request_in_thread
59
+ end
60
+
61
+ loop do
62
+ client, = @socket.accept
63
+
64
+ @thread_queue.push(client)
65
+ end
66
+ end
67
+
68
+ def handle_request_in_thread
69
+ Thread.new do
70
+ tid = Thread.current.object_id
71
+
72
+ puts "[#{tid}] Thread started"
73
+ loop do
74
+ client = @thread_queue.pop
75
+
76
+ handle(client)
77
+ client.close
78
+ end
79
+ end
80
+ end
81
+
51
82
  def handle(client)
52
83
  read_request_message(client)
53
84
  .then { |message| Request.build(message) }
@@ -61,9 +92,11 @@ module Adelnor
61
92
  def read_request_message(client)
62
93
  message = ''
63
94
 
64
- message += client.gets
65
- puts
66
- puts "[#{Time.now}] #{message}"
95
+ if (line = client.gets)
96
+ message += line
97
+ end
98
+
99
+ puts "\n[#{Time.now}] #{message}"
67
100
 
68
101
  while (line = client.gets)
69
102
  break if line == "\r\n"
@@ -75,6 +108,8 @@ module Adelnor
75
108
  end
76
109
 
77
110
  def welcome_message
111
+ thread_pool_message = "Running with thread pool of #{@options[:thread_pool]} threads" if @options[:thread_pool]
112
+
78
113
  <<~WELCOME
79
114
  |\---/|
80
115
  | o_o |
@@ -86,6 +121,8 @@ module Adelnor
86
121
 
87
122
  Adelnor is running at http://0.0.0.0:#{@port}
88
123
  Listening to HTTP connections
124
+
125
+ #{thread_pool_message}
89
126
  WELCOME
90
127
  end
91
128
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Adelnor
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.6'
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adelnor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Proença
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: A dead simple, yet Rack-compatible, HTTP server written in Ruby
14
28
  email: leandronsp@gmail.com
15
29
  executables: []
@@ -29,7 +43,7 @@ homepage: https://github.com/leandronsp/adelnor
29
43
  licenses:
30
44
  - MIT
31
45
  metadata: {}
32
- post_install_message:
46
+ post_install_message:
33
47
  rdoc_options: []
34
48
  require_paths:
35
49
  - lib
@@ -44,8 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
58
  - !ruby/object:Gem::Version
45
59
  version: '0'
46
60
  requirements: []
47
- rubygems_version: 3.3.3
48
- signing_key:
61
+ rubygems_version: 3.4.10
62
+ signing_key:
49
63
  specification_version: 4
50
64
  summary: Adelnor HTTP server
51
65
  test_files: []