Goji可以与Google App Engine/Go一起使用。

huangapple go评论80阅读模式
英文:

Does goji work with Google App Engine/Go?

问题

我想使用Goji和Google App Engine/Go开发应用程序。

我从https://github.com/zenazn/goji复制并粘贴了示例代码,并将函数名从"main"更改为"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()
}

然后运行这个应用程序

# goapp serve

但是这个应用程序报错:

bad runtime process port ['']
     
2014/06/01 08:35:30.125553 listen tcp :8000: bind: address already in use

我该如何在GAE/Go中使用Goji?或者我不能在GAE/Go中使用Goji吗?

英文:

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?

答案1

得分: 5

init函数是特殊的,它们会自动运行以初始化模块。通过在init函数中提供你的应用程序,你停止了代码的初始化过程(因为goji.Serve永远不会终止)。可能发生的情况是某些内容依赖于运行时标志,而你在它们解析之前就运行了。

你可以编写一个单一的应用引擎处理程序,将服务转发给goji.DefaultMux。我没有测试过,但是像下面这样的代码应该可以工作:

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

func init() {
    http.Handle("/", goji.DefaultMux)
    ... 在这里注册你的goji处理程序
}
英文:

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
}

答案2

得分: 2

这在我的开发服务器上运行良好。

package main

import (
	"fmt"
	"net/http"

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

func index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "首页")
}

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

func init() {
	http.Handle("/", goji.DefaultMux)
	goji.Get("/", index)
	goji.Get("/hello/:name", hello)
}
英文:

This works fine in my dev server.

package main

import (
	"fmt"
	"net/http"

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

func index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "index page")
}

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

func init() {
	http.Handle("/", goji.DefaultMux)
	goji.Get("/", index)
	goji.Get("/hello/:name", hello)
}

huangapple
  • 本文由 发表于 2014年6月1日 16:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/23978181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定