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)
}
Related
I created this small HTTP Server in GO for static files:
func wrapHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(srw, r)
log.Printf("GET %s", r.RequestURI)
}
}
func main() {
http.HandleFunc("/", wrapHandler(
http.FileServer(http.Dir("/static"))
))
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
It works perfectly with React and Angular dist files (after transpile them). However if I already selected a route e.g. http://example.org:8080/customers and click on refresh in the browser I got a 404 page not found. That is the only situation where my code is failing.
It happens because on React and Angular index.html acts as a front controller and can handle the routes. However to make it working I need to internally redirect all not found requests to index.html.
Since is the angular/react handling the route I wouldn't like to create a http.HandleFunc for each route created in the angular/react. I would like to something similar to express :
app.use(express.static(path.join(__dirname, 'static')));
app.use("*",function(req,res){
res.sendFile(path.join(__dirname, 'static/index.html'));
});
or NGINX:
try_files $uri $uri/ index.html;
Any clues on how to do it in go?
It's important to understand how Go's http package handles route matching as it's a bit different from other languages/frameworks. HandleFunc uses ServeMux under the hood and (from the docs):
ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL. [emphasis mine]
Given this behavior, I would recommend creating explicit handlers for each folder in static (e.g. css/, js/), or putting all in a single subfolder of static, and then respond with your index.html file for all other requests (using the root route (/)). This works because requests with routes prefixed by /css/ or /js/ will more closely match the appropriate static handler routes, while all others will not and therefore only most closely match the root route. You just need to be sure not to create conflicting routes on your front-end.
This way any request explicitly for CSS/JS/image assets will be handled by serving the static directory and all other requests will be responded to with your React app.
Here's an example (leaving out your wrapHandler for simplicity):
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
})
http.Handle("/js/", http.FileServer(http.Dir("static")))
http.Handle("/css/", http.FileServer(http.Dir("static")))
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
or if you want to be a bit more explicit:
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
})
jsFs := http.FileServer(http.Dir("static/js"))
http.Handle("/js/", http.StripPrefix("/js", jsFs))
cssFs := http.FileServer(http.Dir("static/css"))
http.Handle("/css/", http.StripPrefix("/css", cssFs))
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
My website works pretty much the same way (using Vue instead of React).
An Improvement
As mentioned above, you might consider putting all you static assets in a sub folder of your current static/ folder. Consider structuring your files like this:
public/
index.html
static/
css/
js/
img/
Which is the default file structure for built React apps (but public is called build by default).
This will let you use the above approach in a more streamlined way since you'll only need one fileserver handler for all static assets. Then you can use the following code:
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/index.html")
})
fs := http.FileServer(http.Dir("public/static/"))
http.Handle("/static/", http.StripPrefix("/static", fs))
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
How would I go about creating a GET HTTP request mid application in GAE? I don't want to have it as a handler function, I simply have a URL that I need to get the response body from.
Use the urlfetch package.
ctx := appengine.NewContext(r) // r is the *http.Request arg to the handler
client := urlfetch.Client(ctx)
resp, err := client.Get("http://example.com")
if err != nil {
// handle the error
}
body := resp.Body // body is an io.Reader containing the response body
Here's a complete example.
I haven't used it so apologies if this doesn't work. According to GAE's docs you probably want to use urlfetch to get a *http.Client something like (N.B. the context package is standard in the just released Go 1.7):
import (
"context" // Go 1.7
// "golang.org/x/net/context" // Go < 1.7
"google.golang.org/appengine/urlfetch"
)
client := urlfetch.Client(context.Background())
resp, err := client.Get("http://example.com/")
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.
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)
}