Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does goji work with Google App Engine/Go?

I would like to develop application with Goji and Google App Engine/Go.

I copied and pasted sample code from https://github.com/zenazn/goji and changed func name from "main" to "init".

package main

import (
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}

func init() {
    goji.Get("/hello/:name", hello)
    goji.Serve()
}

and Run this application

 # goapp serve

but this application says

 bad runtime process port ['']

 2014/06/01 08:35:30.125553 listen tcp :8000: bind: address already in use

How can I use Goji with GAE/Go? Or I cannot use Goji with GAE/Go?

like image 299
yosuke Avatar asked Jun 01 '14 08:06

yosuke


1 Answers

init functions are special, in that they're run automatically to initialize modules. By serving you app in an init function, you've stopped the initialization of your code mid-way (because goji.Serve never terminates). Probably what's happening is that something depends on runtime flags, and you're running before they're parsed.

What you can do is write a single app engine handler that forwards the serving the goji.DefaultMux. I've not tested it, but something like this should work:

import (
    "fmt"
    "github.com/zenazn/goji"
    "net/http"
)

func init() {
    http.Handle("/", goji.DefaultMux)
    ... Register your goji handlers here
}
like image 51
Paul Hankin Avatar answered Nov 05 '22 21:11

Paul Hankin