mirror of
https://github.com/ION606/static-site-hosting.git
synced 2026-05-14 22:16:54 +00:00
I gave in...back to docker
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
all: run
|
||||
|
||||
run:
|
||||
nohup python app.py > output.log 2>&1 & echo $$! > app.pid
|
||||
docker compose build
|
||||
docker compose up -d
|
||||
|
||||
stop:
|
||||
@if [ -s app.pid ]; then \
|
||||
kill -9 $$(cat app.pid) && rm -f app.pid; \
|
||||
else \
|
||||
echo "No running process found."; \
|
||||
fi
|
||||
docker compose down -v
|
||||
|
||||
restart: stop run
|
||||
|
||||
logs:
|
||||
tail -f output.log
|
||||
docker compose logs -f
|
||||
|
||||
clean: stop
|
||||
rm -f output.log app.pid
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
all: run
|
||||
|
||||
run:
|
||||
nohup .venv/bin/python app.py > output.log 2>&1 & echo $$! > app.pid
|
||||
|
||||
stop:
|
||||
@if [ -s app.pid ]; then \
|
||||
kill -9 $$(cat app.pid) && rm -f app.pid; \
|
||||
else \
|
||||
echo "No running process found."; \
|
||||
fi
|
||||
|
||||
restart: stop run
|
||||
|
||||
logs:
|
||||
tail -f output.log
|
||||
|
||||
clean: stop
|
||||
rm -f output.log app.pid
|
||||
|
||||
reset: clean
|
||||
rm -rf instance
|
||||
rm -rf sites
|
||||
@@ -31,16 +31,19 @@ from secrets import token_hex
|
||||
app = Flask(__name__)
|
||||
|
||||
try:
|
||||
with open("instance/secret.key", "rb") as f:
|
||||
with open("/app/instance/secret.key", "rb") as f:
|
||||
app.config["SECRET_KEY"] = bytes.hex(f.readline())
|
||||
except FileNotFoundError as e:
|
||||
os.mkdir('instance')
|
||||
with open("instance/secret.key", "wb") as f:
|
||||
if not os.path.exists("/app/instance"):
|
||||
os.mkdir("/app/instance")
|
||||
|
||||
with open("/app/instance/secret.key", "wb") as f:
|
||||
newKey = token_hex(64)
|
||||
f.write(bytearray.fromhex(newKey))
|
||||
app.config["SECRET_KEY"] = newKey
|
||||
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite"
|
||||
PORT = 5121
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////app/instance/db.sqlite"
|
||||
app.config["UPLOAD_FOLDER"] = "sites"
|
||||
app.config["SERVER_NAME"] = "tinysite.cloud"
|
||||
app.config["SESSION_COOKIE_DOMAIN"] = ".tinysite.cloud"
|
||||
@@ -62,11 +65,16 @@ RESERVED_SUBDOMAINS = {
|
||||
"secure",
|
||||
"mail",
|
||||
"status",
|
||||
"gateway"
|
||||
"gateway",
|
||||
}
|
||||
|
||||
|
||||
def isDefaultRoute(subdomain):
|
||||
# TODO: add specific page redirects here as they're added
|
||||
def isDefaultRoute(hostname: str):
|
||||
if hostname.count(".") < 2:
|
||||
return True
|
||||
|
||||
subdomain = hostname.host.split(".")[0]
|
||||
return not subdomain or subdomain in RESERVED_SUBDOMAINS
|
||||
|
||||
|
||||
@@ -179,7 +187,12 @@ def register():
|
||||
@login_required
|
||||
def dashboard():
|
||||
sites = Site.query.filter_by(user_id=current_user.id).all()
|
||||
return render_template("dashboard.html", sites=sites, subdomain=request.host.split('.')[0], hostname=app.config["SERVER_NAME"])
|
||||
return render_template(
|
||||
"dashboard.html",
|
||||
sites=sites,
|
||||
subdomain=request.host.split(".")[0],
|
||||
hostname=app.config["SERVER_NAME"],
|
||||
)
|
||||
|
||||
|
||||
@app.route("/upload", methods=["POST"])
|
||||
@@ -311,7 +324,7 @@ def delete_file(site_id, filename):
|
||||
@app.route("/", subdomain="<subdomain>", defaults={"filename": "index.html"})
|
||||
@app.route("/<path:filename>")
|
||||
def serve_site_content(filename):
|
||||
subdomain = request.host.split('.')[0]
|
||||
subdomain = request.host.split(".")[0]
|
||||
|
||||
site = Site.query.filter_by(subdomain=subdomain).first_or_404()
|
||||
site.last_accessed = datetime.utcnow()
|
||||
@@ -321,8 +334,7 @@ def serve_site_content(filename):
|
||||
app.config["UPLOAD_FOLDER"], str(site.user_id), str(site.id)
|
||||
)
|
||||
|
||||
# TODO: add specific page redirects here as they're added
|
||||
if isDefaultRoute(subdomain):
|
||||
if isDefaultRoute(request.host):
|
||||
return send_from_directory("index.html")
|
||||
|
||||
# Security check
|
||||
@@ -358,11 +370,11 @@ def home():
|
||||
if isDefaultRoute(request.host):
|
||||
return render_template("home.html")
|
||||
else:
|
||||
return serve_site_content('index.html')
|
||||
return serve_site_content("index.html")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
serve(app, host="0.0.0.0", port=5121)
|
||||
serve(app, host="0.0.0.0", port=PORT)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
ports:
|
||||
- "5121:5121"
|
||||
networks:
|
||||
- static
|
||||
volumes:
|
||||
- ./instance:/app/instance
|
||||
- ./sites:/app/sites
|
||||
environment:
|
||||
- FLASK_ENV=production
|
||||
|
||||
networks:
|
||||
static:
|
||||
Reference in New Issue
Block a user