forked from benrowe/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_netlify_redirects.go
51 lines (45 loc) · 1.07 KB
/
gen_netlify_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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/kjk/u"
)
const (
// https://www.netlify.com/docs/headers-and-basic-auth/#custom-headers
netlifyHeaders = `
# long-lived caching
/s/*
Cache-Control: max-age=31536000
/*
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
`
)
func genNetlifyHeaders() {
path := filepath.Join("www", "_headers")
err := ioutil.WriteFile(path, []byte(netlifyHeaders), 0644)
u.Must(err)
}
// TODO: this should be in 404.html for each book
func genNetlifyRedirectsForBook(b *Book) []string {
var res []string
// catch-all redirect for all other missing pages
s := fmt.Sprintf(`/essential/%s/* /essential/%s/404.html 404`, b.Dir, b.Dir)
res = append(res, s)
//res = append(res, "")
return res
}
func genNetlifyRedirects(books []*Book) {
var a []string
for _, b := range books {
ab := genNetlifyRedirectsForBook(b)
a = append(a, ab...)
}
s := strings.Join(a, "\n")
path := filepath.Join("www", "_redirects")
err := ioutil.WriteFile(path, []byte(s), 0644)
u.Must(err)
}