Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
I
IA-Short
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
WWW
IA-Short
Commits
6df1ce16
Commit
6df1ce16
authored
Feb 24, 2019
by
132ikl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add subdomain support, clean request form code
parent
756ebdeb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
22 deletions
+23
-22
config.yml
config.yml
+5
-5
liteshort.py
liteshort.py
+18
-17
No files found.
config.yml
View file @
6df1ce16
...
...
@@ -42,17 +42,17 @@ random_gen_timeout: 5
# Default: '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 you don't know, leave 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
#
If not set, it is automatically taken from the base URL
# String:
Subdomain to host the web interface on.
#
Useful if you want the shorturls on the short domain but the web interface on a subdomain.
# If you don't know, leave unset
# Default: unset
redir_url
:
subdomain
:
'
'
# Boolean: Show link to project repository on GitHub at bottom right corner of page
# Default: true
...
...
liteshort.py
View file @
6df1ce16
...
...
@@ -20,14 +20,14 @@ def load_config():
req_options
=
{
'admin_username'
:
'admin'
,
'database_name'
:
"urls"
,
'random_length'
:
4
,
'allowed_chars'
:
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
,
'random_gen_timeout'
:
5
,
'site_name'
:
'liteshort'
,
'site_
url
'
:
None
,
'show_github_link'
:
True
,
'secret_key'
:
None
,
'disable_api'
:
False
,
'
redir_url'
:
None
'random_gen_timeout'
:
5
,
'site_name'
:
'liteshort'
,
'site_
domain
'
:
None
,
'show_github_link'
:
True
,
'secret_key'
:
None
,
'disable_api'
:
False
,
'
subdomain'
:
''
}
config_types
=
{
'admin_username'
:
str
,
'database_name'
:
str
,
'random_length'
:
int
,
'allowed_chars'
:
str
,
'random_gen_timeout'
:
int
,
'site_name'
:
str
,
'site_
url
'
:
(
str
,
type
(
None
)),
'show_github_link'
:
bool
,
'secret_key'
:
str
,
'disable_api'
:
bool
,
'
redir_url
'
:
(
str
,
type
(
None
))
'site_
domain
'
:
(
str
,
type
(
None
)),
'show_github_link'
:
bool
,
'secret_key'
:
str
,
'disable_api'
:
bool
,
'
subdomain
'
:
(
str
,
type
(
None
))
}
for
option
in
req_options
.
keys
():
...
...
@@ -127,9 +127,9 @@ def nested_list_to_dict(l):
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
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
# Allows for setting an error message with explicitly checking in regular code
if
result
:
...
...
@@ -190,9 +190,10 @@ def close_db(error):
app
.
config
.
update
(
load_config
())
# Add YAML config to Flask config
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
():
return
response
(
request
,
True
)
...
...
@@ -203,17 +204,16 @@ def main_redir(url):
if
long
:
return
redirect
(
long
,
301
)
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
)
@
app
.
route
(
'/'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/'
,
methods
=
[
'POST'
]
,
subdomain
=
app
.
config
[
'subdomain'
]
)
def
main_post
():
# Check if long in form (ie. provided by curl) and not blank (browsers always send blank forms as empty quote)
if
'long'
in
request
.
form
and
request
.
form
[
'long'
]:
if
request
.
form
.
get
(
'long'
):
if
not
validate_long
(
request
.
form
[
'long'
]):
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
result
=
validate_short
(
request
.
form
[
'short'
])
if
validate_short
(
request
.
form
[
'short'
])
is
True
:
...
...
@@ -221,7 +221,7 @@ def main_post():
else
:
return
result
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'
)
else
:
short
=
generate_short
(
request
)
...
...
@@ -229,14 +229,15 @@ def main_post():
return
response
(
request
,
None
,
'Short URL already taken'
)
long_exists
=
check_long_exist
(
request
.
form
[
'long'
])
if
long_exists
and
not
request
.
form
[
'short'
]:
return
response
(
request
,
(
current_app
.
config
[
'site_url'
]
or
request
.
base_url
)
+
long_exists
,
if
long_exists
and
not
request
.
form
.
get
(
'short'
):
# 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'
)
get_db
().
cursor
().
execute
(
'INSERT INTO urls (long,short) VALUES (?,?)'
,
(
request
.
form
[
'long'
],
short
))
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'
)
elif
'api'
in
request
.
form
:
elif
request
.
form
.
get
(
'api'
)
:
if
current_app
.
config
[
'disable_api'
]:
return
response
(
request
,
None
,
"API is disabled."
)
# 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