1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Website performance improvements: HTTP/2 Server Push

This commit is contained in:
Krateng 2019-02-17 14:25:40 +01:00
parent 88badbb4f0
commit 1c029e33ff
8 changed files with 53 additions and 20 deletions

View File

@ -25,7 +25,8 @@ webserver = Bottle()
@webserver.route("")
@webserver.route("/")
def mainpage():
return static_html("start")
response = static_html("start")
return response
# this is the fallback option. If you run this service behind a reverse proxy, it is recommended to rewrite /db/ requests to the port of the db server
@ -114,12 +115,19 @@ def static(name):
@webserver.route("/<name>")
def static_html(name):
linkheaders = ["</maloja.css>; rel=preload; as=style"]
keys = removeIdentical(FormsDict.decode(request.query))
# If a python file exists, it provides the replacement dict for the html file
if os.path.exists("website/" + name + ".py"):
txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
#txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys,DATABASE_PORT)
# add headers for server push
for resource in resources:
linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"])
# apply key substitutions
with open("website/" + name + ".html") as htmlfile:
html = htmlfile.read()
for k in txt_keys:
@ -128,11 +136,14 @@ def static_html(name):
for element in txt_keys[k]:
html = html.replace(k,element,1)
else:
html = html.replace(k,txt_keys[k])
html = html.replace(k,txt_keys[k])
response.set_header("Link",",".join(linkheaders))
return html
# Otherwise, we just serve the html file
response.set_header("Link",",".join(linkheaders))
return static_file("website/" + name + ".html",root="")
#set graceful shutdown

View File

@ -2,7 +2,7 @@ import urllib
import json
def replacedict(keys,dbport):
def instructions(keys,dbport):
from utilities import getArtistInfo
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink
@ -10,6 +10,7 @@ def replacedict(keys,dbport):
info = getArtistInfo(keys["artist"])
imgurl = info.get("image")
#desc = info.get("info")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"]))
db_data = json.loads(response.read())
@ -43,5 +44,8 @@ def replacedict(keys,dbport):
html += "</tr>"
html += "</table>"
return {"KEY_ARTISTNAME":keys["artist"],"KEY_ENC_ARTISTNAME":urllib.parse.quote(keys["artist"]),"KEY_IMAGEURL":imgurl, "KEY_DESCRIPTION":"","KEY_TRACKLIST":html,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr}
replace = {"KEY_ARTISTNAME":keys["artist"],"KEY_ENC_ARTISTNAME":urllib.parse.quote(keys["artist"]),"KEY_IMAGEURL":imgurl, "KEY_DESCRIPTION":"","KEY_TRACKLIST":html,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr}
return (replace,pushresources)

View File

@ -2,7 +2,7 @@ import urllib
import json
from htmlgenerators import artistLink
def replacedict(keys,dbport):
def instructions(keys,dbport):
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/issues")
db_data = json.loads(response.read())
@ -42,4 +42,4 @@ def replacedict(keys,dbport):
html += "</table>"
return {"KEY_ISSUESLIST":html,"KEY_ISSUES":str(i)}
return ({"KEY_ISSUESLIST":html,"KEY_ISSUES":str(i)},[])

View File

@ -2,7 +2,7 @@ import urllib
import json
def replacedict(keys,dbport):
def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, keysToUrl, pickKeys, clean
@ -41,6 +41,8 @@ def replacedict(keys,dbport):
#imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image")
else:
imgurl = ""
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
# build list
@ -54,5 +56,7 @@ def replacedict(keys,dbport):
html += "</tr>"
html += "</table>"
return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}
replace = {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}
return (replace,pushresources)

View File

