顯示具有 Google App Engine 標籤的文章。 顯示所有文章
顯示具有 Google App Engine 標籤的文章。 顯示所有文章

2012/06/22

Webapp2: Hello world

最近又在摸索 Google app engine,webapp2 基本上就是 GAE 內建的 web framework 啦,提供基本的 URL route、request handler、template、database 存取,相容於 WSGI 架構。

這是官方的 Hello world
helloworld.py
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp2 World!')

app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)

app.yaml
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: helloworld.py

若使用 WSGI 來啓動 server,預設的 configuration file 就是 app.yaml,裡面會指明要執行哪個 python code,像上面就是 helloworld.py 這個檔案。執行後會去尋找"app"這個 object,所以物件的命名不能有錯,不然就會出現錯誤:
ERROR    2012-06-20 07:37:47,561 wsgi.py:189] 
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 187, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 239, in _LoadHandler
    raise ImportError('%s has no attribute %s' % (handler, name))
ImportError:  has no attribute app


如果不想透過 google app launcher 來啓動 project,請參考這一篇 Quick start (to use webapp2 outside of App Engine),需要額外安裝三個 package。
程式碼如下,要自己另外帶起 http server:
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)

def main():
    from paste import httpserver
    httpserver.serve(app, host='127.0.0.1', port='8080')

if __name__ == '__main__':
    main()

再從 command line 下達指令就可以了
$ python main.py
serving on http://127.0.0.1:8080

2012/03/21

Google app engine (GAE) - Hello World

Step by step 紀錄我的學習過程:

1. 申請 Google app engine

2. 環境
  • Python 2.5.2 - MacOS X有內建

3. 了解 Google App 限制

4. 了解 Google App 提供的資源

5. 下載SDK Python 專用的 Google 應用服務引擎 SDK
我自己是用 mac 版的圖形介面,按滑鼠右鍵選擇 new,然後選擇自己的 project directory,再來點左上角的 Run,就可以透過 localhost 去預覽自己的 app:
6. IDE - Eclipse
下載 Google Plugin for Eclipse

話說安裝的時候一直遇到下面的錯誤,最後換了一個網路環境就可以了,應該是網路問題吧?
An error occurred while collecting items to be installed
session context was:(profile=epp.package.jee, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, action=).
HTTP Server 'Bad Gateway' : http://dl.google.com/eclipse/plugin/3.7/plugins/com.google.gdt.eclipse.gph.subversive_2.5.2.v201202290255-rel-r37.jar
HttpClient connection error response code 502.


7. 範例 - Hello, World!
下載後,記得修改 app.yaml,application name 要填自己的才有辦法 deploy 到 server
application: ysl-test1
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: helloworld.py


8. 部署 - 使用 appcfg.py
我自己是使用OS X的圖形介面,按一下右上角的 Deploy 就自動部署上到 google 了,部署成功訊息如下:

9. 瀏覽 http://(application_name).appspot.com 看看 Hello World有沒有 show 出來

10. 後台管理
  • 系統監控
  • 檢視 Log
  • Application 設定


小結
Google app engine架設網站相當快速、非常適合懶得自己架 server (IDC or AWS) 的人使用,所有的 application setting 透過 Web UI 就可以達成。
但跟 google 綁這麼緊密,使用它的data storage、API、管理工具、Log... 將來如果想轉到其他環境...應該會很難。



參考資料