-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect.go
90 lines (75 loc) · 2.34 KB
/
redirect.go
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
)
const (
base = "https://docs.djangoproject.com/ja/3.1/"
prefix = "/en/latest/"
suffix = ".html"
srcPrefix = "_sources/"
srcSuffix = ".txt"
)
func migrateUrl(s string) string {
m := map[string]string{
`howto/apache-auth/`: "howto/deployment/wsgi/apache-auth/",
`howto/deployment/fastcgi/`: "internals/deprecation/#deprecation-removed-in-1-9",
`howto/deployment/modpython/`: "internals/deprecation/#deprecation-removed-in-1-5",
`internals/committers/`: "internals/organization/#prerogatives",
`internals/documentation/`: "internals/contributing/writing-documentation/",
`obsolete/.*`: "internals/deprecation/",
`ref/authbackends/`: "topics/auth/customizing/#authentication-backends",
`ref/contrib/comments/.*`: "releases/1.8/#features-removed-in-1-8",
`ref/contrib/csrf/`: "ref/csrf/",
`ref/contrib/databrowse/`: "releases/1.4/#django-contrib-databrowse",
`ref/contrib/formtools/.*`: "releases/1.8/#removal-of-django-contrib-formtools",
`ref/contrib/localflavor/`: "internals/deprecation/#deprecation-removed-in-1-6",
`ref/contrib/webdesign/`: "releases/1.8/#django-contrib-webdesign",
`.*/generic-views.*/`: "topics/class-based-views/",
`releases/1.0-.*/`: "releases/1.0/",
`releases/1.1-.*/`: "releases/1.1/",
}
for k, v := range m {
if regexp.MustCompile(k).MatchString(s) {
return v
}
}
return s
}
func mapUrl(s string) string {
if !strings.HasPrefix(s, prefix) {
return base
}
// Remove .html and .txt
for _, _suffix := range []string{suffix, srcSuffix} {
if strings.HasSuffix(s, _suffix) {
s = strings.TrimSuffix(s, _suffix) + "/"
}
}
// Remove /en/latest and _sources
for _, _prefix := range []string{prefix, srcPrefix} {
if strings.HasPrefix(s, _prefix) {
s = strings.TrimPrefix(s, _prefix)
}
}
s = migrateUrl(s)
return base + s
}
func redirect(w http.ResponseWriter, r *http.Request) {
var s = mapUrl(r.URL.Path)
http.Redirect(w, r, s, 301)
}
func main() {
http.HandleFunc("/", redirect)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}