Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nginx-redirector
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
eterfund
nginx-redirector
Commits
d2d01ce9
Commit
d2d01ce9
authored
Aug 14, 2019
by
Никита Ефремов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated Generators, parser code options and Redirector\'s use of Generators
parent
1462c067
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
30 deletions
+51
-30
generators.py
dev/generators.py
+35
-21
parser.py
dev/parser.py
+16
-7
redirector.py
dev/redirector.py
+0
-2
No files found.
dev/generators.py
View file @
d2d01ce9
...
@@ -6,13 +6,15 @@ class Generator:
...
@@ -6,13 +6,15 @@ class Generator:
self
.
conf_gen
=
ConfigGenerator
()
self
.
conf_gen
=
ConfigGenerator
()
def
generate
(
self
,
redirects_data
,
project_name
):
def
generate
(
self
,
redirects_data
,
project_name
):
redirects_map
,
options_map
=
self
.
map_gen
.
generate_map
(
redirects_data
,
project_name
)
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
project_name
)
conf_data
=
self
.
conf_gen
.
generate_conf
(
project_name
)
redirects_with_options
=
self
.
map_gen
.
generate_opt_map
(
redirects_data
[
1
],
project_name
)
conf_data
=
self
.
conf_gen
.
generate_config
(
project_name
,
[
code
for
code
,
data
in
redirects_with_options
])
try
:
try
:
with
open
(
const
.
MAPS_DIR
+
"/
%
s.map"
%
project_name
,
"w"
)
as
map_file
:
with
open
(
const
.
MAPS_DIR
+
"/
%
s.map"
%
project_name
,
"w"
)
as
map_file
:
map_file
.
write
(
redirects_map
)
map_file
.
write
(
redirects_map
)
with
open
(
const
.
MAPS_DIR
+
"/
%
s_options.map"
%
project_name
,
"w"
)
as
map_file
:
for
code
,
data
in
redirects_with_options
:
map_file
.
write
(
options_map
)
with
open
(
const
.
MAPS_DIR
+
"/
%
s_
%
s_options.map"
%
(
project_name
,
code
),
"w"
)
as
map_file
:
map_file
.
write
(
data
)
with
open
(
const
.
CONFIG_DIR
+
"/
%
s.conf"
%
project_name
,
"w"
)
as
conf_file
:
with
open
(
const
.
CONFIG_DIR
+
"/
%
s.conf"
%
project_name
,
"w"
)
as
conf_file
:
conf_file
.
write
(
conf_data
)
conf_file
.
write
(
conf_data
)
except
Exception
as
e
:
except
Exception
as
e
:
...
@@ -29,30 +31,42 @@ class Generator:
...
@@ -29,30 +31,42 @@ class Generator:
class
MapGenerator
:
class
MapGenerator
:
def
__init__
(
self
):
def
__init__
(
self
):
self
.
start_line
=
[
"map $uri $
%
s_redirect {"
,
"map $uri $
%
s_redirect_option {"
]
self
.
start_line
=
"map $uri $
%
s_redirect {"
self
.
status_codes
=
{
'301'
:
"redirect"
}
self
.
opt_start_line
=
"map $uri $
%
s_
%
s_redirect {"
# (project_name, redirect_option)
self
.
endline
=
"
\n
}"
self
.
endline
=
"
\n
}"
def
generate_map
(
self
,
redirects_sorted
,
project_name
):
def
generate_map
(
self
,
redirects
,
project_name
):
data
=
self
.
start_line
[
0
]
%
project_name
data
=
self
.
start_line
%
project_name
data2
=
self
.
start_line
[
1
]
%
project_name
for
arg1
,
arg2
in
redirects
:
for
arg1
,
arg2
in
redirects_sorted
[
0
]:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
data
+=
self
.
endline
return
data
def
generate_opt_map
(
self
,
redirect_dict
,
project_name
):
result
=
[]
for
code
,
redirects
in
redirect_dict
.
items
():
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
data2
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
"redirect"
)
data
+=
self
.
endline
for
i
,
item
in
enumerate
(
redirects_sorted
[
1
]):
result
.
append
((
code
,
data
))
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
item
[
0
],
item
[
1
])
return
result
data2
+=
"
\n\t
%
s
\t
%
s;"
%
(
item
[
0
],
self
.
status_codes
[
redirects_sorted
[
2
][
i
][
1
][
0
][
0
]])
data
+=
self
.
endline
+
"
\n
"
data2
+=
self
.
endline
return
data
,
data2
class
ConfigGenerator
:
class
ConfigGenerator
:
def
__init__
(
self
):
def
__init__
(
self
):
self
.
start_line
=
"if ($
%
s_redirect) {
\n
"
self
.
start_line
=
"if ($
%
s_redirect) {
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^/
%
s/(.*)$ $
%
s_redirect $
%
s_redirect_option;
\n
}
"
self
.
opt_start_line
=
"if ($
%
s_
%
s_redirect) {
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^/
%
s/(.*)$ $
%
s_redirect
%
s;
\n
"
def
generate_conf
(
self
,
project_name
):
self
.
opt_rewrite_line
=
"
\t
rewrite ^/
%
s/(.*)$ $
%
s_
%
s_redirect
%
s;
\n
"
return
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
,
project_name
))
self
.
option_line
=
"return
%
s;
\n
}
\n
"
def
generate_config
(
self
,
project_name
,
options
):
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
,
"break"
))
+
"}
\n
"
for
code
in
options
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
code
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
code
,
"break"
))
+
(
self
.
option_line
%
code
)
return
data
dev/parser.py
View file @
d2d01ce9
...
@@ -17,6 +17,8 @@ class MapLineParser:
...
@@ -17,6 +17,8 @@ class MapLineParser:
def
parse_line
(
self
,
line
,
prefix
,
i
):
def
parse_line
(
self
,
line
,
prefix
,
i
):
line
=
line
.
strip
()
line
=
line
.
strip
()
return_code
,
options
=
0
,
[]
return_code
,
options
=
0
,
[]
import
pdb
pdb
.
set_trace
()
if
line
.
startswith
(
"#"
)
or
len
(
line
)
==
0
:
if
line
.
startswith
(
"#"
)
or
len
(
line
)
==
0
:
return
-
1
,
None
,
None
return
-
1
,
None
,
None
...
@@ -41,11 +43,14 @@ class MapLineParser:
...
@@ -41,11 +43,14 @@ class MapLineParser:
elif
line
[
0
]
.
startswith
(
"//"
):
# if new URI relative to the root
elif
line
[
0
]
.
startswith
(
"//"
):
# if new URI relative to the root
line
[
0
]
=
line
[
0
][
1
:]
# cutting out extra '/' at the beginning
line
[
0
]
=
line
[
0
][
1
:]
# cutting out extra '/' at the beginning
line
[
1
]
=
prefix
+
line
[
1
]
elif
line
[
1
]
.
startswith
(
"//"
):
elif
line
[
1
]
.
startswith
(
"//"
):
line
[
1
]
=
line
[
1
][
1
:]
line
[
1
]
=
line
[
1
][
1
:]
line
[
0
]
=
prefix
+
line
[
0
]
elif
self
.
url_absolute
.
fullmatch
(
line
[
1
]):
elif
self
.
url_absolute
.
fullmatch
(
line
[
1
]):
pass
line
[
0
]
=
prefix
+
line
[
0
]
else
:
# default url
else
:
# default url
line
[
0
]
=
prefix
+
line
[
0
]
line
[
0
]
=
prefix
+
line
[
0
]
...
@@ -96,17 +101,17 @@ class ConfigReader:
...
@@ -96,17 +101,17 @@ class ConfigReader:
return
return_list
return
return_list
def
parse_map
(
self
,
map_file
,
yaml_file
):
def
parse_map
(
self
,
map_file
,
yaml_file
):
res
=
[[],
[],
[]
]
res
=
[[],
{}
]
res_prefix
=
None
res_prefix
=
None
try
:
try
:
for
map_path
,
prefix
in
self
.
parse_yaml
(
yaml_file
):
for
map_path
,
prefix
in
self
.
parse_yaml
(
yaml_file
):
if
map_path
==
map_file
or
os
.
getcwd
()
+
"/"
+
map_path
==
map_file
:
if
map_path
==
map_file
or
os
.
getcwd
()
+
"/"
+
map_path
==
map_file
:
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
#
???: Is the last directory of map_path a project's name?
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
#
Last directory of map_path a project's name
with
open
(
map_file
,
"r"
)
as
file
:
with
open
(
map_file
,
"r"
)
as
file
:
for
i
,
line
in
enumerate
(
file
):
for
i
,
line
in
enumerate
(
file
):
request_url
,
redirect_url
,
option
s
=
None
,
None
,
[]
request_url
,
redirect_url
,
option
=
None
,
None
,
[]
try
:
try
:
return_code
,
request_url
,
redirect_url
,
*
option
s
=
\
return_code
,
request_url
,
redirect_url
,
*
option
=
\
self
.
line_parser
.
parse_line
(
line
,
prefix
,
i
)
self
.
line_parser
.
parse_line
(
line
,
prefix
,
i
)
except
MapLineParser
.
RegexpTestError
as
e
:
except
MapLineParser
.
RegexpTestError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_file
)
self
.
logger
.
log
(
e
.
message
%
map_file
)
...
@@ -119,8 +124,12 @@ class ConfigReader:
...
@@ -119,8 +124,12 @@ class ConfigReader:
if
return_code
==
0
:
if
return_code
==
0
:
res
[
0
]
.
append
((
request_url
,
redirect_url
))
res
[
0
]
.
append
((
request_url
,
redirect_url
))
elif
return_code
==
1
:
elif
return_code
==
1
:
res
[
1
]
.
append
((
request_url
,
redirect_url
))
opt_
=
option
[
0
][
0
]
if
option
else
option
res
[
2
]
.
append
((
request_url
,
options
))
if
opt_
in
res
[
1
]
.
keys
():
res
[
1
][
opt_
]
.
append
[(
request_url
,
redirect_url
)]
else
:
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
return
res
,
res_prefix
return
res
,
res_prefix
except
YAMLError
as
e
:
except
YAMLError
as
e
:
self
.
logger
.
log
(
"Error occurred while reading
%
s"
%
yaml_file
+
str
(
e
))
self
.
logger
.
log
(
"Error occurred while reading
%
s"
%
yaml_file
+
str
(
e
))
...
...
dev/redirector.py
View file @
d2d01ce9
...
@@ -17,8 +17,6 @@ class Redirector:
...
@@ -17,8 +17,6 @@ class Redirector:
self
.
generator
=
generators
.
Generator
()
self
.
generator
=
generators
.
Generator
()
def
generate
(
self
,
yaml_file
,
map_file
):
def
generate
(
self
,
yaml_file
,
map_file
):
import
pdb
pdb
.
set_trace
()
project_name
=
"Error"
project_name
=
"Error"
try
:
try
:
data
,
project_name
=
self
.
parser
.
parse_map
(
map_file
,
yaml_file
)
data
,
project_name
=
self
.
parser
.
parse_map
(
map_file
,
yaml_file
)
...
...
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