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
1462c067
Commit
1462c067
authored
Jul 17, 2019
by
Никита Ефремов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remade and debugged .conf and .map files generation
parent
a62218c4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
36 deletions
+46
-36
generators.py
dev/generators.py
+41
-19
redirector.py
dev/redirector.py
+5
-17
No files found.
dev/generators.py
View file @
1462c067
from
dev
import
const
class
Generator
:
def
__init__
(
self
):
self
.
map_gen
=
MapGenerator
()
self
.
conf_gen
=
ConfigGenerator
()
def
generate
(
self
,
redirects_data
,
project_name
):
redirects_map
,
options_map
=
self
.
map_gen
.
generate_map
(
redirects_data
,
project_name
)
conf_data
=
self
.
conf_gen
.
generate_conf
(
project_name
)
try
:
with
open
(
const
.
MAPS_DIR
+
"/
%
s.map"
%
project_name
,
"w"
)
as
map_file
:
map_file
.
write
(
redirects_map
)
with
open
(
const
.
MAPS_DIR
+
"/
%
s_options.map"
%
project_name
,
"w"
)
as
map_file
:
map_file
.
write
(
options_map
)
with
open
(
const
.
CONFIG_DIR
+
"/
%
s.conf"
%
project_name
,
"w"
)
as
conf_file
:
conf_file
.
write
(
conf_data
)
except
Exception
as
e
:
print
(
e
)
raise
self
.
GenerationError
(
"Can
\'
t generate new .conf or new .map file
\
from
%
s .map file for
%
s project"
%
(
map_file
,
project_name
),
e
)
class
GenerationError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
self
.
errors
=
errors
class
MapGenerator
:
class
MapGenerator
:
def
__init__
(
self
):
def
__init__
(
self
):
self
.
start_line
=
"map $uri $
%
s_redirect {"
self
.
start_line
=
[
"map $uri $
%
s_redirect {"
,
"map $uri $
%
s_redirect_option {"
]
self
.
codes_dict
=
{
'301'
:
"permanent"
,
'302'
:
'redirect'
}
self
.
status_codes
=
{
'301'
:
"redirect"
}
self
.
endline
=
"
\n
}"
self
.
endline
=
"
\n
}"
def
generate_map
(
self
,
redirects_sorted
,
project_name
):
def
generate_map
(
self
,
redirects_sorted
,
project_name
):
data
=
self
.
start_line
%
project_name
data
=
self
.
start_line
[
0
]
%
project_name
data2
=
self
.
start_line
[
1
]
%
project_name
for
arg1
,
arg2
in
redirects_sorted
[
0
]:
for
arg1
,
arg2
in
redirects_sorted
[
0
]:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
for
arg1
,
arg2
in
redirects_sorted
[
1
]:
data2
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
"redirect"
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
for
i
,
item
in
enumerate
(
redirects_sorted
[
1
]):
"""
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
item
[
0
],
item
[
1
])
for i, args in enumerate(redirects_sorted[1]):
data2
+=
"
\n\t
%
s
\t
%
s;"
%
(
item
[
0
],
self
.
status_codes
[
redirects_sorted
[
2
][
i
][
1
][
0
][
0
]])
try:
data
+=
self
.
endline
+
"
\n
"
key_ = redirects_sorted[2][i]
data2
+=
self
.
endline
except KeyError:
return
data
,
data2
key_ = None
data = "
\n\t
%
s
\t
%
s;"
%
(args[0], args[1]) if not key_
else "
\n\t
%
s
\t
%
s
\t
%
s;"
%
(args[0], args[1], key_)
"""
data
+=
self
.
endline
return
data
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 redirect
;
\n
}"
self
.
rewrite_line
=
"
\t
rewrite ^/
%
s/(.
*)$ $
%
s_redirect $
%
s_redirect_option
;
\n
}"
def
generate_conf
(
self
,
project_name
):
def
generate_conf
(
self
,
project_name
):
return
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
))
return
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
,
project_name
))
dev/redirector.py
View file @
1462c067
...
@@ -14,10 +14,11 @@ redirector_watch = None
...
@@ -14,10 +14,11 @@ redirector_watch = None
class
Redirector
:
class
Redirector
:
def
__init__
(
self
,
logger
=
None
):
def
__init__
(
self
,
logger
=
None
):
self
.
parser
=
parser
.
ConfigReader
(
logger
=
logger
)
self
.
parser
=
parser
.
ConfigReader
(
logger
=
logger
)
self
.
map_generator
=
generators
.
MapGenerator
()
self
.
generator
=
generators
.
Generator
()
self
.
config_generator
=
generators
.
ConfigGenerator
()
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
)
...
@@ -26,26 +27,13 @@ class Redirector:
...
@@ -26,26 +27,13 @@ class Redirector:
# FIXME: what is the better way to do so?
# FIXME: what is the better way to do so?
except
Exception
as
e
:
except
Exception
as
e
:
raise
self
.
RedirectorParserError
(
"Can
\'
t parse .map file
%
s"
%
map_file
,
e
)
raise
self
.
RedirectorParserError
(
"Can
\'
t parse .map file
%
s"
%
map_file
,
e
)
try
:
self
.
generator
.
generate
(
data
,
project_name
)
with
open
(
const
.
MAPS_DIR
+
"/
%
s.map"
%
project_name
,
"w"
)
as
map_file
:
map_file
.
write
(
self
.
map_generator
.
generate_map
(
data
,
project_name
))
with
open
(
const
.
CONFIG_DIR
+
"/
%
s.conf"
%
project_name
,
"w"
)
as
conf_file
:
conf_file
.
write
(
self
.
config_generator
.
generate_conf
(
project_name
))
except
Exception
as
e
:
print
(
e
)
raise
self
.
RedirectorGenerationError
(
"Can
\'
t generate new .conf or new .map file
\
from
%
s .map file for
%
s project"
%
(
map_file
,
project_name
),
e
)
class
RedirectorParserError
(
Exception
):
class
RedirectorParserError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
super
()
.
__init__
(
message
)
self
.
errors
=
errors
self
.
errors
=
errors
class
RedirectorGenerationError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
self
.
errors
=
errors
class
RedirectorInputDataError
(
Exception
):
class
RedirectorInputDataError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
super
()
.
__init__
(
message
)
...
@@ -121,7 +109,7 @@ def main(args=None):
...
@@ -121,7 +109,7 @@ def main(args=None):
redirector
=
Redirector
()
redirector
=
Redirector
()
try
:
try
:
redirector
.
generate
(
args
.
yaml_file
[
0
],
args
.
map_file
[
0
])
redirector
.
generate
(
args
.
yaml_file
[
0
],
args
.
map_file
[
0
])
except
(
Redirector
.
Redirector
GenerationError
,
redirector
.
RedirectorParserError
)
as
e
:
except
(
generators
.
Generator
.
GenerationError
,
redirector
.
RedirectorParserError
)
as
e
:
print
(
"CRITICAL:
\n
"
+
str
(
e
))
print
(
"CRITICAL:
\n
"
+
str
(
e
))
...
...
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