81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
from flask import Flask, render_template_string
|
|
import subprocess
|
|
import os
|
|
import re
|
|
import datetime
|
|
pocelo = datetime.datetime.now()
|
|
app = Flask(__name__)
|
|
visits = 0
|
|
|
|
navbar = """
|
|
<nav>
|
|
<a href="index.html">Početna</a>
|
|
<a href="projects.html">Projekti</a>
|
|
<a href="contribute.html">Kako doprinijeti</a>
|
|
</nav>
|
|
|
|
|
|
"""
|
|
|
|
|
|
# Putanje do HTML datoteka
|
|
base_dir = os.path.abspath('..')
|
|
html_files = {
|
|
'index.html': os.path.join(base_dir, 'html', 'index.html'),
|
|
'projects.html': os.path.join(base_dir, 'html', 'projects.html'),
|
|
'contribute.html': os.path.join(base_dir, 'html', 'contribute.html')
|
|
}
|
|
|
|
# Putanja do fajla sa brojem posjeta
|
|
visit_file = os.path.join(base_dir, 'visits.txt')
|
|
|
|
# Git commit info
|
|
git_commit = result = subprocess.run(
|
|
['git', 'log', '-1', '--pretty=%B'],
|
|
capture_output=True, text=True, check=True
|
|
).stdout.splitlines()[0]
|
|
|
|
# MOTD (Message of the Day)
|
|
motd = "Dobrodošli na našu stranicu!"
|
|
|
|
# Funkcija za broj posjeta
|
|
# Funkcija za zamjenu placeholder-a u HTML sadržaju
|
|
def replace_placeholders(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
global visits
|
|
global pocelo
|
|
content = file.read()
|
|
footer = f'''
|
|
<footer>
|
|
<p><strong>Git Commit:</strong> {git_commit}</p>
|
|
<p><strong>Broj posjeta stranici:</strong> {visits}</p>
|
|
<p><strong>Server pokrenut:</strong> {pocelo.strftime("%d.%m.%Y %H:%M:%S")}</p>
|
|
</footer>
|
|
'''
|
|
content = re.sub(r'{{GIT_COMMIT}}', git_commit, content)
|
|
content = re.sub(r'{{MOTD}}', motd, content)
|
|
content = re.sub(r'{{nav}}', navbar, content)
|
|
content = re.sub(r'{{footer}}', footer, content)
|
|
visits+=1
|
|
return content
|
|
|
|
# Ruta za početnu stranicu
|
|
@app.route('/')
|
|
def home():
|
|
# Učitavanje index.html i zamjena placeholder-a
|
|
print(html_files)
|
|
content = replace_placeholders(html_files['index.html'])
|
|
return content
|
|
|
|
# Ruta za druge stranice (projects, contribute)
|
|
@app.route('/<page>')
|
|
def other_pages(page):
|
|
# Provjera ako stranica postoji u mapiranju
|
|
if page in html_files:
|
|
content = replace_placeholders(html_files[page])
|
|
return content
|
|
else:
|
|
return "<h1>Stranica nije pronađena</h1>", 404
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0', port=8000)
|