diff options
author | Feuerfuchs <git@feuerfuchs.dev> | 2019-06-20 15:47:04 +0200 |
---|---|---|
committer | Feuerfuchs <git@feuerfuchs.dev> | 2019-06-20 15:47:04 +0200 |
commit | de13213492ce02464c5ecd8544ea5ae6b33fa712 (patch) | |
tree | b227baa4ce533336fe1c8f1dbd58709d1c542714 | |
parent | Fix for unescaped HTML (diff) | |
download | gopherproxy-de13213492ce02464c5ecd8544ea5ae6b33fa712.tar.gz gopherproxy-de13213492ce02464c5ecd8544ea5ae6b33fa712.tar.bz2 gopherproxy-de13213492ce02464c5ecd8544ea5ae6b33fa712.zip |
Make output about ignored robots.txt optional
-rw-r--r-- | cmd/gopherproxy/main.go | 9 | ||||
-rw-r--r-- | gopherproxy.go | 8 |
2 files changed, 9 insertions, 8 deletions
diff --git a/cmd/gopherproxy/main.go b/cmd/gopherproxy/main.go index aced41b..e9dea18 100644 --- a/cmd/gopherproxy/main.go +++ b/cmd/gopherproxy/main.go | |||
@@ -10,14 +10,15 @@ import ( | |||
10 | var ( | 10 | var ( |
11 | // TODO: Allow config file and environment vars | 11 | // TODO: Allow config file and environment vars |
12 | // (opt -> env -> config -> default) | 12 | // (opt -> env -> config -> default) |
13 | bind = flag.String("bind", "0.0.0.0:8000", "[int]:port to bind to") | 13 | bind = flag.String("bind", "0.0.0.0:8000", "[int]:port to bind to") |
14 | robotsfile = flag.String("robots-file", "robots.txt", "robots.txt file") | 14 | robotsfile = flag.String("robots-file", "robots.txt", "robots.txt file") |
15 | uri = flag.String("uri", "floodgap.com", "<host>:[port] to proxy to") | 15 | robotsdebug = flag.Bool("robots-debug", false, "print output about ignored robots.txt") |
16 | uri = flag.String("uri", "floodgap.com", "<host>:[port] to proxy to") | ||
16 | ) | 17 | ) |
17 | 18 | ||
18 | func main() { | 19 | func main() { |
19 | flag.Parse() | 20 | flag.Parse() |
20 | 21 | ||
21 | // Use a config struct | 22 | // Use a config struct |
22 | log.Fatal(gopherproxy.ListenAndServe(*bind, *robotsfile, *uri)) | 23 | log.Fatal(gopherproxy.ListenAndServe(*bind, *robotsfile, *robotsdebug, *uri)) |
23 | } | 24 | } |
diff --git a/gopherproxy.go b/gopherproxy.go index 74f33bb..961806a 100644 --- a/gopherproxy.go +++ b/gopherproxy.go | |||
@@ -87,12 +87,12 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st | |||
87 | // to the request path and renders the content using the provided template. | 87 | // to the request path and renders the content using the provided template. |
88 | // The optional robots parameters points to a robotstxt.RobotsData struct | 88 | // The optional robots parameters points to a robotstxt.RobotsData struct |
89 | // to test user agents against a configurable robotst.txt file. | 89 | // to test user agents against a configurable robotst.txt file. |
90 | func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, styletext string, uri string) http.HandlerFunc { | 90 | func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, robotsdebug bool, styletext string, uri string) http.HandlerFunc { |
91 | return func(w http.ResponseWriter, req *http.Request) { | 91 | return func(w http.ResponseWriter, req *http.Request) { |
92 | agent := req.UserAgent() | 92 | agent := req.UserAgent() |
93 | path := strings.TrimPrefix(req.URL.Path, "/") | 93 | path := strings.TrimPrefix(req.URL.Path, "/") |
94 | 94 | ||
95 | if robotsdata != nil && !robotsdata.TestAgent(path, agent) { | 95 | if robotsdata != nil && robotsdebug && !robotsdata.TestAgent(path, agent) { |
96 | log.Printf("UserAgent %s ignored robots.txt", agent) | 96 | log.Printf("UserAgent %s ignored robots.txt", agent) |
97 | } | 97 | } |
98 | 98 | ||
@@ -203,7 +203,7 @@ func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc { | |||
203 | // specified by the request. The robots argument is a pointer to | 203 | // specified by the request. The robots argument is a pointer to |
204 | // a robotstxt.RobotsData struct for testing user agents against | 204 | // a robotstxt.RobotsData struct for testing user agents against |
205 | // a configurable robots.txt file. | 205 | // a configurable robots.txt file. |
206 | func ListenAndServe(bind, robotsfile, uri string) error { | 206 | func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error { |
207 | var ( | 207 | var ( |
208 | tpl *template.Template | 208 | tpl *template.Template |
209 | robotsdata *robotstxt.RobotsData | 209 | robotsdata *robotstxt.RobotsData |
@@ -265,7 +265,7 @@ func ListenAndServe(bind, robotsfile, uri string) error { | |||
265 | log.Fatal(err) | 265 | log.Fatal(err) |
266 | } | 266 | } |
267 | 267 | ||
268 | http.HandleFunc("/", GopherHandler(tpl, robotsdata, styletext, uri)) | 268 | http.HandleFunc("/", GopherHandler(tpl, robotsdata, robotsdebug, styletext, uri)) |
269 | http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) | 269 | http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) |
270 | http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) | 270 | http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) |
271 | http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) | 271 | http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) |