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("")
@webserver.route("/") @webserver.route("/")
def mainpage(): 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 # 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>") @webserver.route("/<name>")
def static_html(name): def static_html(name):
linkheaders = ["</maloja.css>; rel=preload; as=style"]
keys = removeIdentical(FormsDict.decode(request.query)) keys = removeIdentical(FormsDict.decode(request.query))
# If a python file exists, it provides the replacement dict for the html file # If a python file exists, it provides the replacement dict for the html file
if os.path.exists("website/" + name + ".py"): 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: with open("website/" + name + ".html") as htmlfile:
html = htmlfile.read() html = htmlfile.read()
for k in txt_keys: for k in txt_keys:
@ -128,11 +136,14 @@ def static_html(name):
for element in txt_keys[k]: for element in txt_keys[k]:
html = html.replace(k,element,1) html = html.replace(k,element,1)
else: else:
html = html.replace(k,txt_keys[k]) html = html.replace(k,txt_keys[k])
response.set_header("Link",",".join(linkheaders))
return html return html
# Otherwise, we just serve the html file # Otherwise, we just serve the html file
response.set_header("Link",",".join(linkheaders))
return static_file("website/" + name + ".html",root="") return static_file("website/" + name + ".html",root="")
#set graceful shutdown #set graceful shutdown

View File

@ -2,7 +2,7 @@ import urllib
import json import json
def replacedict(keys,dbport): def instructions(keys,dbport):
from utilities import getArtistInfo from utilities import getArtistInfo
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink
@ -10,6 +10,7 @@ def replacedict(keys,dbport):
info = getArtistInfo(keys["artist"]) info = getArtistInfo(keys["artist"])
imgurl = info.get("image") imgurl = info.get("image")
#desc = info.get("info") #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"])) response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"]))
db_data = json.loads(response.read()) db_data = json.loads(response.read())
@ -43,5 +44,8 @@ def replacedict(keys,dbport):
html += "</tr>" html += "</tr>"
html += "</table>" 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 import json
from htmlgenerators import artistLink from htmlgenerators import artistLink
def replacedict(keys,dbport): def instructions(keys,dbport):
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/issues") response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/issues")
db_data = json.loads(response.read()) db_data = json.loads(response.read())
@ -42,4 +42,4 @@ def replacedict(keys,dbport):
html += "</table>" 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 import json
def replacedict(keys,dbport): def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, keysToUrl, pickKeys, clean 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") #imgurl = getArtistInfo(scrobbles[0]["artists"][0]).get("image")
else: else:
imgurl = "" imgurl = ""
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
# build list # build list
@ -54,5 +56,7 @@ def replacedict(keys,dbport):
html += "</tr>" html += "</tr>"
html += "</table>" 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): for element in getArtistsInfo(ls):
result.append(element.get("image")) result.append(element.get("image"))
def replacedict(keys,dbport): def instructions(keys,dbport):
from utilities import getArtistsInfo, getTracksInfo from utilities import getArtistsInfo, getTracksInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean, getTimeDesc from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean, getTimeDesc
max_show = 15 max_show = 15
posrange = ["#" + str(i) for i in range(1,max_show)] posrange = ["#" + str(i) for i in range(1,max_show)]
#clean(keys) #clean(keys)
#timekeys = pickKeys(keys,"since","to","in") #timekeys = pickKeys(keys,"since","to","in")
#limitkeys = pickKeys(keys) #limitkeys = pickKeys(keys)
@ -91,11 +93,14 @@ def replacedict(keys,dbport):
t1.join() t1.join()
t2.join() t2.join()
t3.join() t3.join()
pushresources = [{"file":img,"type":"image"} for img in artistimages + trackimages + scrobbleimages if img.startswith("/")]
replace = {"KEY_ARTISTIMAGE":artistimages,"KEY_ARTISTNAME":artisttitles,"KEY_ARTISTLINK":artistlinks,"KEY_POSITION_ARTIST":posrange,
return {"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_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_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} "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 import json
def replacedict(keys,dbport): def instructions(keys,dbport):
from utilities import getArtistInfo from utilities import getArtistInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean
@ -18,6 +18,7 @@ def replacedict(keys,dbport):
info = getArtistInfo(topartist) info = getArtistInfo(topartist)
imgurl = info.get("image") imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
# get total amount of scrobbles # get total amount of scrobbles
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys)) response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/scrobbles?" + keysToUrl(timekeys,limitkeys))
@ -44,5 +45,6 @@ def replacedict(keys,dbport):
i += 1 i += 1
html += "</table>" 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 import json
def replacedict(keys,dbport): def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean 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 #topartist = charts[0]["track"]["artists"][0] #for now
info = getTrackInfo(charts[0]["track"]["artists"],charts[0]["track"]["title"]) info = getTrackInfo(charts[0]["track"]["artists"],charts[0]["track"]["title"])
imgurl = info.get("image") imgurl = info.get("image")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
@ -52,5 +54,7 @@ def replacedict(keys,dbport):
i += 1 i += 1
html += "</table>" 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 import json
def replacedict(keys,dbport): def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, getTimeDesc from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, getTimeDesc
@ -10,7 +10,7 @@ def replacedict(keys,dbport):
limitkeys = pickKeys(keys,"artist","title") limitkeys = pickKeys(keys,"artist","title")
info = getTrackInfo(keys.getall("artist"),keys.get("title")) info = getTrackInfo(keys.getall("artist"),keys.get("title"))
imgurl = info.get("image") 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)) response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/trackinfo?" + keysToUrl(limitkeys))
db_data = json.loads(response.read()) db_data = json.loads(response.read())
@ -34,4 +34,7 @@ def replacedict(keys,dbport):
html += "</table>" 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)