prvi commit
This commit is contained in:
commit
bd39af7663
4 changed files with 260 additions and 0 deletions
71
code/main.py
Normal file
71
code/main.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
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>
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
visit_file = os.path.join(base_dir, 'visits.txt')
|
||||
|
||||
git_commit = result = subprocess.run(
|
||||
['git', 'log', '-1', '--pretty=%B'],
|
||||
capture_output=True, text=True, check=True
|
||||
).stdout.splitlines()[0]
|
||||
|
||||
motd = "Dobrodošli na našu stranicu!"
|
||||
|
||||
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
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
print(html_files)
|
||||
content = replace_placeholders(html_files['index.html'])
|
||||
return content
|
||||
|
||||
@app.route('/<page>')
|
||||
def other_pages(page):
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue