Sahil Chopra
Sahil Chopra

Reputation: 11

Google App Engine Custom Domain - Routing in Go

I've been utilizing the url provided to me by the google-app-engine, i.e. "projectname-id.appspot.com" for my project. I had previously purchased a custom domain from GoDaddy, and I followed the steps at the following link to verify ownership of the domain name within the Google Cloud Platform, add the domain to my Google App Engine Project, and update my DNS settings in GoDaddy to point to the listed CNAME/server combination. https://cloud.google.com/appengine/docs/go/console/using-custom-domains-and-ssl

When I visit my custom domain, a website page is served, but it's always a 404. The "projectname-id.appspot.com" url still works correctly, and when I look at the log statements in the Google App Engine, it receives the request from both my custom domain and the appspot domain - which seems to suggest the domain dns was updated properly. See image below, where the 404 is from the custom domain and the 200 is from the appspot url:

Google App Engine Logs

Is there anything else I have to do? The backend is written in Go, and we're using the Mux Router. Do I need to modifiy my app.yaml file or edit my routes somehow? Any suggestions would be greatly appreciated.

Here I've included some code snippets I use to initialize my server:

App.yaml

    version: alpha-001
    runtime: go
    api_version: go1

    handlers:
    - url: /.*
      script: _go_app

    env_variables:
      PRODUCTION: 'TRUE'
      DATASTORE_DATASET: 'app-id'
      DATASTORE_HOST: 'http://localhost:8043'
      DATASTORE_EMULATOR_HOST: 'localhost:8043'
      DATASTORE_PROJECT_ID: 'app-id'
      GOOGLE_APPLICATION_CREDENTIALS: './app-string.json'

Initializing the WebConsole (Server):

    func init() {
        // Web Server for API Endpoints
        flag.Parse()

        var server *web_console.WebConsole
        if prod := os.Getenv("PRODUCTION"); prod == "FALSE" {
           server = web_console.NewWebConsole(false)
        } else {
           server = web_console.NewWebConsole(true)
        }

        server.Run()
      }

WebConsole.go

type WebConsole struct {
    prod   bool
    Mux    *mux.Router
    DbMap  *gorp.DbMap
    Client *datastore.Client
}

func NewWebConsole(prod bool) *WebConsole {
    return &WebConsole{
        prod: prod,
    }
}

func (w *WebConsole) Run() {
    w.dbInit()
    w.routesInit()
}

func (w *WebConsole) dbInit() {
    // Configure SQL connection
    // Code removed for privacy reasons
}

func (w *WebConsole) routesInit() {
    // Configure routes with Mux
    w.Mux = mux.NewRouter()
    api.AddCertChallengeApis(w.Mux)

    // The path "/" matches everything not matched by some other path.
    // Checkout: http://stackoverflow.com/questions/26581231/google-cloud-go-handler-without-router-gorilla-mux
    //      for more details
    http.Handle("/", w.Mux)

}

Api Package File for Routing

package api

import (
    "github.com/gorilla/mux"
    "google.golang.org/appengine"
    "google.golang.org/appengine/log"
    "net/http"
    "strings"
)

func AddCertChallengeApis(r *mux.Router) {
    r.Schemes("http")
    r.HandleFunc("/", defaultHandler())
}

func defaultHandler() http.HandlerFunc {
    return func(rw http.ResponseWriter, req *http.Request) {
        // Construct new app engine context
        c := appengine.NewContext(req)

        log.Infof(c, "App ID: %v", appengine.AppID(c))

        rw.Header().Set("Content-Type", "text/plain")
        rw.Write([]byte("hi, welcome to my website yo"))
        log.Infof(c, "Hit the website")
    }
}

Upvotes: 1

Views: 387

Answers (1)

mgbelisle
mgbelisle

Reputation: 641

I have done custom domains on appengine/go before, and there is nothing else you should have to do. I would try removing this line, though,

    r.Schemes("http")

in case it is something https related.

Upvotes: 0

Related Questions