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.
Related
I am getting following error when I am running "goapp serve myapp/" from Myproject folder inside src.
go-app-builder: Failed parsing input: parser: bad import "unsafe" in
github.com/gorilla/websocket/client.go from GOPATH
my file structure is some something like this
$GOPATH/src
|-github.com/gorilla/websocket
|-MyProject
|-myapp
|-app.yaml
|-mainApp.go (which contain init function and part of app package)
Please let me know how to correct this.
on a related subject, I read that google app provide websocket support only in paid app. Is there a way for me to test my website before getting into payment mode? Or is there a better option that google app engine?
I have a written a REST API using the Gorilla mux package and all data is stored in a MySQL database. I'm using the go-sql-driver/mysql package to access it.
When I compile and run the bin directly, it works as expected.
This is my first attepmt at deploying about to google cloud services, so I'm not familair with any special setup thatr needs to occur to make this work.
All code can be found at cobraclamp/hotswapper-api
NOTE: I'm aware that the InitDB in main has boilerplate credentials, they are properly set in the local and production projects
I haven't trawled through all your code, but I guess the problem is you initialise your router in main.
As per the App Engine go SDK docs and the Gorilla mux docs, you need to do this in an init() function:
Or, for Google App Engine, register it in a init() function:
func init() {
http.Handle("/", router)
}
If you don't do this I guess your app will get a 404 for any route.
Currently I'm using the bunch to do npm style type builds as well as using symbolic links in my web project to be able to do builds without have to pull from a git repository.
For example my directory structure is like the following
testapp/
.vendor
-controllers
--user_controller.go
-routers
--router.go
-models
--user.go
server.go
Bunchfile
so inside the .vendor/src directory
I also have
.vendor/src/example.com/tgo/testapp/routers
So if i don't want to have to duplicate my folders in the .vendor directory I will use a symbolic link - this works great when I do
ln -s ~/Documents/dev/go/testapp/ ~/Documents/dev/go/testapp/.vendor/src/example.com/tgo/
bunch go build
However for Google App Engine - Trying to see if this will work, haven't been able to figure it out yet.
Here is the code for server.go
package main
import (
"example.com/tgo/testapp/routers"
"github.com/codegangsta/negroni"
"net/http"
"log"
)
func init(){
//For Google App Engine
//settings.Init()
router := routers.InitRoutes()
n := negroni.Classic()
n.UseHandler(router)
http.Handle("/", n)
}
func main() {
router := routers.InitRoutes()
n := negroni.Classic()
n.UseHandler(router)
log.Println("Listening......")
http.ListenAndServe(":3001", n)
}
I actually figured it out the issue has to do with Assigning $GOPATH before you run the upload to App Engine.
So I just have a script that sets the environment Variable $GOPATH to the .vendor directory then run goapp serve or goapp deploy and everything works!
Look forward to moving everything over to 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.
I'm new to Google App Engine and Python. I've almost completed a project, but can't get the get_serving_url() function to work. I've stripped everything down to the most basic functionality, following the documentation. And yet I still get a 500 error from the server. Any thoughts? Here is the code:
from google.appengine.api import images
....
class Team(db.Model):
avatar = db.BlobProperty()
....
def to_dict(self):
....
image_url = images.get_serving_url(self.avatar.key())
The last line is the problem...commenting it out makes the app run fine. But it is copied almost directly from the documentation. I should note that I can download the avatar blob directly with:
class GetTeamAvatar(webapp2.RequestHandler):
def post(self):
team_id = self.request.get('team_id')
team = Team.get_by_id(long(team_id))
self.response.write(team.avatar)
So I know it is stored correctly. I do not have PIL on my machine...is that the issue? The datastore's image API says it has PIL locally so if I'm deploying my app it shouldn't matter, right? I have Python 3.3 and apparently PIL stopped at 2.6.
Python appengine run time is 2.7, (OK and 2.5) so don't even try to work with 3.x.
Secondly get_serving_URL is a method you call with a BlobStore entity key not a BlobProperty.
You are confusing two different things here.
I would concentrate on getting your code to run locally correctly under 2.7 first, and PIL is available for 2.7.
I'm very impressed if you're trying to deploy your app without even testing it locally.
One thing you'll need to do is make PIL available in your app.yaml via the libraries attribute.