Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
heroku-buildpack-static
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Python-Dev
heroku-buildpack-static
Commits
7b295ee2
Commit
7b295ee2
authored
Jun 25, 2015
by
Terence Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
separate NginxConfig + regex into a separate file
parent
3ebad8d8
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
72 deletions
+72
-72
nginx_config.rb
scripts/config/lib/nginx_config.rb
+30
-0
nginx_config_util.rb
scripts/config/lib/nginx_config_util.rb
+19
-0
make-config
scripts/config/make-config
+4
-54
glob_to_regex_spec.rb
spec/glob_to_regex_spec.rb
+0
-18
nginx_config_util_spec.rb
spec/scripts/config/lib/nginx_config_util_spec.rb
+19
-0
No files found.
scripts/config/lib/nginx_config.rb
0 → 100644
View file @
7b295ee2
require
'json'
require_relative
'nginx_config_util'
class
NginxConfig
def
initialize
(
json_file
)
json
=
{}
json
=
JSON
.
parse
(
File
.
read
(
json_file
))
if
File
.
exist?
(
json_file
)
json
[
"worker_connections"
]
||=
ENV
[
"WORKER_CONNECTIONS"
]
||
512
json
[
"port"
]
||=
ENV
[
"PORT"
]
||
5000
json
[
"root"
]
||=
"public_html/"
json
[
"proxies"
]
||=
{}
json
[
"proxies"
].
each
do
|
loc
,
hash
|
if
hash
[
"origin"
][
-
1
]
!=
"/"
json
[
"proxies"
][
loc
].
merge!
(
"origin"
=>
hash
[
"origin"
]
+
"/"
)
end
end
json
[
"clean_urls"
]
||=
false
json
[
"routes"
]
||=
{}
json
[
"routes"
]
=
Hash
[
json
[
"routes"
].
map
{
|
route
,
target
|
[
NginxConfigUtil
.
to_regex
(
route
),
target
]
}]
json
[
"redirects"
]
||=
{}
json
[
"error_page"
]
||=
nil
json
.
each
do
|
key
,
value
|
self
.
class
.
send
(
:define_method
,
key
)
{
value
}
end
end
def
context
binding
end
end
scripts/config/lib/nginx_config_util.rb
0 → 100644
View file @
7b295ee2
module
NginxConfigUtil
def
self
.
to_regex
(
path
)
segments
=
[]
while
!
path
.
empty?
if
path
[
0
...
2
]
==
'**'
segments
<<
'.*'
path
=
path
[
2
..-
1
]
elsif
path
[
0
...
1
]
==
'*'
segments
<<
'[^/]*'
path
=
path
[
1
..-
1
]
else
next_star
=
path
.
index
(
"*"
)
||
path
.
length
segments
<<
Regexp
.
escape
(
path
[
0
...
next_star
])
path
=
path
[
next_star
..-
1
]
end
end
segments
.
join
end
end
scripts/config/make-config
View file @
7b295ee2
#!/usr/bin/env ruby
#!/usr/bin/env ruby
require
'fileutils'
require
'fileutils'
require
'erb'
require_relative
'lib/nginx_config'
TEMPLATE
=
File
.
join
(
File
.
dirname
(
__FILE__
),
'templates/nginx.conf.erb'
)
TEMPLATE
=
File
.
join
(
File
.
dirname
(
__FILE__
),
'templates/nginx.conf.erb'
)
USER_CONFIG
=
'static.json'
USER_CONFIG
=
'static.json'
NGINX_CONFIG
=
'config/nginx.conf'
NGINX_CONFIG
=
'config/nginx.conf'
require
'erb'
erb
=
ERB
.
new
(
File
.
read
(
TEMPLATE
)).
result
(
NginxConfig
.
new
(
USER_CONFIG
).
context
)
require
'json'
File
.
write
(
NGINX_CONFIG
,
erb
)
class
NginxConfig
def
initialize
(
json_file
)
json
=
{}
json
=
JSON
.
parse
(
File
.
read
(
json_file
))
if
File
.
exist?
(
json_file
)
json
[
"worker_connections"
]
||=
ENV
[
"WORKER_CONNECTIONS"
]
||
512
json
[
"port"
]
||=
ENV
[
"PORT"
]
||
5000
json
[
"root"
]
||=
"public_html/"
json
[
"proxies"
]
||=
{}
json
[
"proxies"
].
each
do
|
loc
,
hash
|
if
hash
[
"origin"
][
-
1
]
!=
"/"
json
[
"proxies"
][
loc
].
merge!
(
"origin"
=>
hash
[
"origin"
]
+
"/"
)
end
end
json
[
"clean_urls"
]
||=
false
json
[
"routes"
]
||=
{}
json
[
"routes"
]
=
Hash
[
json
[
"routes"
].
map
{
|
route
,
target
|
[
self
.
class
.
to_regex
(
route
),
target
]
}]
json
[
"redirects"
]
||=
{}
json
[
"error_page"
]
||=
nil
json
.
each
do
|
key
,
value
|
self
.
class
.
send
(
:define_method
,
key
)
{
value
}
end
end
def
context
binding
end
def
self
.
to_regex
(
path
)
segments
=
[]
while
!
path
.
empty?
if
path
[
0
...
2
]
==
'**'
segments
<<
'.*'
path
=
path
[
2
..-
1
]
elsif
path
[
0
...
1
]
==
'*'
segments
<<
'[^/]*'
path
=
path
[
1
..-
1
]
else
next_star
=
path
.
index
(
"*"
)
||
path
.
length
segments
<<
Regexp
.
escape
(
path
[
0
...
next_star
])
path
=
path
[
next_star
..-
1
]
end
end
segments
.
join
end
end
if
__FILE__
==
$0
erb
=
ERB
.
new
(
File
.
read
(
TEMPLATE
)).
result
(
NginxConfig
.
new
(
USER_CONFIG
).
context
)
File
.
write
(
NGINX_CONFIG
,
erb
)
end
spec/glob_to_regex_spec.rb
deleted
100644 → 0
View file @
3ebad8d8
require_relative
"spec_helper"
load
File
.
join
(
File
.
dirname
(
__FILE__
),
'../scripts/config/make-config'
)
RSpec
.
describe
"NginxConfig#to_regex"
do
samples
=
[
[
'/foo/'
,
'/foo/'
],
[
'/foo/*'
,
'/foo/[^/]*'
],
[
'/foo/**'
,
'/foo/.*'
],
[
'/cache/*'
,
'/cache/[^/]*'
],
]
samples
.
each
do
|
(
input
,
output
)
|
it
"converts
#{
input
}
to
#{
output
}
"
do
result
=
NginxConfig
.
to_regex
(
input
)
expect
(
result
).
to
eq
output
end
end
end
spec/scripts/config/lib/nginx_config_util_spec.rb
0 → 100644
View file @
7b295ee2
require_relative
"../../../spec_helper"
require_relative
"../../../../scripts/config/lib/nginx_config_util"
RSpec
.
describe
NginxConfigUtil
do
describe
".to_regex"
do
samples
=
[
[
'/foo/'
,
'/foo/'
],
[
'/foo/*'
,
'/foo/[^/]*'
],
[
'/foo/**'
,
'/foo/.*'
]
]
samples
.
each
do
|
(
input
,
output
)
|
it
"converts
#{
input
}
to
#{
output
}
"
do
result
=
NginxConfigUtil
.
to_regex
(
input
)
expect
(
result
).
to
eq
output
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment