1
0
mirror of https://git.ikl.sh/132ikl/liteshort.git synced 2023-08-10 21:13:04 +03:00

Reorganize into package

This commit is contained in:
132ikl 2020-04-04 23:14:10 -04:00
parent ae1a05c83d
commit f465ee2b76
10 changed files with 22 additions and 39 deletions

View File

@ -1,4 +1,4 @@
Copyright 2020 Steven Spangler Copyright 2020 132ikl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -25,10 +25,9 @@ Start in the directory you wish to install to and modify to fit your installatio
```sh ```sh
git clone https://github.com/132ikl/liteshort git clone https://github.com/132ikl/liteshort
python3 -m venv virtualenv python3 -m venv venv
source virtualenv/bin/activate source venv/bin/activate
pip install wheel pip3 install -r requirements.txt uwsgi
pip install bcrypt flask pyyaml uwsgi
``` ```
Edit `liteshort.ini` and `liteshort.service` as seen fit. Then edit `config.yml` according to the [Configuration](#configuration) section. Edit `liteshort.ini` and `liteshort.service` as seen fit. Then edit `config.yml` according to the [Configuration](#configuration) section.

View File

@ -1,5 +1,5 @@
[uwsgi] [uwsgi]
module = wsgi:app module = liteshort.wsgi:app
master = true master = true
processes = 2 processes = 2
@ -9,4 +9,3 @@ chmod-socket = 666
vacuum = true vacuum = true
die-on-term = true die-on-term = true

0
liteshort/__init__.py Normal file
View File

View File

@ -1,36 +1,21 @@
# Copyright (c) 2020 Steven Spangler <132@ikl.sh> #!/usr/bin/env python3
# This file is part of liteshort by 132ikl
# This software is license under the MIT license. It should be included in your copy of this software.
# A copy of the MIT license can be obtained at https://mit-license.org/
import os import os
import random import random
import sqlite3 import sqlite3
import time import time
import urllib import urllib
import bcrypt import flask
import yaml from bcrypt import checkpw
from flask import ( from flask import current_app, g, redirect, render_template, request, url_for
Flask, from yaml import safe_load
current_app,
flash,
g,
jsonify,
make_response,
redirect,
render_template,
request,
send_from_directory,
url_for,
)
app = Flask(__name__) app = flask.Flask(__name__)
def load_config(): def load_config():
with open("config.yml") as config: with open("config.yml") as config:
configYaml = yaml.safe_load(config) configYaml = safe_load(config)
config = { config = {
k.lower(): v for k, v in configYaml.items() k.lower(): v for k, v in configYaml.items()
} # Make config keys case insensitive } # Make config keys case insensitive
@ -137,7 +122,7 @@ def linking_to_blocklist(long):
def check_password(password, pass_config): def check_password(password, pass_config):
if pass_config["password_hashed"]: if pass_config["password_hashed"]:
return bcrypt.checkpw( return checkpw(
password.encode("utf-8"), password.encode("utf-8"),
pass_config["admin_hashed_password"].encode("utf-8"), pass_config["admin_hashed_password"].encode("utf-8"),
) )
@ -220,15 +205,15 @@ def response(rq, result, error_msg="Error: Unknown error"):
if rq.form.get("api"): if rq.form.get("api"):
if rq.accept_mimetypes.accept_json: if rq.accept_mimetypes.accept_json:
if result: if result:
return jsonify(success=bool(result), result=result) return flask.jsonify(success=bool(result), result=result)
return jsonify(success=bool(result), message=error_msg) return flask.jsonify(success=bool(result), message=error_msg)
else: else:
return "Format type HTML (default) not supported for API" # Future-proof for non-json return types return "Format type HTML (default) not supported for API" # Future-proof for non-json return types
else: else:
if result and result is not True: if result and result is not True:
flash(result, "success") flask.flash(result, "success")
elif not result: elif not result:
flash(error_msg, "error") flask.flash(error_msg, "error")
return render_template("main.html") return render_template("main.html")
@ -302,7 +287,7 @@ app.config["SERVER_NAME"] = app.config["site_domain"]
@app.route("/favicon.ico", subdomain=app.config["subdomain"]) @app.route("/favicon.ico", subdomain=app.config["subdomain"])
def favicon(): def favicon():
return send_from_directory( return flask.send_from_directory(
os.path.join(app.root_path, "static"), os.path.join(app.root_path, "static"),
"favicon.ico", "favicon.ico",
mimetype="image/vnd.microsoft.icon", mimetype="image/vnd.microsoft.icon",
@ -318,10 +303,10 @@ def main():
def main_redir(url): def main_redir(url):
long = get_long(url) long = get_long(url)
if long: if long:
resp = make_response(redirect(long, 301)) resp = flask.make_response(flask.redirect(long, 301))
else: else:
flash('Short URL "' + url + "\" doesn't exist", "error") flask.flash('Short URL "' + url + "\" doesn't exist", "error")
resp = make_response(redirect(url_for("main"))) resp = flask.make_response(flask.redirect(url_for("main")))
resp.headers.set("Cache-Control", "no-store, must-revalidate") resp.headers.set("Cache-Control", "no-store, must-revalidate")
return resp return resp

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -1,4 +1,4 @@
from liteshort import app from .main import app
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run()