介紹
Google App Engine 可以讓你在 Google 上面運行你的網頁程式. 可以使用免費的網域名 appspot.com 或是透過 Google Apps 來使用自己的網域名稱.
目前只能用 Python 來寫 GAE 程式.
Getting Started
本範例會做出一個簡單的 Guest book, 讓使用者可以在上面發表一個公開的留言, 使用者還可以使用匿名或是 Google 帳號來發表. 透過這個範例可以學習到:
- 如何使用 App Engine datastore
- 如何將 Google 帳號整合進程式裡面
- 如何使用 App Engine 裡面的一個簡易的 Python 網頁框架 - webapp
- 如何使用 Django template engine
The Development Environment
開發與上傳 Google App Engine 必須要安裝
在範例中會使用到兩個 SDK 的指令
Hello, World!
建一個目錄取名 helloworld. 在這個目錄裡面建一個檔案 helloworld.py, 內容是:print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
每個 App Engine 程式都有一個設定檔案叫 app.yaml. 裡面會描述如何處理網頁的請求.
現在在 helloworld 目錄裡面再建立一個 app.yaml, 內容是:
application: helloworld
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: helloworld.py
- application 是程式的識別代號, 在 App Engine 上面這是一個唯一識別代號. 本地測試所以可以先暫時取名為 helloworld
- version 是程式的版本號, 如果在上傳前有修改的話, App Engine 會保留上一個版本, 可以在管理介面上回復到上個版本
- runtime 是指使用什麼語言運行, 目前只能是 python, 以後會有其他語言的支持
- api_version !!?? 應該跟 runtime 有關係
- handlers 是使用 RegEx 來設定這些 URLs 應該由那個程式做處理
接著測試剛剛寫的內容, 在命令列下執行:
google_appengine/dev_appserver.py helloworld/
執行過後, 會開始運行網頁服務, port 8080. 只需要在瀏覽器上輸入
就可以看到效果了. 另外可以使用 Ctrl+C 來關閉. 關於其他的參數請參考 Dev Web Server reference 
Using the webapp FrameworkGoogle App Engine 支援各種純 python 寫的 CGI 框架, 包含 Django, CherryPy, PyIons, web.py. 而 App Engine 也內建一個簡單的框架 webapp. 一個 webapp 程式包含了三個部分:
- 一個或多個 RequestHandler 用來處理請求與回覆
- 一個 WSGIApplication 實體, 會根據請求的 URLs 來分配如何處理
- 一個例行程序用來執行 WSGIApplication
現在重新修改 helloworld/helloworld.py 的內容:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication([('/', MainPage)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Using the Users Service
TODO: http://code.google.com/appengine/docs/python/gettingstarted/usingusers.html
// code block
@TODO:
-----
Configuring Eclipse on Windows to Use With Google App Engine
http://code.google.com/appengine/articles/eclipse.html
-----
你可以連到 http://localhost:8080/_ah/admin/datastore 檢查資料 (存在你電腦裡)。
-----
|
|