Setting up an App Engine Context in Go - google-app-engine

I post JSON to an app I registered at Google App Engine but I am baffled by the authentication process in my Go code to get it working in appengine:
func init() {
http.HandleFunc("/post", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
app := appengine.NewContext(r)
client := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(app, "https://www.googleapis.com/auth/bigquery"),
Base: &urlfetch.Transport{
Context: app,
},
},
}
log.Print(client)
}
In following the docs, I have reduced my problem to the code above which consistently gives me the following error:
2015/01/28 09:05:32 appengine: NewContext passed an unknown http.Request
I'd love some pointers as to how I can provide appengine with a "known" http.Request because ultimately I am trying to get to the storage api which also requires a valid context.

Does removing and re go getting google.golang.org/appengine from your GOPATH fixes the issue ?
Edit: Also, a colleague of mine said that after rebooting all went well.

Related

Go Handling HTTPS requests in Google App Engine

In GAE I just use a default domain name: https://*.appspot.com, so I don't need to generate self-signed certificates.
Google App Engine docs specify how app.yaml should be configured to serve SSL connections:
https://cloud.google.com/appengine/docs/standard/go/config/appref#handlers_secure
But to serve an HTTPS connection in Go I write the following code example where I need to specify the certificates' filenames:
import (
"net/http"
)
func main() {
go http.ListenAndServeTLS(Address, "cert.pem", "key.pem", nil)
}
I don't understand how in this case to serve SSL requests if I don't generate certificates myself.
You don't need to call http.ListenAndServeTLS on App Engine. If you have your app.yaml set up correctly, traffic will be served over SSL for you. A minimal App Engine app might be something like this:
package main
import (
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hi")
}

How to use Gin-gonic middleware with Google App Engine?

I'm using Gin-Gonic on a Google App Engine platform for my website.
Everything works fine but I'm starting to need to use some middleware.
When I try to use :
router.Use(MyMiddleware())
The middleware returned by MyMiddleware() doesn't seem to be run.
So my questions are :
Is it possible to use gin-gonic middlewares when working with GAE ?
If so, how can I achieve it ?
Thank you !
Here are my sources :
main.go :
func init() {
router := routes.Router()
// Set the config to the context
router.Use(SetConfiguration())
http.Handle("/", router)
}
func SetConfiguration() gin.HandlerFunc {
configuration := config.GetConfiguration()
return func(c *gin.Context) {
c.Set("config", configuration)
c.Next()
}
}
PS : routes.Router() simply set a router with gin.New() and add some routes.
The middleware route should be the first to be added before the other routes. See this file from a project of mine where I use CORS Middleware and Auth Middleware
https://github.com/wilsontamarozzi/panda-api/blob/master/routers/router.go

Is it possible to use the martini framework on app engine

Is it possible to use http://martini.codegangsta.io on Google's app engine? Does anyone have an example? Any gotcha's that I should be aware of before going down this path? Thanks in advance.
As long as martini does not use cgo or the unsafe and syscall package it should be fine.
The README of martini contains an example of using martini with GAE as #elithar pointed out:
package hello
import (
"net/http"
"github.com/go-martini/martini"
)
func init() {
m := martini.Classic()
m.Get("/", func() string {
return "Hello world!"
})
http.Handle("/", m)
}
Another example of using third-party packages with app engine is this randomly chosen app engine example project.

Google App Engine with GoLang "permission denied" error on Oauth2 autentification

Oauth2 autentification library
Works well on the localhost but crashes when is uploaded to the Google App Engine
oauth.go
When it does the line 250 of the above code
r, err := (&http.Client{Transport: t.transport()}).PostForm(t.TokenURL, v)
The error response is "permission denied"
From the api.go documentation :
Since the Google App Engine URL Fetch API requires a per-request
context, you must use the *plus.Service from within an HTTP handler.
This package provides the WithNoAuthPlus and WithOAuthPlus functions
which you can use to wrap your HTTP handlers to provide them with
fully initialized *plus.Services.
Example:
c := appengine.NewContext(r)
trans := &oauth.Transport{
Config: oauthConfig,
Transport: &urlfetch.Transport{Context: c},
}
trans.Exchange(code)
resp, err := trans.Client().Get(profileInfoURL)

GAE Golang - HTTP JSON RPC call works in the dev_appserver, but not on the App Engine?

I'm creating a Go Google App Engine application that will be making HTTP JSON RPC calls to a Bitcoin server. I got my code to work properly on the dev_appserver, but when deployed on GAE, the code seems to not work. I'm using a library available on GitHub, and call it like this:
func GetInfo(id interface{})(map[string]interface{}, os.Error){
resp, err:=httpjsonrpc.Call("user:pass#111.1.1.1:18332", "getinfo", id, nil)
if err!=nil{
log.Println(err)
return resp, err
}
return resp, err
}
Which when called should give:
map[proxy: keypoololdest:1.327368259e+09 blocks:45385 keypoolsize:101 connections:11 version:50200 difficulty:8.88353262 generate:false hashespersec:0 paytxfee:0 balance:0 genproclimit:-1 testnet:true errors:]
But on GAE calling the function seems to be causing an error. What part of the code could be working on dev_appserver, but fail on GAE?
You should make you are using urlfetch.Transport to make HTTP calls in production as described in urlfetch documentation.
Instead of doing:
resp, err := http.Post(address,
"application/json", strings.NewReader(string(data)))
You should be doing:
client := urlfetch.Client(context)
resp, error := client.Post(address,
"application/json", strings.NewReader(string(data)))
As you can see in the implementation, urlfetch.Client is just a shortcut to construct an http.Client that uses urlfetch.Transport.

Resources