Skip to content

Commit

Permalink
- bugfix
Browse files Browse the repository at this point in the history
- package doc
- README code snipped
  • Loading branch information
pakohan committed Aug 14, 2015
1 parent f674bf0 commit fbd7b19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
```
16 changes: 9 additions & 7 deletions seo4ajax.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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 },
}
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fbd7b19

Please sign in to comment.