forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects.go
60 lines (53 loc) · 1.5 KB
/
redirects.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
package main
import (
"bytes"
"log"
"net/http"
)
var redirectPages = map[string]string{
"go-sdk.html": "go-toolchain.html",
"tool-gold.html": "tool-golds.html",
}
func (go101 *Go101) RedirectArticlePage(w http.ResponseWriter, r *http.Request, file string) bool {
redirectPage, ok := redirectPages[file]
if ok {
page, isLocal := go101.articlePages.Get(file), go101.IsLocalServer()
if page == nil {
pageParams := map[string]interface{}{
"RedirectPage": "/article/" + redirectPage,
"IsLocalServer": isLocal,
"Value": func() func(string, ...interface{}) interface{} {
var kvs = map[string]interface{}{}
return func(k string, v ...interface{}) interface{} {
if len(v) == 0 {
return kvs[k]
}
kvs[k] = v[0]
return ""
}
}(),
}
t := retrievePageTemplate(Template_Redirect, !isLocal)
var buf bytes.Buffer
if err := t.Execute(&buf, pageParams); err == nil {
page = buf.Bytes()
} else {
page = []byte(err.Error())
}
if !isLocal {
go101.articlePages.Set(file, page)
}
}
if len(page) == 0 { // blank page means page not found.
log.Printf("article page %s is not found", file)
//w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
http.Redirect(w, r, "/article/101.html", http.StatusNotFound)
} else if isLocal {
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
} else {
w.Header().Set("Cache-Control", "max-age=50000") // about 14 hours
}
w.Write(page)
}
return ok
}