-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview_on_demand.go
227 lines (201 loc) · 5.36 KB
/
preview_on_demand.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"time"
)
var (
gPreviewArticles *Articles
)
func serve404(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Path
path := filepath.Join("www", "404.html")
parts := strings.Split(uri[1:], "/")
if len(parts) > 2 && parts[0] == "essential" {
bookName := parts[1]
maybePath := filepath.Join("www", "essential", bookName, "404.html")
if fileExists(maybePath) {
fmt.Printf("'%s' exists\n", maybePath)
path = maybePath
} else {
fmt.Printf("'%s' doesn't exist\n", maybePath)
}
}
fmt.Printf("Serving 404 from '%s' for '%s'\n", path, uri)
d, err := ioutil.ReadFile(path)
if err != nil {
d = []byte(fmt.Sprintf("URL '%s' not found!", uri))
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusNotFound)
w.Write(d)
}
func writeHTMLHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
}
// tempRedirect gives a Moved Permanently response.
func doRedirect(w http.ResponseWriter, r *http.Request, newPath string, code int) {
if q := r.URL.RawQuery; q != "" {
newPath += "?" + q
}
w.Header().Set("Location", newPath)
if code == 0 {
code = http.StatusFound
}
w.WriteHeader(code)
}
func handleIndexOnDemand(w http.ResponseWriter, r *http.Request) {
fmt.Printf("uri: %s\n", r.URL.Path)
uri := r.URL.Path
nAgain := 0
again:
nAgain++
if nAgain > 3 {
fmt.Printf("Too many 200 redirects for '%s'\n", r.URL.Path)
serve404(w, r)
return
}
if tryServeKnown(w, r) {
return
}
if tryServeNotionCacheImg(w, r) {
return
}
if tryServeTagArchive(w, r) {
return
}
if tryServeArticle(w, r) {
return
}
for _, redir := range netlifyRedirects {
if redir.from == uri {
// this is internal rewrite
if redir.code == 200 {
fmt.Printf("Rewrite %s => %s\n", uri, redir.to)
uri = redir.to
r.URL.Path = uri
goto again
}
fmt.Printf("Redirect %s => %s, code: %d\n", uri, redir.to, redir.code)
doRedirect(w, r, redir.to, redir.code)
}
}
isDir := strings.HasSuffix(uri, "/")
fileName := filepath.FromSlash(uri[1:])
if isDir {
fileName = filepath.Join(fileName, "index.html")
}
path := filepath.Join("www", fileName)
if !fileExists(path) {
fmt.Printf("path '%s' for url '%s' doesn't exist\n", path, uri)
serve404(w, r)
return
}
fmt.Printf("Serving file: %s\n", path)
http.ServeFile(w, r, path)
}
var knownURLs = map[string]func(*Articles, io.Writer) error{
"/": genIndex,
"/changelog.html": genChangelog,
"/archives.html": genArchives,
"/sitemap.xml": genSitemap,
"/atom.xml": genAtom,
"/atom-all.xml": genAtomAll,
"/book/go-cookbook.html": genGoCookbook,
"/tools/generate-unique-id.html": genToolGenerateUniqueID,
"/tools/generate-unique-id": genToolGenerateUniqueID,
}
func tryServeKnown(w http.ResponseWriter, r *http.Request) bool {
uri := r.URL.Path
if fn := knownURLs[uri]; fn != nil {
writeHTMLHeaders(w)
logIfError(fn(gPreviewArticles, w))
return true
}
return false
}
func findArticleByID(store *Articles, id string) *Article {
for _, article := range store.articles {
if article.ID == id {
return article
}
}
return nil
}
func tryServeArticle(w http.ResponseWriter, r *http.Request) bool {
uri := r.URL.Path
articlePath := strings.TrimPrefix(uri, "/article/")
if uri == articlePath {
return false
}
articlePath = strings.Split(articlePath, "/")[0]
articleID := strings.TrimSuffix(articlePath, ".html")
article := findArticleByID(gPreviewArticles, articleID)
if article == nil {
fmt.Printf("Didn't find article with id '%s' for uri '%s'\n", articleID, uri)
return false
}
genArticle(article, w)
return true
}
// tag/<tagname>
func tryServeTagArchive(w http.ResponseWriter, r *http.Request) bool {
uri := r.URL.Path
tag := strings.TrimPrefix(uri, "/tag/")
if uri == tag {
return false
}
netlifyWriteArticlesArchiveForTag(gPreviewArticles, tag, w)
return true
}
// for /img/<name> check notion_cache/img
func tryServeNotionCacheImg(w http.ResponseWriter, r *http.Request) bool {
uri := r.URL.Path
imgName := strings.TrimPrefix(uri, "/img/")
if uri == imgName {
return false
}
path := filepath.Join("notion_cache", "img", imgName)
if !fileExists(path) {
return false
}
http.ServeFile(w, r, path)
return true
}
// https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
func makeHTTPServerOnDemand() *http.Server {
mux := &http.ServeMux{}
mux.HandleFunc("/", handleIndexOnDemand)
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second, // introduced in Go 1.8
Handler: mux,
}
return srv
}
func startPreviewOnDemand(articles *Articles) {
gPreviewArticles = articles
addAllRedirects(gPreviewArticles)
httpSrv := makeHTTPServerOnDemand()
httpSrv.Addr = "127.0.0.1:8183"
go func() {
err := httpSrv.ListenAndServe()
// mute error caused by Shutdown()
if err == http.ErrServerClosed {
err = nil
}
panicIfErr(err)
fmt.Printf("HTTP server shutdown gracefully\n")
}()
fmt.Printf("Started listening on %s\n", httpSrv.Addr)
openBrowser("http://" + httpSrv.Addr)
waitForCtrlC()
}