from os.path import join, dirname, abspath from flask import Flask, request, redirect, abort TITLE = 'zx2c4 golang repo' PACKAGE_HOST = 'golang.zx2c4.com' DEFAULT_REDIRECT = 'https://git.zx2c4.com/' app = Flask(__name__) repos = {} def populate_repos(): global repos with open(join(dirname(abspath(__file__)), 'repos.txt'), 'r') as f: for l in f: s = l.split('\t', 1) if len(s) != 2: continue s[0] = s[0].rstrip('/') s[1] = s[1].rstrip('\n') if len(s[0]) == 0 or len(s[1]) == 0: continue repos[s[0]] = s[1] def is_goget(): return request.args.get('go-get') == '1' @app.route('/') def default(): if is_goget(): return abort(404) o = '' + TITLE + '' for p in sorted(repos.keys()): o += '

go get ' + PACKAGE_HOST + '/' + p + '

' o += '' return o @app.route('/', methods=['GET']) def goget(path): while True: if len(path) == 0: if is_goget(): return abort(404) else: return redirect(DEFAULT_REDIRECT, code=302) if path in repos: break path = dirname(path) repo = repos[path] if not is_goget(): return redirect(repo, code=302) return '' + path + ': ' + TITLE + 'go get ' + PACKAGE_HOST + '/' + path + '' populate_repos() if __name__ == '__main__': app.run(debug=True)