-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts.py
99 lines (67 loc) · 2.88 KB
/
fonts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
from google.appengine.ext.webapp import template
import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from datetime import datetime
from datetime import timedelta
from datetime import date
from google.appengine.api import memcache
class MainPage(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, {}))
class StaticCustomFileHandler(webapp.RequestHandler):
def get(self):
filename = self.request.path.split("/")[-1]
folder = "fonts"
fileContent = self.getFile(filename, folder)
if fileContent:
self.setHeaders(filename)
self.response.out.write(fileContent)
else:
self.error(404)
# HTTP Headers are set according to advice from:
# http://code.google.com/speed/page-speed/docs/caching.html
def setHeaders(self, filename):
current_time = datetime.utcnow()
expires_time = current_time + timedelta(days=365)
# As the content is static, the Last-Modified header shoudn't change, set to fixed date in the past.
last_modified = date(1980, 07, 10)
self.response.headers.add_header("Last-Modified", last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT'))
# Specify either Expires OR Cache-Control headers NOT both
# Setting Expires header to maximum values allowd by the RFC guidelines
self.response.headers.add_header("Expires", expires_time.strftime('%a, %d %b %Y %H:%M:%S GMT') )
self.response.headers['Cache-Control'] = 'public, max-age=315360000'
# Woff files will now work cross domain unless Access-Control-Allow-Origin is set
# You can set this to * (for request from any domain) or you can set your domain explicitly
self.response.headers.add_header("Access-Control-Allow-Origin", "*")
# Some font formats won't work unless Content-Type is set correctly.
fileExtension = filename.split(".")[-1]
contentType = {
'svg': 'image/svg+xml',
'eot': 'application/vnd.ms-fontobject',
'ttf': 'application/octet-stream',
'woff': 'application/x-woff'
}[fileExtension]
self.response.headers['Content-Type'] = contentType
def getFile(self, filename, folder):
file_data = memcache.get(filename)
if file_data is not None:
return file_data
else:
pathToFile = folder + "/" + filename
if os.path.exists(pathToFile):
file_data = open(pathToFile, "r").read()
memcache.add(filename, file_data)
return file_data
else:
return False
application = webapp.WSGIApplication(
[('/', MainPage),
(r'/fonts/.*', StaticCustomFileHandler)],
debug=False)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()