nginx-server 0.0.0 → 0.0.1

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: 98107dc1a5a370fcc90168e74eb0408fcf9ac84c06161dd04ccbb7e665fc049d
4
- data.tar.gz: cb2ac11c851173ef82d2368f4d49d64b6318bb05b904a8eb07e193d253551e86
3
+ metadata.gz: 2f822a7a5a6abd79550514220b4439c29ed656b1867dc66fb4d6e87acd89c9e4
4
+ data.tar.gz: 6a25fda0ea854b89d13dc54b1f1d770845b5170a19ad844aef13f706745ca14c
5
5
  SHA512:
6
- metadata.gz: 7e4cd3edb4f0044508d37b9029b47ef6f8c5daf4c74f68decab57209e54568bcdf0af312de91ae424ca541990582671fe7d5d25e853dd11c9ec2e7e74c6413cf
7
- data.tar.gz: 73460b49c80da4fc4cd7949102c575add1e7e5f21aba46c68a9556e860b18c49a1bd15dceae487a653a87c3458b180ba96a9d8d0f46b33c13b8f7ca7e7b9c956
6
+ metadata.gz: 61ae824f946d7d0ff8ad3bcafde5e430bf3e7fade230b226767468218e4bf2993f3b1c11557950af13e549bbabec5847c66c5d892e2167075a07a12bf1f20f33
7
+ data.tar.gz: 249c00bfbf54487af5bc59212044d70fec7fd069833485f521df8067cdbb732e3fcc1ee504e1a4b8430211cd7601f21a42df7c153b429eefaa6531822352f017
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'hobby'
4
+
3
5
  gemspec
@@ -2,17 +2,23 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  nginx-server (0.0.0)
5
+ puma
5
6
  suppress_output
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
11
+ hobby (0.1.2)
12
+ rack
13
+ puma (3.11.4)
14
+ rack (2.0.5)
10
15
  suppress_output (0.0.1)
11
16
 
12
17
  PLATFORMS
13
18
  ruby
14
19
 
15
20
  DEPENDENCIES
21
+ hobby
16
22
  nginx-server!
17
23
 
18
24
  BUNDLED WITH
@@ -3,7 +3,7 @@ events {
3
3
  }
4
4
 
5
5
  error_log <%= error_log %>;
6
- pid <%= pid %>;
6
+ pid <%= pidfile %>;
7
7
 
