cbll
cbll

Reputation: 7219

Google App Engine Golang returns 404 page not found

app.yaml

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

My main.go file:

package main

import (
    "fmt"
    "github.com/cbll/stockmarket-service/lib"
    "net/http"
    "log"
    "google.golang.org/appengine"
)

func main() {
    appengine.Main()
    fmt.Println("Starting the application...")
    // Run application until halted
    for {
        go lib.GetStockData()
        StockController()
    }
}


// Expose controller at http://localhost:8080/
func StockController() {
    var port = ":8080"

    fmt.Println("Starting web server listening on port", port)

    http.HandleFunc("/", lib.StocksHandler)
    log.Fatal(http.ListenAndServe(port, nil))
}

If I comment out the appengine.Main(), I can run it locally without any issues. It starts on port 8080.

gcloud app deploy works without issues and will correctly deploy the app to google cloud.

However, gcloud app browse which takes me to the site returns 404 page not found. And indeed, checking the logs with gcloud app logs read:

2018-01-06 00:04:18 default[20180106t010155]  + exec app
2018-01-06 00:08:32 default[20180106t010155]  "GET /" 404

I'm a bit unsure of where to troubleshoot it here. It works locally, and it specifies :8080 as the port as it should(as far as I know?), and imports the appengine main method. What could I be missing?

Edit:

Changing the handlers to

- url: /

Results in:

2018-01-06 00:17:30 default[20180106t010915]  "GET / HTTP/1.1" 404

.. instead?

Upvotes: 0

Views: 865

Answers (1)

Thundercat
Thundercat

Reputation: 120931

The standard environment provides the main program and runs the http server. Put your initialization code in an init() function. Do not use package main.

package app

import (
    "github.com/cbll/stockmarket-service/lib"
    "net/http"
)

func init() {
    go lib.GetStockData()
    http.HandleFunc("/", lib.StocksHandler)
}

Upvotes: 3

Related Questions