@ -12,13 +12,15 @@ def getpictures(ls,result,tracks=False):
for element in getArtistsInfo(ls):
result.append(element.get("image"))
def replacedict(keys,dbport):
def instructions(keys,dbport):
from utilities import getArtistsInfo, getTracksInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean, getTimeDesc
max_show = 15
posrange = ["#" + str(i) for i in range(1,max_show)]
#clean(keys)
#timekeys = pickKeys(keys,"since","to","in")
#limitkeys = pickKeys(keys)
@ -91,11 +93,14 @@ def replacedict(keys,dbport):
t1.join()
t2.join()
t3.join()
pushresources = [{"file":img,"type":"image"} for img in artistimages + trackimages + scrobbleimages if img.startswith("/")]
return {"KEY_ARTISTIMAGE":artistimages,"KEY_ARTISTNAME":artisttitles,"KEY_ARTISTLINK":artistlinks,"KEY_POSITION_ARTIST":posrange,
replace = {"KEY_ARTISTIMAGE":artistimages,"KEY_ARTISTNAME":artisttitles,"KEY_ARTISTLINK":artistlinks,"KEY_POSITION_ARTIST":posrange,
"KEY_TRACKIMAGE":trackimages,"KEY_TRACKNAME":tracktitles,"KEY_TRACKLINK":tracklinks,"KEY_POSITION_TRACK":posrange,
"KEY_SCROBBLES_TODAY":scrobbles_today,"KEY_SCROBBLES_MONTH":scrobbles_month,"KEY_SCROBBLES_YEAR":scrobbles_year,"KEY_SCROBBLES_TOTAL":scrobbles_total,
"KEY_SCROBBLE_TIME":scrobbletimes,"KEY_SCROBBLE_ARTISTS":scrobbleartists,"KEY_SCROBBLE_TITLE":scrobbletracklinks,"KEY_SCROBBLE_IMAGE":scrobbleimages}
return (replace,pushresources)

View File

@ -2,7 +2,7 @@ import urllib
import json
def replacedict(keys,dbport):
def instructions(keys,dbport):
from utilities import getArtistInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean
@ -18,6 +18,7 @@ def replacedict(keys,dbport):
info = getArtistInfo(topartist)
imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
# get total amount of scrobbles
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
@ -44,5 +45,6 @@ def replacedict(keys,dbport):
i += 1
html += "</table>"
return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_ARTISTLIST":html}
replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_ARTISTLIST":html}
return (replace,pushresources)

View File

@ -2,7 +2,7 @@ import urllib
import json
def replacedict(keys,dbport):
def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean
@ -26,6 +26,8 @@ def replacedict(keys,dbport):
#topartist = charts[0]["track"]["artists"][0] #for now
info = getTrackInfo(charts[0]["track"]["artists"],charts[0]["track"]["title"])
imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
@ -52,5 +54,7 @@ def replacedict(keys,dbport):
i += 1
html += "</table>"
return {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring}
replace = {"KEY_TOPARTIST_IMAGEURL":imgurl,"KEY_SCROBBLES":str(scrobbles),"KEY_TRACKLIST":html,"KEY_LIMITS":limitstring}
return (replace,pushresources)

View File

@ -2,7 +2,7 @@ import urllib
import json
def replacedict(keys,dbport):
def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, getTimeDesc
@ -10,7 +10,7 @@ def replacedict(keys,dbport):
limitkeys = pickKeys(keys,"artist","title")
info = getTrackInfo(keys.getall("artist"),keys.get("title"))
imgurl = info.get("image")
desc = info.get("info")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/trackinfo?" + keysToUrl(limitkeys))
db_data = json.loads(response.read())
@ -34,4 +34,7 @@ def replacedict(keys,dbport):
html += "</table>"
return {"KEY_TRACKTITLE":limitkeys.get("title"),"KEY_ARTISTS":artistLinks(limitkeys.getall("artist")),"KEY_SCROBBLES":scrobblesnum,"KEY_IMAGEURL":imgurl,"KEY_SCROBBLELINK":keysToUrl(limitkeys),"KEY_SCROBBLELIST":html,"KEY_POSITION":pos}
replace = {"KEY_TRACKTITLE":limitkeys.get("title"),"KEY_ARTISTS":artistLinks(limitkeys.getall("artist")),"KEY_SCROBBLES":scrobblesnum,"KEY_IMAGEURL":imgurl,
"KEY_SCROBBLELINK":keysToUrl(limitkeys),"KEY_SCROBBLELIST":html,"KEY_POSITION":pos}
return (replace,pushresources)