aboutsummaryrefslogtreecommitdiffstats
path: root/__init__.py
blob: 462f382cea12389dd26a99b80c7c94b8039911fd (plain) (blame)
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
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 = '<html><head><title>' + TITLE + '</title></head><body>'
    for p in sorted(repos.keys()):
        o += '<p>go get ' + PACKAGE_HOST + '/' + p + '</p>'
    o += '</body></html>'
    return o

@app.route('/<path:path>', 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 '<html><head><title>' + path + ': ' + TITLE + '</title><meta name="go-import" content="' + PACKAGE_HOST + '/' + path + ' git ' + repo + '"><meta name="go-source" content="' + PACKAGE_HOST + '/' + path + ' _ ' + repo + '/tree{/dir} ' + repo + '/tree{/dir}/{file}#n{line}"></head><body>go get ' + PACKAGE_HOST + '/' + path + '</body></html>'

populate_repos()

if __name__ == '__main__':
    app.run(debug=True)