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
|
all: run
|
||||||
|
|
||||||
run:
|
run:
|
||||||
nohup python app.py > output.log 2>&1 & echo $$! > app.pid
|
docker compose build
|
||||||
|
docker compose up -d
|
||||||
|
|
||||||
stop:
|
stop:
|
||||||
@if [ -s app.pid ]; then \
|
docker compose down -v
|
||||||
kill -9 $$(cat app.pid) && rm -f app.pid; \
|
|
||||||
else \
|
|
||||||
echo "No running process found."; \
|
|
||||||
fi
|
|
||||||
|
|
||||||
restart: stop run
|
restart: stop run
|
||||||
|
|
||||||
logs:
|
logs:
|
||||||
tail -f output.log
|
docker compose logs -f
|
||||||
|
|
||||||
clean: stop
|
clean: stop
|
||||||
rm -f output.log app.pid
|
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__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
try:
|
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())
|
app.config["SECRET_KEY"] = bytes.hex(f.readline())
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError as e:
|
||||||
os.mkdir('instance')
|
if not os.path.exists("/app/instance"):
|
||||||
with open("instance/secret.key", "wb") as f:
|
os.mkdir("/app/instance")
|
||||||
|
|
||||||
|
with open("/app/instance/secret.key", "wb") as f:
|
||||||
newKey = token_hex(64)
|
newKey = token_hex(64)
|
||||||
f.write(bytearray.fromhex(newKey))
|
f.write(bytearray.fromhex(newKey))
|
||||||
app.config["SECRET_KEY"] = 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["UPLOAD_FOLDER"] = "sites"
|
||||||
app.config["SERVER_NAME"] = "tinysite.cloud"
|
app.config["SERVER_NAME"] = "tinysite.cloud"
|
||||||
app.config["SESSION_COOKIE_DOMAIN"] = ".tinysite.cloud"
|
app.config["SESSION_COOKIE_DOMAIN"] = ".tinysite.cloud"
|
||||||
@@ -62,11 +65,16 @@ RESERVED_SUBDOMAINS = {
|
|||||||
"secure",
|
"secure",
|
||||||
"mail",
|
"mail",
|
||||||
"status",
|
"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
|
return not subdomain or subdomain in RESERVED_SUBDOMAINS
|
||||||
|
|
||||||
|
|
||||||
@@ -179,7 +187,12 @@ def register():
|
|||||||
@login_required
|
@login_required
|
||||||
def dashboard():
|
def dashboard():
|
||||||
sites = Site.query.filter_by(user_id=current_user.id).all()
|
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"])
|
@app.route("/upload", methods=["POST"])
|
||||||
@@ -311,18 +324,17 @@ def delete_file(site_id, filename):
|
|||||||
@app.route("/", subdomain="<subdomain>", defaults={"filename": "index.html"})
|
@app.route("/", subdomain="<subdomain>", defaults={"filename": "index.html"})
|
||||||
@app.route("/<path:filename>")
|
@app.route("/<path:filename>")
|
||||||
def serve_site_content(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 = Site.query.filter_by(subdomain=subdomain).first_or_404()
|
||||||
site.last_accessed = datetime.utcnow()
|
site.last_accessed = datetime.utcnow()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
site_dir = os.path.join(
|
site_dir = os.path.join(
|
||||||
app.config["UPLOAD_FOLDER"], str(site.user_id), str(site.id)
|
app.config["UPLOAD_FOLDER"], str(site.user_id), str(site.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: add specific page redirects here as they're added
|
if isDefaultRoute(request.host):
|
||||||
if isDefaultRoute(subdomain):
|
|
||||||
return send_from_directory("index.html")
|
return send_from_directory("index.html")
|
||||||
|
|
||||||
# Security check
|
# Security check
|
||||||
@@ -358,11 +370,11 @@ def home():
|
|||||||
if isDefaultRoute(request.host):
|
if isDefaultRoute(request.host):
|
||||||
return render_template("home.html")
|
return render_template("home.html")
|
||||||
else:
|
else:
|
||||||
return serve_site_content('index.html')
|
return serve_site_content("index.html")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
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