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
e9910f24
Commit
e9910f24
authored
Mar 31, 2022
by
Soldatoff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove the extra slash, which works without a prefix. Fixed .conf generation without prefix
parent
24a459b0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
15 deletions
+24
-15
nginx-redirector.spec
.gear/nginx-redirector.spec
+1
-1
redirector.py
redirector/redirector.py
+2
-2
generators.py
redirector/utils/generators.py
+19
-10
parser.py
redirector/utils/parser.py
+2
-2
No files found.
.gear/nginx-redirector.spec
View file @
e9910f24
...
...
@@ -3,7 +3,7 @@
%define biname nginx-redirector
Name: nginx-redirector
Version: 0.2.0
Version: 0.2.0
.1
Release: alt1
Summary: CLI-utility for building nginx redirects
...
...
redirector/redirector.py
View file @
e9910f24
...
...
@@ -14,13 +14,13 @@ class Redirector:
project_name
=
"Error"
try
:
data
,
project_name
=
self
.
parser
.
parse_map
(
map_file
,
yaml_file
)
if
not
project_name
:
if
not
data
:
raise
self
.
RedirectorInputDataError
(
"Can
\'
t properly parse .map or .yaml files"
,
Exception
())
# FIXME: what is the better way to do so?
except
Exception
as
e
:
raise
self
.
RedirectorParserError
(
"Can
\'
t parse .map file
%
s"
%
map_file
,
e
)
self
.
generator
.
generate
(
data
,
project_name
)
self
.
generator
.
generate
(
data
,
project_name
,
map_file
)
class
RedirectorParserError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
...
...
redirector/utils/generators.py
View file @
e9910f24
from
redirector.utils.const
import
MAPS_DIR
,
CONFIG_DIR
from
redirector.utils.const
import
MAPS_DIR
,
CONFIG_DIR
,
CONFIG
from
redirector.utils.parser
import
ConfigReader
class
Generator
:
def
__init__
(
self
):
...
...
@@ -9,15 +10,20 @@ class Generator:
return
MAPS_DIR
,
CONFIG_DIR
def
generate
(
self
,
redirects_data
,
project_name
):
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
project_name
)
def
generate
(
self
,
redirects_data
,
project_name
,
arg_map
=
None
):
if
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
])
>
len
(
"/"
):
prefix
=
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
]
.
split
(
"/"
)[
-
1
]
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
"/{}"
.
format
(
project_name
))
else
:
prefix
=
arg_map
.
split
(
"/"
)[
-
2
]
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
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
])
conf_data
=
self
.
conf_gen
.
generate_config
(
project_name
,
[
code
for
code
,
data
in
redirects_with_options
]
,
prefix
)
maps_dir
,
config_dir
=
self
.
get_conf_dirs
()
try
:
with
open
(
"{}/{}.map"
.
format
(
maps_dir
,
pr
oject_name
),
"w"
)
as
map_file
:
with
open
(
"{}/{}.map"
.
format
(
maps_dir
,
pr
efix
),
"w"
)
as
map_file
:
map_file
.
write
(
redirects_map
)
for
code
,
data
in
redirects_with_options
:
if
code
==
"301"
:
...
...
@@ -26,7 +32,7 @@ class Generator:
code
=
"redirect"
with
open
(
maps_dir
+
"/
%
s_
%
s_options.map"
%
(
project_name
,
code
),
"w"
)
as
map_file
:
map_file
.
write
(
data
)
with
open
(
config_dir
+
"/
%
s.conf"
%
pr
oject_name
,
"w"
)
as
conf_file
:
with
open
(
config_dir
+
"/
%
s.conf"
%
pr
efix
,
"w"
)
as
conf_file
:
conf_file
.
write
(
conf_data
)
except
Exception
as
e
:
print
(
e
)
...
...
@@ -51,7 +57,7 @@ class MapGenerator:
data
=
self
.
start_line
%
project_name
for
arg1
,
arg2
in
redirects
:
# print('redirect is', arg1, arg2)
data
+=
"
\n\t
/
%
s
\t
/
%
s;"
%
(
project_name
+
arg1
,
project_name
+
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
project_name
+
arg1
,
project_name
+
arg2
)
data
+=
self
.
endline
return
data
...
...
@@ -75,12 +81,15 @@ class ConfigGenerator:
def
__init__
(
self
):
self
.
start_line
=
"if ($
%
s_redirect) {
\n
"
self
.
opt_start_line
=
"if ($
%
s_
%
s_redirect) {
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^
/
%
s/(.*)$ $
%
s_redirect
%
s;
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^
%
s/(.*)$ $
%
s_redirect
%
s;
\n
"
self
.
opt_rewrite_line
=
"
\t
rewrite ^/
%
s/(.*)$ $
%
s_
%
s_redirect
%
s;
\n
"
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
,
"redirect"
))
+
"}
\n
"
def
generate_config
(
self
,
project_name
,
options
,
prefix
):
if
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
])
>
len
(
"/"
):
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
"/{}"
.
format
(
prefix
),
project_name
,
"redirect"
))
+
"}
\n
"
else
:
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
""
,
project_name
,
"redirect"
))
+
"}
\n
"
for
code
in
options
:
if
code
==
"permanent"
:
...
...
redirector/utils/parser.py
View file @
e9910f24
...
...
@@ -123,8 +123,8 @@ class ConfigReader:
for
map_path
,
prefix
in
self
.
parse_yaml
(
yaml_file
):
abs_map_path
=
get_map_path
(
map_path
,
map_dir
)
if
map_file_name
not
in
abs_map_path
:
continue
#
if map_file_name not in abs_map_path:
#
continue
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
# Last directory of map_path a project's name
...
...
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