I built a simple web app using google app engine and golang. in code below, I use fmt.Println twice to print out somehting for debugging purpose. I have no problem running the app. everything works except nothing print out on the terminal.
func HomeHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q := datastore.NewQuery("Post").Ancestor(goblogKey(c)).Order("-CreatedOn").Limit(10)
//posts := make([]entity.Post, 0, 10)
var posts []entity.Post
if _, err := q.GetAll(c, &posts); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(string(len(posts)) + "...........")
postList := []dto.Post{}
for _, val := range posts {
newpost := dto.Post{
Post: val,
BodyHTML: template.HTML(val.Body),
}
fmt.Println(val.Title)
postList = append(postList, newpost)
}
page := dto.PageData{Title: "Home", Posts: postList}
templates.ExecuteTemplate(w, "index", page)
}
In the real appengine enviroment you can't se anything output to stdout.
Appengine context give you away to log (that you can check in you appengine admin's page and in console playground).
func HomeHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c.Debugf("The message: %s", "foo")
...
Read more: https://developers.google.com/appengine/docs/go/reference#Context
standard i/o Or Error is used communicate with app server used by the devleoper. In production system there's no meaning of using standard i/o. In production systems log is used to track the results. In app engine there's some limitations. like fmt, socket etc.
Its always better to use log when testing or running program in remote server.
Related
I would like to get Stackdriver Trace to work on the new Go 1.11 standard runtime on Google App Engine. This is all still labeled as beta so maybe it just doesn't quite work yet. However, I followed the step-by-step directions and it isn't working. I deployed the code (almost) exactly as it is listed in the link and my traces are flat (i.e. doesn't include the waterfall view I would expect with the incoming request at the top and the outgoing request nested underneath).
Sample code
package main
import (
"fmt"
"log"
"net/http"
"os"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
)
func main() {
// Create and register a OpenCensus Stackdriver Trace exporter.
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
})
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
client := &http.Client{
Transport: &ochttp.Transport{
// Use Google Cloud propagation format.
Propagation: &propagation.HTTPFormat{},
},
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req, _ := http.NewRequest("GET", "https://www.google.com/robots.txt", nil)
// The trace ID from the incoming request will be
// propagated to the outgoing request.
req = req.WithContext(r.Context())
// The outgoing request will be traced with r's trace ID.
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
fmt.Fprint(w, "OK")
})
http.Handle("/foo", handler)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), &ochttp.Handler{}))
}
Sample trace:
As written in the reply comment in the original question, can you try the config for sampling?: trace.AlwaysSample()
You can find some comments about sampling rate in OpenCensus Trace documentation and godoc of OpenCensus Trace library:
By default, traces will be sampled relatively rarely. To change the sampling frequency for your entire program, call ApplyConfig. Use a ProbabilitySampler to sample a subset of traces, or use AlwaysSample to collect a trace on every run:
When i tried to implement push notification in golang App Engine using onesignal enviorment.But iam getting error "http.DefaultTransport and http.DefaultClient are not available in App Engine".This is my code,
func (c *PushNotificationController) CreateNotification() {
client := onesignal.NewClient(nil)
client.AppKey = "MyAppKey"
client.UserKey = "MyUserKey"
notifID := CreateNotifications(client)
log.Println(notifID)
}
func CreateNotifications(client *onesignal.Client) string {
playerID := "SamplePlayerId" // valid
notificationReq := &onesignal.NotificationRequest{
AppID: "MyAppKey",
Contents: map[string]string{"en": "English message"},
IsIOS: true,
IncludePlayerIDs: []string{playerID},
}
if createRes, res, err := client.Notifications.Create(notificationReq){
if err != nil {
log.Fatal(err)
}
return createRes.ID
}
...
}
Use http on appengine, you have to use urlfetch.
https://cloud.google.com/appengine/docs/standard/go/urlfetch/reference
i.e. the package you use doesn't support appengine.
A similar issue has been addressed by Robby Colvin in this blog
It explains how to make a third party package run in such conditions. Hoping this will help.
I have some fairly simple Go code running in AppEngine that should be using OAuth2 to fetch the list of files from the user's account. It seems to initialize the service OK but when it tries to fetch the file list, I get this error:
OAuthError: RoundTrip: no Token supplied
package foo
import (
"appengine"
"appengine/urlfetch"
"code.google.com/p/goauth2/oauth"
"code.google.com/p/google-api-go-client/drive/v2"
"fmt"
"net/http"
)
var config = &oauth.Config{
ClientId: "(redacted).apps.googleusercontent.com",
ClientSecret: "REDACTED",
Scope: "https://www.googleapis.com/auth/drive",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
func init() {
http.HandleFunc("/", home)
}
func home(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
transport := &oauth.Transport{
Config: config,
Transport: &urlfetch.Transport{Context: c}}
svc, err := drive.New(transport.Client())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
q := svc.Files.List()
_, err = q.Do()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Success!")
}
I cannot figure out what I'm doing wrong here. Any help would be kindly appreciated.
This page is kinda old but it outlines the steps nicely with Go code.
http://golangtutorials.blogspot.com/2011/11/oauth2-3-legged-authorization-in-go-web.html
The token configuration is not enough; you first have to get a valid access token with the following steps:
Redirect the user to the page returned by AuthCodeURL. The user will be shown the name of your application and the requested permissions.
If the user grants the permissions, they will be redirected to the RedirectURL you gave in the configuration. The URL will contain a query parameter named code.
Retrieve the code parameter and pass it to Exchange. If everything went well, the requests should now be authenticated properly.
I am trying to upload a file in my GAE app. How do I the upload of a file in Google App Engine using Go and using the r.FormValue()?
You have to go through the Blobstore Go API Overview to get an idea and there is a full example on how could you store & serve user data on Google App Engine using Go.
I would suggest you to do that example in a completely separate application, so you'll be able to experiment with it for a while before trying to integrate it to your already existing one.
I managed to solve my problem by using the middle return param, "other". These code below are inside the upload handler
blobs, other, err := blobstore.ParseUpload(r)
Then assign corresponding formkey
file := blobs["file"]
**name := other["name"]** //name is a form field
**description := other["description"]** //descriptionis a form field
And use it like this in my struct value assignment
newData := data{
Name: **string(name[0])**,
Description: **string(description[0])**,
Image: string(file[0].BlobKey),
}
datastore.Put(c, datastore.NewIncompleteKey(c, "data", nil), &newData )
Not 100% sure this is the right thing but this solves my problem and it is now uploading the image to blobstore and saving other data and blobkey to datastore.
Hope this could help others too.
I have tried the full example from here https://developers.google.com/appengine/docs/go/blobstore/overview, and it worked fine doing the upload in blobstore and serving it.
But inserting extra post values to be saved somewhere in the datastore erases the values of "r.FormValue() "? Please refer to the code below
func handleUpload(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
//tried to put the saving in the datastore here, it saves as expected with correct values but would raised a server error.
blobs, _, err := blobstore.ParseUpload(r)
if err != nil {
serveError(c, w, err)
return
}
file := blobs["file"]
if len(file) == 0 {
c.Errorf("no file uploaded")
http.Redirect(w, r, "/", http.StatusFound)
return
}
// a new row is inserted but no values in column name and description
newData:= data{
Name: r.FormValue("name"), //this is always blank
Description: r.FormValue("description"), //this is always blank
}
datastore.Put(c, datastore.NewIncompleteKey(c, "Data", nil), &newData)
//the image is displayed as expected
http.Redirect(w, r, "/serve/?blobKey="+string(file[0].BlobKey), http.StatusFound)
}
Is it not possible to combine the upload with regular data? How come the values of r.FormValue() seems to disappear except for the file (input file type)? Even if I would have to force upload first before associating the blobkey, as the result of the upload, to other data, it would not be possible since I could not pass any r.FormValue() to the upload handler(which like I said becomes empty, or would raised an error when accessed prior the blobs, _, err := blobstore.ParseUpload(r) statement). I hope someone could help me solve this problem. Thank you!
In addition to using the Blobstore API, you can just use the Request.FormFile() method to get the file upload content. Use net\http package documentation for additional help.
Using the Request directly allows you to skip setting up an blobstore.UploadUrl() before handling the upload POST message.
A simple example would be:
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// Create an App Engine context.
c := appengine.NewContext(r)
// use FormFile()
f, _, err := r.FormFile("file")
if err != nil {
c.Errorf("FormFile error: %v", err)
return
}
defer f.Close()
// do something with the file here
c.Infof("Hey!!! got a file: %v", f)
}
Where can one read logs created by calling function:
log.Print("Message")
The tab "Logs" under Main seems to only display information about what URLs were called, but without any debug information that would be displayed by the application.
As described in the documentation, you should use the Context interface to log instead of log, if you want your logs to show up in the console.
c := appengine.NewContext(r)
c.Infof("Requested URL: %v", r.URL)
If you are using the new App Engine package google.golang.org/appengine, in the README:
Logging methods that were on appengine.Context are now functions in google.golang.org/appengine/log
So you should use
c := appengine.NewContext(r)
log.Infof(c, "Requested URL: %v", r.URL)
The same context object must be passed around in other method calls.
Here is an example:
func handleSign(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
writeError(c, err)
return
}
}
func writeError(c appengine.Context, err os.Error) {
c.Errorf("%v", err)
}