8
8
  http {
9
9
  access_log <%= access_log %>;
@@ -0,0 +1,18 @@
1
+ events {
2
+ worker_connections 1024;
3
+ }
4
+
5
+ error_log <%= error_log %>;
6
+ pid <%= pidfile %>;
7
+
8
+ http {
9
+ access_log <%= access_log %>;
10
+
11
+ <%= upstreams %>
12
+
13
+ server {
14
+ listen <%= address %>;
15
+
16
+ <%= locations %>
17
+ }
18
+ }
@@ -0,0 +1,23 @@
1
+ require 'nginx/server'
2
+ require 'hobby'
3
+
4
+ class First
5
+ include Hobby
6
+ get { 'Some first.' }
7
+ end
8
+
9
+ class Second
10
+ include Hobby
11
+ get { 'Some second.' }
12
+ end
13
+
14
+ server = Nginx.proxy do
15
+ host '127.0.0.1'
16
+ port 8080
17
+
18
+ map :first, First.new
19
+ map :second, Second.new
20
+ end
21
+
22
+ server.start
23
+ sleep
@@ -0,0 +1,18 @@
1
+ events {
2
+ worker_connections 1024;
3
+ }
4
+
5
+ error_log <%= error_log %>;
6
+ pid <%= pidfile %>;
7
+
8
+ http {
9
+ access_log <%= access_log %>;
10
+
11
+ <%= upstreams %>
12
+
13
+ server {
14
+ listen <%= address %>;
15
+
16
+ <%= locations %>
17
+ }
18
+ }
@@ -2,15 +2,55 @@ require 'securerandom'
2
2
  require 'pathname'
3
3
  require 'erb'
4
4
  require 'suppress_output'
5
+ require 'puma'
5
6
 
6
7
  module Nginx
8
+ def self.proxy &block
9
+ config = Config.new block
10
+ Proxy.from_file "#{__dir__}/proxy.conf.erb", **config
11
+ end
12
+
13
+ class Config
14
+ def initialize dsl
15
+ @maps = {}
16
+ instance_exec &dsl
17
+ end
18
+
19
+ def to_hash
20
+ {
21
+ host: @host,
22
+ port: @port,
23
+ maps: @maps,
24
+ }
25
+ end
26
+
27
+ def host host
28
+ @host = host
29
+ end
30
+
31
+ def port port
32
+ @port = port
33
+ end
34
+
35
+ def map matcher, app
36
+ @maps[matcher] = app
37
+ end
38
+ end
39
+
7
40
  class Server
8
- def initialize template,
9
- dir: "/tmp/nginx.#{Process.pid}.#{SecureRandom.uuid}"
41
+ def self.from_file file, **all
42
+ new (IO.read file), **all
43
+ end
44
+
45
+ def initialize string,
46
+ dir: "/tmp/nginx.#{Process.pid}.#{SecureRandom.uuid}", **params
10
47
  @dir = Pathname dir; @dir.mkpath
11
- create_config template
48
+ set_params params
49
+ create_config string
12
50
  end
13
51
 
52
+ attr_reader :dir
53
+
14
54
  def start
15
55
  @nginx_pid = suppress_output do
16
56
  spawn "nginx -c nginx.conf -p #{@dir} -g 'daemon off;'"
@@ -22,6 +62,7 @@ module Nginx
22
62
  `kill #{@nginx_pid}`
23
63
  end
24
64
 
65
+
25
66
  def error_log
26
67
  'error.log'
27
68
  end
@@ -31,13 +72,72 @@ module Nginx
31
72
  end
32
73
 
33
74
  def pid
75
+ IO.read(pidfile).to_i
76
+ end
77
+
78
+ def pidfile
34
79
  'nginx.pid'
35
80
  end
36
81
 
82
+
37
83
  private
38
- def create_config template
39
- string = ERB.new(IO.read template).result(binding)
40
- IO.write "#{@dir}/nginx.conf", string
84
+ def set_params params
85
+ params.each do |key, value|
86
+ define_singleton_method(key) { value }
87
+ end
88
+ end
89
+
90
+ def create_config input
91
+ output = ERB.new(input).result(binding)
92
+ IO.write "#{@dir}/nginx.conf", output
93
+ end
94
+ end
95
+
96
+ class Proxy < Server
97
+ def start
98
+ maps.each do |key, app|
99
+ fork do
100
+ server = Puma::Server.new app
101
+ server.add_unix_listener socket_of key
102
+ server.run
103
+ sleep
104
+ end
41
105
  end
106
+
107
+ super
108
+ end
109
+
110
+
111
+ def socket_of key
112
+ "#{@dir}/#{key}.sock"
113
+ end
114
+
115
+ def upstreams
116
+ maps.keys.map do |key|
117
+ <<-S
118
+ upstream #{key} {
119
+ server unix:#{socket_of key};
120
+ }
121
+ S
122
+ end.join "\n\n"
123
+ end
124
+
125
+ def address
126
+ if host
127
+ "#{host}:#{port}"
128
+ else
129
+ port
130
+ end
131
+ end
132
+
133
+ def locations
134
+ maps.keys.map do |key|
135
+ <<-S
136
+ location /#{key}/ {
137
+ proxy_pass http://#{key}/;
138
+ }
139
+ S
140
+ end.join "\n\n"
141
+ end
42
142
  end
43
143
  end
@@ -1,9 +1,10 @@
1
1
  Gem::Specification.new do |g|
2
2
  g.name = 'nginx-server'
3
3
  g.files = `git ls-files`.split($/)
4
- g.version = '0.0.0'
4
+ g.version = '0.0.1'
5
5
  g.summary = 'Manage Nginx servers from Ruby.'
6
6
  g.authors = ['Anatoly Chernow']
7
7
 
8
8
  g.add_dependency 'suppress_output'
9
+ g.add_dependency 'puma'
9
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nginx-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-01 00:00:00.000000000 Z
11
+ date: 2018-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: suppress_output
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: puma
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email:
29
43
  executables: []
@@ -34,6 +48,9 @@ files:
34
48
  - Gemfile.lock
35
49
  - examples/0.rb
36
50
  - examples/nginx.conf.erb
51
+ - examples/proxy.conf.erb
52
+ - examples/proxy.rb
53
+ - lib/nginx/proxy.conf.erb
37
54
  - lib/nginx/server.rb
38
55
  - nginx-server.gemspec
39
56
  homepage: