Skip to content

Instantly share code, notes, and snippets.

@ekos
Created April 25, 2017 07:13
Show Gist options
  • Save ekos/9f2afd9412adbd2c8559d98f208c34a5 to your computer and use it in GitHub Desktop.
Save ekos/9f2afd9412adbd2c8559d98f208c34a5 to your computer and use it in GitHub Desktop.
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