-
Notifications
You must be signed in to change notification settings - Fork 7
/
reader.go
102 lines (84 loc) · 2.2 KB
/
reader.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
91
92
93
94
95
96
97
98
99
100
101
102
package urlzap
import (
"context"
"errors"
"net/http"
"os"
"strings"
"text/template"
"golang.org/x/sync/errgroup"
)
// Callback Function which would be called once a key/value is read.
type Callback func(path, url string) error
// Mux defines interface used to register HTTP routes on a mux.
type Mux interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}
// HTMLFileCallback creates static files based on a HTML template.
func HTMLFileCallback(c Config, tmpl *template.Template) Callback {
outputPath := strings.TrimSuffix(c.Path, "/")
return func(path, url string) error {
fullpath := outputPath + "/" + path
if err := os.MkdirAll(fullpath, 0700); err != nil {
return err
}
w, err := os.Create(fullpath + "/index.html")
if err != nil {
return err
}
if c.DisableMetaFetch {
return tmpl.Execute(w, redirectHTMLArgs{Title: url, URL: url})
}
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
meta, err := GetMetaData(resp.Body)
if err != nil {
return err
}
return tmpl.Execute(w, redirectHTMLArgs{
URL: url,
Title: meta.Title,
MetaTags: meta.Tags,
})
}
}
// HTTPMuxCallback register redirect routes in a mux.
func HTTPMuxCallback(parent string, r Mux) Callback {
parent = strings.TrimSuffix(parent, "/")
return func(path, url string) error {
route := parent + "/" + path
r.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, url, 301)
})
return nil
}
}
func read(ctx context.Context, eg *errgroup.Group, path string, urls URLs, cb Callback) {
for k, v := range urls {
kk, vv := k, v
eg.Go(func() error {
id, ok := kk.(string)
if !ok {
return errors.New("malformated document")
}
switch val := vv.(type) {
case string:
if err := cb(path+id, val); err != nil {
return err
}
case URLs:
read(ctx, eg, path+id+"/", val, cb)
}
return nil
})
}
}
// Read parses all URLs and calls the callback once found a key (string) and value (url)
func Read(ctx context.Context, path string, urls URLs, cb Callback) error {
eg, ctx := errgroup.WithContext(ctx)
read(ctx, eg, path, urls, cb)
return eg.Wait()
}