xref: /aosp_15_r20/external/perfetto/infra/ui.perfetto.dev/appengine/main.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1# Copyright (C) 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import flask
16import requests
17
18BUCKET_NAME = 'ui.perfetto.dev'
19
20REQ_HEADERS = [
21    'Accept',
22    # TODO(primiano): re-enable once the gzip handling outage fixed.
23    # 'Accept-Encoding',
24    # 'Cache-Control',
25]
26
27RESP_HEADERS = [
28    'Content-Type',
29    'Content-Encoding',
30    'Content-Length',
31    'Cache-Control',
32    'Date',
33    'Expires',
34]
35
36app = flask.Flask(__name__)
37
38
39# Redirect v1.2.3 to v.1.2.3/
40@app.route('/v<int:x>.<int:y>.<int:z>')
41def version_redirect(x, y, z):
42  return flask.redirect('/v%d.%d.%d/' % (x, y, z), code=302)
43
44
45# Serve the requests from the GCS bucket.
46@app.route('/', methods=['GET'])
47@app.route('/<path:path>', methods=['GET'])
48def main(path=''):
49  path = '/' + path
50  path += 'index.html' if path.endswith('/') else ''
51  req_headers = {}
52  for key in set(flask.request.headers.keys()).intersection(REQ_HEADERS):
53    req_headers[key] = flask.request.headers.get(key)
54  url = 'https://commondatastorage.googleapis.com/' + BUCKET_NAME + path
55  req = requests.get(url, headers=req_headers)
56  if (req.status_code != 200):
57    return flask.abort(req.status_code)
58  resp = flask.Response(req.content)
59  for key in set(req.headers.keys()).intersection(RESP_HEADERS):
60    resp.headers[key] = req.headers.get(key)
61  return resp
62
63
64if __name__ == '__main__':
65  # This is used when running locally only.
66  app.run(host='127.0.0.1', port=10000, debug=True)
67