Commits

lenmore committed ebbf2f3

dropbox的GAE代理

Comments (0)

Files changed (9)

+syntax: glob
+
+Thumbs\.db
+
+\.settings
+\.project
+\.pydevproject
+
+.\.pyc
+.\.log
+.\.bak
+application: omoboxhg 
+version: 1
+runtime: python27
+api_version: 1
+threadsafe: true
+
+
+builtins:
+- datastore_admin: on
+- remote_api: on
+#- deferred: on
+
+default_expiration: "3600s"
+
+handlers:
+- url: /.*
+  script: main.app
+# -*- coding:utf-8 -*-
+import logging
+from types import ListType
+import datetime
+from time import time
+
+from google.appengine.api import memcache
+
+from config import ENABLE_DB_CACHE, ENABLE_PAGE_CACHE
+import utils
+
+
+def set(key, expire_time=0, model_name=None):
+    def _decorate(method):
+        def _wrapper(*args, **kwargs):
+            if not ENABLE_DB_CACHE:
+                return method(*args, **kwargs)
+
+            result = memcache.get(key)
+            if result is None:
+                logging.info("missing:" + key)
+                result = method(*args, **kwargs)
+                memcache.set(key,result,expire_time)
+                
+                if model_name:
+                    _k = "__CK_" + model_name
+                    _v = memcache.get(_k)
+                    if _v is None:
+                        _v = [key, ]
+                    else:
+                        if key not in _v:
+                            _v.append(key)
+
+                    memcache.set(_k, _v)
+
+            return result
+
+        return _wrapper
+    return _decorate
+
+def set_list(key, expire_time=0, model_name=None):
+    def _decorate(method):
+        def _wrapper(*args, **kwargs):
+            if not ENABLE_DB_CACHE:
+                return method(*args, **kwargs)
+
+            result = memcache.get(key)
+            if result is None:
+                logging.info("missing:" + key)
+                result = method(*args, **kwargs)
+                memcache.set(key,result,expire_time)
+                
+                if model_name:
+                    _m = {}
+                    _k = "__CK_" + model_name
+                    _v = memcache.get(_k)
+                    if _v is None:
+                        _v = [key, ]
+                        _m[_k] = _v
+                    else:
+                        if key not in _v:
+                            _v.append(key)
+                            _m[_k] = _v
+                            
+                    if type(result) is ListType:
+                        for r in result:
+                            _k = "__CK_" + model_name + "_" + str(r.key())
+                            #logging.info(_k)
+                            _v = memcache.get(_k)
+                            if _v is None:
+                                _v = [key, ]
+                                _m[_k] = _v
+                            else:
+                                if key not in _v:
+                                    _v.append(key)
+                                    _m[_k] = _v
+                                    
+                    memcache.set_multi(_m)                     
+                
+            return result
+
+        return _wrapper
+    return _decorate
+
+
+def set_page(key=None, expire_time=86400):
+    def _decorate(method):
+        def _wrapper(*args, **kwargs):
+            handler = args[0]         
+            if not ENABLE_PAGE_CACHE:
+                method(*args, **kwargs)
+                #handler.response.out.write("\n<!--Page Cache Disabled-->")
+                #handler.response.out.write("\n<!--" + str(time()-handler.request.environ.get("X_REQUEST_START_AT", 0)) + "-->")
+                #return
+            else:
+                if key:
+                    _k = key
+                else:
+                    _k = "PGC_"+handler.request.path_qs
+		   
+		#logging.info(_k)
+                _v = memcache.get_multi([_k, '__PCK'])
+                _ks = _v.get('__PCK')
+                cache = _v.get(_k)
+                if cache is not None:
+                    handler.response.headers['Content-Type'] = cache['content_type']
+                    handler.response.write(cache['html'])
+
+                    #handler.response.out.write("\n<!--Cached hit-->")
+ 
+                else:
+                    method(*args, **kwargs)
+		    logging.info(handler.response.status_int)
+                    if handler.response.status_int == 200:
+                        last_modify = datetime.datetime.utcnow().strftime("%d %b %Y %H:%M:%S GMT")
+                        cache = {'html':handler.response.body
+                                 , 'last_modify':last_modify
+                                 , 'content_type': handler.response.content_type}
+                        
+                        memcache.set(_k, cache, expire_time)
+                        #handler.response.out.write("\n<!--Cached missing-->")
+                        #handler.response.out.write("\n<!--" + str(time()-handler.request.environ.get("X_REQUEST_START_AT", 0)) + "-->")
+                
+                if handler.response.status_int == 200:    
+                    #handler.response.headers['Etag'] = '"' + cache['etag'] + '"'
+                    handler.response.headers['Last-Modified'] = cache['last_modify']
+                    handler.response.headers['Cache-Control'] = 'public, max-age=3600'
+                    handler.response.headers['Expires'] = (datetime.datetime.utcnow()+datetime.timedelta(seconds=3600)).strftime("%d %b %Y %H:%M:%S GMT")
+    
+                    if _ks is None:
+                        _ks = [_k, ]
+                        memcache.set('__PCK', _ks) 
+                    else:
+                        if _k not in _ks:
+                            _ks.append(_k)
+                            memcache.set('__PCK', _ks)                             
+                 
+
+        return _wrapper
+
+    return _decorate 
+
+
+def delete_multi_by_cache(cache_key):
+    _v = memcache.get(cache_key)
+    if _v is not None:
+        memcache.delete_multi(_v)
+
+
+#class StaticCache(object):
+#    logging.info("Static Cache loaded")
+#    
+#    _cache = {}
+#        
+#    @classmethod
+#    def flush(cls):
+#        for c in cls._cache:
+#            cls._cache[c] = None
+#        logging.info("Static Cache Flush")
+ 
+    
+    
+def flush_all():   
+    memcache.flush_all()
+    #StaticCache.flush()
+    logging.info("All cache flushed")
+    
+    
+    
+    
+# -*- coding:utf-8 -*-
+
+DEBUG = True
+ENABLE_DB_CACHE = False
+ENABLE_PAGE_CACHE = True
+
+DROPBOX = "http://dl.dropbox.com/u/1109390/website/omobox.com/_build"
+# -*- coding: utf-8 -*-
+import datetime
+import logging
+
+import webapp2
+from google.appengine.api import urlfetch
+from google.appengine.ext.webapp import util
+
+import config
+import cache
+
+
+def fetch(path):
+    url = config.DROPBOX + default_page(path, "index.html")
+    return urlfetch.fetch(url)
+
+def default_page(path, page):
+    if path[-1] == "/":
+        path += page
+    else:
+        if path.split("/")[-1].find(".") == -1:
+            path += '/' + page
+
+    return path
+
+
+class AnyHandler(webapp2.RequestHandler):
+    @cache.set_page()
+    def get(self):
+        res = fetch(self.request.path)
+
+        if res.status_code == 200:
+            self.response.headers['Content-Type'] = res.headers['content-type']
+            #self.response.headers['Cache-Control'] = "public, max-age=86400"
+            #self.response.headers['Expires'] = (datetime.datetime.utcnow()+datetime.timedelta(seconds=86400)).strftime("%d %b %Y %H:%M:%S GMT")
+
+            self.response.write(res.content)
+            #for k in res.headers.keys():
+            #    self.response.out.write("%s: %s <br>" % (k, res.headers[k]))
+            
+        else:
+            self.response.set_status(res.status_code)
+            if res.status_code == 404:
+                self.response.write("<h1>404 Page Not Found<h1>")
+            else:
+                self.response.write(res.status_code)
+
+
+class StaticHandler(webapp2.RequestHandler):
+    @cache.set_page()
+    def get(self):
+        res = fetch(self.request.path)
+
+        if res.status_code == 200:
+            self.response.headers['Content-Type'] = res.headers['content-type']
+            #self.response.headers['Cache-Control'] = "max-age=86400"
+            #self.response.headers['Expires'] = (datetime.datetime.utcnow()+datetime.timedelta(seconds=86400)).strftime("%d %b %Y %H:%M:%S GMT")
+
+            self.response.write(res.content)
+            
+        else:
+            self.error(res.status_code)
+
+        
+
+app = webapp2.WSGIApplication([ ('/static/.*', StaticHandler)
+                                , ('/.*', AnyHandler) ]
+                              , debug=config.DEBUG)
+
+@echo off
+set HTTP_PROXY=127.0.0.1:8118
+set HTTPS_PROXY=127.0.0.1:8118
+SET PATH=D:\Program Files (x86)\Python2.7;%PATH%
+
+python "D:\Program Files (x86)\Google\google_appengine\dev_appserver.py" ../gae-dropbox
+SET PATH=D:\Program Files (x86)\Python2.7;%PATH%
+python "D:\Program Files (x86)\Google\google_appengine\dev_appserver.py" --clear_datastore ../gae-dropbox
+@echo off
+set HTTP_PROXY=127.0.0.1:8118
+set HTTPS_PROXY=127.0.0.1:8118
+
+appcfg.py update .
+
+pause
+# -*- coding:utf-8 -*-
+import re, cgi, urllib, md5
+from datetime import datetime
+
+from google.appengine.api import urlfetch
+
+def htmlescape(str):
+    return cgi.escape(str)
+
+def urlquote(url):
+    return urllib.quote(url)
+
+def urlunquote(url):
+    return urllib.unquote(url)
+
+def getmd5(str):
+    return md5.new(str).hexdigest()
+
+def urlopen(url, method='GET', payload=None, useragent = None, cookie = None, charset = None, auto_charset = True):
+    headers = {}
+    if useragent:
+        headers['User-Agent'] = useragent
+    else:
+        headers['User-Agent'] = """Baiduspider+(+http://www.baidu.com/search/spider.htm)"""
+        
+    if cookie:
+        headers['Cookie'] = cookie
+        
+    res = urlfetch.fetch(url
+                        , method = method
+                        , payload = payload
+                        , headers = headers
+                        )
+    
+    if res.status_code == 200:
+        if auto_charset and (not charset):
+            ctype = res.headers.get('Content-Type', '')
+            if 'text' in ctype and 'charset' in ctype:
+                charset = ctype[ctype.index('=')+1:]
+            
+        if charset:
+            return res.content.decode(charset, 'ignore')
+        
+        return res.content
+    else:
+        return ""
+
+
+def intget(integer, default=None):
+    try:
+        return int(integer)
+    except (TypeError, ValueError):
+        _i = ''
+        for x in integer:
+            if ord(x) > 47 and ord(x) < 58:
+                _i = _i + x
+                
+        try:
+            return int(_i)
+        except: 
+            return default
+    
+def floatget(f, default=None):
+    try:
+        return float(f)
+    except (TypeError, ValueError):
+        _i = ''
+        for x in f:
+            if (ord(x) > 47 and ord(x) < 58) or ord(x) == 46:
+                _i = _i + x
+                
+        try:
+            return float(_i)
+        except: 
+            return default
+