Created
April 25, 2017 07:13
-
-
Save ekos/9f2afd9412adbd2c8559d98f208c34a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"github.com/go-martini/martini" | |
"encoding/json" | |
"appengine" | |
"appengine/urlfetch" | |
) | |
type data struct { | |
Id int `json:"id"` | |
Name string `json:"name"` | |
Value string `json:"value"` | |
} | |
func init() { | |
m := martini.Classic() | |
m.Use(AppEngine) | |
m.Get("/", func(c appengine.Context) string { | |
client := urlfetch.Client(c) | |
// {id: 1, name: "名前", value: "値"} のような値を返すと想定 | |
resp, err := client.Get("http://example.com/json") | |
if err != nil { | |
return err.Error() | |
} | |
defer resp.Body.Close() | |
result := make([]byte, resp.ContentLength) | |
resp.Body.Read(result) | |
var d data | |
json.Unmarshal(result, &d) | |
return fmt.Sprint("%s", d) | |
}) | |
} | |
func AppEngine(c martini.Context, r *http.Request) { | |
c.MapTo(appengine.NewContext(r), (*appengine.Context)(nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment