使一个Go Web应用程序不依赖于Google App Engine

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

Making a Go Webapp independent of Google App Engine

问题

这个问题是关于我正在开发的网页应用程序的,这是一个用Go语言编写的订阅阅读器。代码可以在http://github.com/matthewbauer/rivulet找到,但这不是回答问题所必需的。

我有一个用Go语言编写的网页应用程序,正在Google的App Engine上运行。它使用了通常的App Engine库,如datastore、memcache和user。

有没有办法让我的用户在不破坏App Engine兼容性的情况下运行这个应用程序?

Go现在提供了构建约束,根据目标构建平台来排除/包含文件:

// +build !appengine

所以,我知道这是可能的。但是,我最大的问题是我的库依赖于App Engine:datastore、memcache和user。我知道其他库提供了datastore和memcache,而且我可以自己实现user,但是我该如何封装它们?

我知道他们可以使用SDK设置一个开发服务器,但是对于一些用户来说可能太复杂了。我想要一个普通Go项目提供的单个可执行文件。这可能吗?

如果有任何示例存在,我还没有找到它们;任何独立于App Engine的网页应用程序的示例都将不胜感激。我可以通过示例来学习。

英文:

This question comes from working on my webapp, a feed reader written in Go. The code is available at http://github.com/matthewbauer/rivulet although that should not be necessary to answer the question.

I have a webapp written in Go that is running on Google's App Engine. It uses the usual common App Engine libraries like datastore, memcache, and user.

Is there any way to let my users run this app on their own without breaking App Engine compatibility?

Go now provides build constraints that exclude/include files based on target build platform:

// +build !appengine

So, I know that this is possible. But, my biggest problem is with my libraries that depend on App Engine: datastore, memcache, and user. I know other libraries provide datastore and memcache, and I can implement user on my own, but how would I go about wrapping it up?

I know that I can have them set up a development server with the SDK, but that might be too involved for some users. I want a single executable that normal Go projects provide. IS that possible?

If any examples exist, I haven't found them yet; any examples of App Engine independent webapps would be appreciated. I can learn by example.

答案1

得分: 4

你可能需要重构你的代码。

基本的原则是,无论你在哪里依赖于一个AppEngine包,都要为你使用它的方式定义自己的接口。这将允许你将应用程序与appengine库解耦。

一旦你定义了这些接口,你就可以开始提供满足这些接口的替代方案。你将能够插入任何满足这些接口的数据存储或内存缓存,你的应用程序将能够在appengine上运行,也可以作为独立应用运行。

英文:

You will probably have refactor your code.

The basic rule of thumb will be anywhere you depend on an AppEngine package define your own interface for the way you use it. This will allow you decouple the app from the appengine libraries.

Once you've defined those interfaces then you can start providing alternatives that satisfy the interfaces. You'll be able to plugin any Datastore or Memcache that satisfies the interfaces and your app will be able to both run on appengine or as a standalone with the alternatives.

huangapple
  • 本文由 发表于 2013年7月5日 13:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/17481618.html
匿名

发表评论

匿名网友

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

确定