Static pages return 404 in Google App Engine

83 views
Skip to first unread message

hairyCode via StackOverflow

unread,
Feb 25, 2016, 4:20:08 PM2/25/16
to google-appengin...@googlegroups.com

I've been testing out the Google App Engine SDK using GoLang and I'm having issues serving a static html page. If I add the content in the app.yaml under handlers that is fine but when trying to route it from inside my Go application; trying out the url "http://localhost:8080/tr" the page returns 404.

My file system is setup as

/main.go
/app.yaml
/testRoute.html

My main app.go looks like this

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

func init() {
    r := mux.NewRouter()
    r.HandleFunc("/", index)
    r.HandleFunc("/tr", testRoute)
    http.Handle("/", r)
}

func index(w http.ResponseWriter, r *http.Request) {
   //No Issues here 
   fmt.Fprint(w, "Main Index.")
}

func testRoute(w http.ResponseWriter, r *http.Request) { 
    http.FileServer(http.Dir("testRoute.html")).ServeHTTP(w, r)
}



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/35638518/static-pages-return-404-in-google-app-engine

hairyCode via StackOverflow

unread,
Feb 25, 2016, 8:35:06 PM2/25/16
to google-appengin...@googlegroups.com

I fixed this by using the ServeFile method; this also works with folders (eg "/assets/testRoute.html")

func testRoute(w http.ResponseWriter, r *http.Request) { 
    http.ServeFile(w, r, "testRoute.html")
}


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/35638518/static-pages-return-404-in-google-app-engine/35641741#35641741

icza via StackOverflow

unread,
Feb 26, 2016, 2:20:04 AM2/26/16
to google-appengin...@googlegroups.com

You shouldn't use Go handlers to serve static files (unless you want to incorporate other logic such as advanced logging or counting).

You may define static file handlers in your app's configuration file app.yaml. This is detailed in the official docs: Static file handlers

Static files are files to be served directly to the user for a given URL, such as images, CSS stylesheets, or JavaScript source files. Static file handlers describe which files in the application directory are static files, and which URLs serve them.

For efficiency, App Engine stores and serves static files separately from application files. Static files are not available in the application's file system by default. This can be changed by setting the application_readable option to true.

Static file handlers can be defined in two ways: as a directory structure of static files that maps to a URL path, or as a pattern that maps URLs to specific files.

To make AppEngine automatically serve a static file, add this entry to your app.yaml:

- url: /tr
  static_files: testRoute.html
  upload: testRoute.html

To make a whole directory of static files to be served automatically, add this entry to app.yaml:

- url: /assets
  static_dir: assets


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/35638518/static-pages-return-404-in-google-app-engine/35645698#35645698

hairyCode via StackOverflow

unread,
Feb 26, 2016, 2:25:07 AM2/26/16
to google-appengin...@googlegroups.com

I've been testing out the Google App Engine SDK using GoLang and I'm having issues serving a static html page. If I add the content in the app.yaml under handlers that is fine but when trying to route it from inside my Go application; trying out the url http://localhost:8080/tr the page returns 404.

My file system is setup as:

/main.go
/app.yaml
/testRoute.html

My main app.go looks like this:

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux
"
)

func init() {
    r := mux.NewRouter()
    r.HandleFunc("/", index)
    r.HandleFunc("/tr", testRoute)
    http.Handle("/", r)
}

func index(w http.ResponseWriter, r *http.Request) {
   //No Issues here 
   fmt.Fprint(w, "Main Index.")
}

func testRoute(w http.ResponseWriter, r *http.Request) { 
    http.FileServer(http.Dir("testRoute.html")).ServeHTTP(w, r)
}

icza via StackOverflow

unread,
Feb 26, 2016, 2:25:09 AM2/26/16
to google-appengin...@googlegroups.com

You shouldn't use Go handlers to serve static files (unless you want to incorporate other logic such as advanced logging or counting).

You may define static file handlers in your app's configuration file app.yaml. This is detailed in the official docs: Static file handlers

Static files are files to be served directly to the user for a given URL, such as images, CSS stylesheets, or JavaScript source files. Static file handlers describe which files in the application directory are static files, and which URLs serve them.

For efficiency, App Engine stores and serves static files separately from application files. Static files are not available in the application's file system by default. This can be changed by setting the application_readable option to true.

Static file handlers can be defined in two ways: as a directory structure of static files that maps to a URL path, or as a pattern that maps URLs to specific files.

To make AppEngine automatically serve a static file, add this entry to your app.yaml:

- url: /tr
  static_files: testRoute.html
  upload: testRoute.html

To make a whole directory of static files (including subfolders, recursively) to be served automatically, add this entry to app.yaml:

- url: /assets
  static_dir: assets


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/35638518/static-pages-return-404-in-google-app-engine/35645698#35645698
Reply all
Reply to author
Forward
0 new messages