GAEで簡単にWebランキングサ―ビスを実現する方法を紹介してみようと思う
著者もサンプルや資料を継ぎ接ぎしたレベルで実装しているので、改善点等ご意見頂ければ幸いである。
1.前準備
此方のサイトで解説されているので、そちらを参照頂きたい。
GAEの登録とEclipse+PyDevの環境構築まで出来ればOKである。
http://libro.tuyano.com/index2?id=50
2.アプリケーションの作成
最低限必要なファイルは以下の通り。
app.yaml ※applicationの値はGAEで作成したアプリケーション名と合わせること。
application: hogehoge-applicationcrossdomain.xml ※セキュリティ設定は最も緩くしているので、必要に応じて設定すること。
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /crossdomain.xml
mime_type: text/xml
static_files: crossdomain.xml
upload: crossdomain.xml
- url: /simple_db
script: simple_db.app
crossdomain.xml
simple_db.py
<?xml version="1.0"?>この設定ファイルは此方のForumの投稿からお借りしたものです。
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<!-- Read this: www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html -->
<!-- Most restrictive policy: -->
<!--
<site-control permitted-cross-domain-policies="none"/>
-->
<!-- Least restrictive policy: -->
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="*" secure="false"/>
<allow-http-request-headers-from domain="*" headers="*" secure="false"/>
<!--
If you host a crossdomain.xml file with allow-access-from domain=“*”
and don’t understand all of the points described here, you probably
have a nasty security vulnerability. ~ simon willison
-->
</cross-domain-policy>
http://stackoverflow.com/questions/11823025/what-does-this-crossdomain-xml-imply
simple_db.py
from google.appengine.ext import dbあとはGAEにデプロイするだけである。
import webapp2
class SimpleDB(db.Model):
name = db.StringProperty(required=True)
data = db.StringProperty(required=False)
create_time = db.DateTimeProperty(auto_now_add=True)
update_time = db.DateTimeProperty(auto_now=True)
class SimpleDBHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
if self.request.get('name'):
rank = 1
for record in SimpleDB.all().order('-data').fetch(10, 0):
self.response.out.write(str(rank)+'. '+record.data+'\r\n')
rank = rank + 1
else:
self.response.out.write("access denied.")
def post(self):
if self.request.get('name') and self.request.get('data'):
data = SimpleDB(name=self.request.get('name'), data=self.request.get('data'))
data.save()
else:
self.response.out.write("access denied.")
app = webapp2.WSGIApplication([('/simple_db', SimpleDBHandler)], debug=True)
3.使ってみる
UnityからWebランキングサ―ビスを使用する簡単なサンプルを用意したのでご覧頂きたい。
多分動く筈である。
https://github.com/soyliquid/GAETest