Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
WWW
IA-Short
Commits
6df1ce16
Commit
6df1ce16
authored
Feb 24, 2019
by
132ikl
Browse files
Add subdomain support, clean request form code
parent
756ebdeb
Changes
2
Hide whitespace changes
Inline
Side-by-side
config.yml
View file @
6df1ce16
...
@@ -42,17 +42,17 @@ random_gen_timeout: 5
...
@@ -42,17 +42,17 @@ random_gen_timeout: 5
# Default: 'liteshort'
# Default: 'liteshort'
site_name
:
'
liteshort'
site_name
:
'
liteshort'
# String:
URL shown when finished generating shortlinks. Include the / at the end
.
# String:
Domain where the shortlinks will be served from. Useful if using the web interface on a subdomain
.
# If not set, it is automatically taken from the URL the shorten request is sent to.
# If not set, it is automatically taken from the URL the shorten request is sent to.
# If you don't know, leave unset
# If you don't know, leave unset
# Default: unset
# Default: unset
site_
url
:
site_
domain
:
'
'
# String:
URL to redirect to when link not found. Useful if using a reverse proxy with a subdomain for liteshort
# String:
Subdomain to host the web interface on.
#
If not set, it is automatically taken from the base URL
#
Useful if you want the shorturls on the short domain but the web interface on a subdomain.
# If you don't know, leave unset
# If you don't know, leave unset
# Default: unset
# Default: unset
redir_url
:
subdomain
:
'
'
# Boolean: Show link to project repository on GitHub at bottom right corner of page
# Boolean: Show link to project repository on GitHub at bottom right corner of page
# Default: true
# Default: true
...
...
liteshort.py
View file @
6df1ce16
...
@@ -20,14 +20,14 @@ def load_config():
...
@@ -20,14 +20,14 @@ def load_config():
req_options
=
{
'admin_username'
:
'admin'
,
'database_name'
:
"urls"
,
'random_length'
:
4
,
req_options
=
{
'admin_username'
:
'admin'
,
'database_name'
:
"urls"
,
'random_length'
:
4
,
'allowed_chars'
:
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
,
'allowed_chars'
:
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
,
'random_gen_timeout'
:
5
,
'site_name'
:
'liteshort'
,
'site_
url
'
:
None
,
'show_github_link'
:
True
,
'random_gen_timeout'
:
5
,
'site_name'
:
'liteshort'
,
'site_
domain
'
:
None
,
'show_github_link'
:
True
,
'secret_key'
:
None
,
'disable_api'
:
False
,
'
redir_url'
:
None
'secret_key'
:
None
,
'disable_api'
:
False
,
'
subdomain'
:
''
}
}
config_types
=
{
'admin_username'
:
str
,
'database_name'
:
str
,
'random_length'
:
int
,
config_types
=
{
'admin_username'
:
str
,
'database_name'
:
str
,
'random_length'
:
int
,
'allowed_chars'
:
str
,
'random_gen_timeout'
:
int
,
'site_name'
:
str
,
'allowed_chars'
:
str
,
'random_gen_timeout'
:
int
,
'site_name'
:
str
,
'site_
url
'
:
(
str
,
type
(
None
)),
'show_github_link'
:
bool
,
'secret_key'
:
str
,
'site_
domain
'
:
(
str
,
type
(
None
)),
'show_github_link'
:
bool
,
'secret_key'
:
str
,
'disable_api'
:
bool
,
'
redir_url
'
:
(
str
,
type
(
None
))
'disable_api'
:
bool
,
'
subdomain
'
:
(
str
,
type
(
None
))
}
}
for
option
in
req_options
.
keys
():
for
option
in
req_options
.
keys
():
...
@@ -127,9 +127,9 @@ def nested_list_to_dict(l):
...
@@ -127,9 +127,9 @@ def nested_list_to_dict(l):
def
response
(
rq
,
result
,
error_msg
=
"Error: Unknown error"
):
def
response
(
rq
,
result
,
error_msg
=
"Error: Unknown error"
):
if
'api'
in
rq
.
form
and
'format'
not
in
rq
.
form
:
if
rq
.
form
.
get
(
'api'
)
and
not
rq
.
form
.
get
(
'format'
)
==
'json'
:
return
"Format type HTML (default) not support for API"
# Future-proof for non-json return types
return
"Format type HTML (default) not support for API"
# Future-proof for non-json return types
if
'format'
in
rq
.
form
and
rq
.
form
[
'format'
]
==
'json'
:
if
rq
.
form
.
get
(
'format'
)
==
'json'
:
# If not result provided OR result doesn't exist, send error
# If not result provided OR result doesn't exist, send error
# Allows for setting an error message with explicitly checking in regular code
# Allows for setting an error message with explicitly checking in regular code
if
result
:
if
result
:
...
@@ -190,9 +190,10 @@ def close_db(error):
...
@@ -190,9 +190,10 @@ def close_db(error):
app
.
config
.
update
(
load_config
())
# Add YAML config to Flask config
app
.
config
.
update
(
load_config
())
# Add YAML config to Flask config
app
.
secret_key
=
app
.
config
[
'secret_key'
]
app
.
secret_key
=
app
.
config
[
'secret_key'
]
app
.
config
[
'SERVER_NAME'
]
=
app
.
config
[
'site_domain'
]
@
app
.
route
(
'/'
)
@
app
.
route
(
'/'
,
subdomain
=
app
.
config
[
'subdomain'
]
)
def
main
():
def
main
():
return
response
(
request
,
True
)
return
response
(
request
,
True
)
...
@@ -203,17 +204,16 @@ def main_redir(url):
...
@@ -203,17 +204,16 @@ def main_redir(url):
if
long
:
if
long
:
return
redirect
(
long
,
301
)
return
redirect
(
long
,
301
)
flash
(
'Short URL "'
+
url
+
'" doesn
\'
t exist'
,
'error'
)
flash
(
'Short URL "'
+
url
+
'" doesn
\'
t exist'
,
'error'
)
redirect_site
=
(
current_app
.
config
[
'redir_url'
]
or
url_for
(
'main'
)
)
redirect_site
=
url_for
(
'main'
)
return
redirect
(
redirect_site
)
return
redirect
(
redirect_site
)
@
app
.
route
(
'/'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/'
,
methods
=
[
'POST'
]
,
subdomain
=
app
.
config
[
'subdomain'
]
)
def
main_post
():
def
main_post
():
# Check if long in form (ie. provided by curl) and not blank (browsers always send blank forms as empty quote)
if
request
.
form
.
get
(
'long'
):
if
'long'
in
request
.
form
and
request
.
form
[
'long'
]:
if
not
validate_long
(
request
.
form
[
'long'
]):
if
not
validate_long
(
request
.
form
[
'long'
]):
return
response
(
request
,
None
,
"Long URL is not valid"
)
return
response
(
request
,
None
,
"Long URL is not valid"
)
if
'short'
in
request
.
form
and
request
.
form
[
'short'
]
:
if
request
.
form
.
get
(
'short'
)
:
# Validate long as URL and short custom text against allowed characters
# Validate long as URL and short custom text against allowed characters
result
=
validate_short
(
request
.
form
[
'short'
])
result
=
validate_short
(
request
.
form
[
'short'
])
if
validate_short
(
request
.
form
[
'short'
])
is
True
:
if
validate_short
(
request
.
form
[
'short'
])
is
True
:
...
@@ -221,7 +221,7 @@ def main_post():
...
@@ -221,7 +221,7 @@ def main_post():
else
:
else
:
return
result
return
result
if
get_long
(
short
)
==
request
.
form
[
'long'
]:
if
get_long
(
short
)
==
request
.
form
[
'long'
]:
return
response
(
request
,
(
current_
app
.
config
[
'site_
url'
]
or
request
.
base_url
)
+
short
,
return
response
(
request
,
(
(
'https://'
+
app
.
config
[
'site_
domain'
]
+
'/'
)
or
request
.
base_url
)
+
short
,
'Error: Failed to return pre-existing non-random shortlink'
)
'Error: Failed to return pre-existing non-random shortlink'
)
else
:
else
:
short
=
generate_short
(
request
)
short
=
generate_short
(
request
)
...
@@ -229,14 +229,15 @@ def main_post():
...
@@ -229,14 +229,15 @@ def main_post():
return
response
(
request
,
None
,
return
response
(
request
,
None
,
'Short URL already taken'
)
'Short URL already taken'
)
long_exists
=
check_long_exist
(
request
.
form
[
'long'
])
long_exists
=
check_long_exist
(
request
.
form
[
'long'
])
if
long_exists
and
not
request
.
form
[
'short'
]:
if
long_exists
and
not
request
.
form
.
get
(
'short'
):
return
response
(
request
,
(
current_app
.
config
[
'site_url'
]
or
request
.
base_url
)
+
long_exists
,
# TODO: un-hack-ify adding the protocol here
return
response
(
request
,
((
'https://'
+
app
.
config
[
'site_domain'
]
+
'/'
)
or
request
.
base_url
)
+
long_exists
,
'Error: Failed to return pre-existing random shortlink'
)
'Error: Failed to return pre-existing random shortlink'
)
get_db
().
cursor
().
execute
(
'INSERT INTO urls (long,short) VALUES (?,?)'
,
(
request
.
form
[
'long'
],
short
))
get_db
().
cursor
().
execute
(
'INSERT INTO urls (long,short) VALUES (?,?)'
,
(
request
.
form
[
'long'
],
short
))
get_db
().
commit
()
get_db
().
commit
()
return
response
(
request
,
(
current_
app
.
config
[
'site_
url'
]
or
request
.
base_url
)
+
short
,
return
response
(
request
,
(
(
'https://'
+
app
.
config
[
'site_
domain'
]
+
'/'
)
or
request
.
base_url
)
+
short
,
'Error: Failed to generate'
)
'Error: Failed to generate'
)
elif
'api'
in
request
.
form
:
elif
request
.
form
.
get
(
'api'
)
:
if
current_app
.
config
[
'disable_api'
]:
if
current_app
.
config
[
'disable_api'
]:
return
response
(
request
,
None
,
"API is disabled."
)
return
response
(
request
,
None
,
"API is disabled."
)
# All API calls require authentication
# All API calls require authentication
...
...
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