From f465ee2b7643236ccae7f6d76483e5f6558b61e3 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Sat, 4 Apr 2020 23:14:10 -0400 Subject: [PATCH] Reorganize into package --- LICENSE | 2 +- README.md | 7 ++- liteshort.ini | 3 +- liteshort/__init__.py | 0 liteshort.py => liteshort/main.py | 47 +++++++------------ {static => liteshort/static}/GitHub.svg | 0 {static => liteshort/static}/favicon.ico | Bin {static => liteshort/static}/styles.css | 0 {templates => liteshort/templates}/main.html | 0 wsgi.py => liteshort/wsgi.py | 2 +- 10 files changed, 22 insertions(+), 39 deletions(-) create mode 100644 liteshort/__init__.py rename liteshort.py => liteshort/main.py (92%) rename {static => liteshort/static}/GitHub.svg (100%) rename {static => liteshort/static}/favicon.ico (100%) rename {static => liteshort/static}/styles.css (100%) rename {templates => liteshort/templates}/main.html (100%) rename wsgi.py => liteshort/wsgi.py (61%) diff --git a/LICENSE b/LICENSE index 410d83e..6db2ded 100644 --- a/LICENSE +++ b/LICENSE @@ -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: diff --git a/README.md b/README.md index 0683f03..0bc414f 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,9 @@ Start in the directory you wish to install to and modify to fit your installatio ```sh git clone https://github.com/132ikl/liteshort -python3 -m venv virtualenv -source virtualenv/bin/activate -pip install wheel -pip install bcrypt flask pyyaml uwsgi +python3 -m venv venv +source venv/bin/activate +pip3 install -r requirements.txt uwsgi ``` Edit `liteshort.ini` and `liteshort.service` as seen fit. Then edit `config.yml` according to the [Configuration](#configuration) section. diff --git a/liteshort.ini b/liteshort.ini index 886b8c1..d085460 100644 --- a/liteshort.ini +++ b/liteshort.ini @@ -1,5 +1,5 @@ [uwsgi] -module = wsgi:app +module = liteshort.wsgi:app master = true processes = 2 @@ -9,4 +9,3 @@ chmod-socket = 666 vacuum = true die-on-term = true - diff --git a/liteshort/__init__.py b/liteshort/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/liteshort.py b/liteshort/main.py similarity index 92% rename from liteshort.py rename to liteshort/main.py index 969e93a..17ac5be 100644 --- a/liteshort.py +++ b/liteshort/main.py @@ -1,36 +1,21 @@ -# Copyright (c) 2020 Steven Spangler <132@ikl.sh> -# 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/ - +#!/usr/bin/env python3 import os import random import sqlite3 import time import urllib -import bcrypt -import yaml -from flask import ( - Flask, - current_app, - flash, - g, - jsonify, - make_response, - redirect, - render_template, - request, - send_from_directory, - url_for, -) +import flask +from bcrypt import checkpw +from flask import current_app, g, redirect, render_template, request, url_for +from yaml import safe_load -app = Flask(__name__) +app = flask.Flask(__name__) def load_config(): with open("config.yml") as config: - configYaml = yaml.safe_load(config) + configYaml = safe_load(config) config = { k.lower(): v for k, v in configYaml.items() } # Make config keys case insensitive @@ -137,7 +122,7 @@ def linking_to_blocklist(long): def check_password(password, pass_config): if pass_config["password_hashed"]: - return bcrypt.checkpw( + return checkpw( 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.accept_mimetypes.accept_json: if result: - return jsonify(success=bool(result), result=result) - return jsonify(success=bool(result), message=error_msg) + return flask.jsonify(success=bool(result), result=result) + return flask.jsonify(success=bool(result), message=error_msg) else: return "Format type HTML (default) not supported for API" # Future-proof for non-json return types else: if result and result is not True: - flash(result, "success") + flask.flash(result, "success") elif not result: - flash(error_msg, "error") + flask.flash(error_msg, "error") 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"]) def favicon(): - return send_from_directory( + return flask.send_from_directory( os.path.join(app.root_path, "static"), "favicon.ico", mimetype="image/vnd.microsoft.icon", @@ -318,10 +303,10 @@ def main(): def main_redir(url): long = get_long(url) if long: - resp = make_response(redirect(long, 301)) + resp = flask.make_response(flask.redirect(long, 301)) else: - flash('Short URL "' + url + "\" doesn't exist", "error") - resp = make_response(redirect(url_for("main"))) + flask.flash('Short URL "' + url + "\" doesn't exist", "error") + resp = flask.make_response(flask.redirect(url_for("main"))) resp.headers.set("Cache-Control", "no-store, must-revalidate") return resp diff --git a/static/GitHub.svg b/liteshort/static/GitHub.svg similarity index 100% rename from static/GitHub.svg rename to liteshort/static/GitHub.svg diff --git a/static/favicon.ico b/liteshort/static/favicon.ico similarity index 100% rename from static/favicon.ico rename to liteshort/static/favicon.ico diff --git a/static/styles.css b/liteshort/static/styles.css similarity index 100% rename from static/styles.css rename to liteshort/static/styles.css diff --git a/templates/main.html b/liteshort/templates/main.html similarity index 100% rename from templates/main.html rename to liteshort/templates/main.html diff --git a/wsgi.py b/liteshort/wsgi.py similarity index 61% rename from wsgi.py rename to liteshort/wsgi.py index 55a289f..6e83334 100644 --- a/wsgi.py +++ b/liteshort/wsgi.py @@ -1,4 +1,4 @@ -from liteshort import app +from .main import app if __name__ == "__main__": app.run()