From fbd7b19b3332e33c9462b189e2b21bf5312ee5b5 Mon Sep 17 00:00:00 2001 From: Patrick Kohan Date: Fri, 14 Aug 2015 16:04:38 +0200 Subject: [PATCH] - bugfix - package doc - README code snipped --- README.md | 13 +++++++++++++ seo4ajax.go | 16 +++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3b761b6..a315c8f 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,16 @@ Golang connector for Seo4Ajax [GoDoc](http://godoc.org/github.com/justwatchcom/go-seo4ajax). Code and tests is highly adapted from [connect middleware](https://www.npmjs.com/package/connect-s4a). + +``` +http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if seo4ajax.IsPrerender(r) { + err := seo4ajax.GetPrerenderedPage(w, r) + if err != nil { + log.Print(err.Error()) + } + } else { + fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) + } +}) +``` diff --git a/seo4ajax.go b/seo4ajax.go index e2fd253..3133409 100644 --- a/seo4ajax.go +++ b/seo4ajax.go @@ -1,3 +1,7 @@ +/* + Package seo4ajax provides a library for accessing the SEO4Ajax prerender service. + Before using, you need to set ServerIp to a valid IP address. +*/ package seo4ajax import ( @@ -33,9 +37,7 @@ var ( token = os.Getenv("SEO4AJAX_TOKEN") client = &http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { - return errRedirect - }, + CheckRedirect: func(req *http.Request, via []*http.Request) error { return errRedirect }, } ) @@ -101,12 +103,12 @@ func GetPrerenderedPage(w http.ResponseWriter, req *http.Request) (err error) { } defer resp.Body.Close() - for header, val := range resp.Header { - w.Header()[header] = val - } - switch resp.StatusCode { case 200: + for header, val := range resp.Header { + w.Header()[header] = val + } + _, err = io.Copy(w, resp.Body) case 302: http.Redirect(w, req, resp.Header.Get("Location"), resp.StatusCode)