Showing posts with label appengine. Show all posts
Showing posts with label appengine. Show all posts

Friday, September 27, 2013

App Engine 1.8.5 released

Good news!

We’re back with Google App Engine 1.8.5, and this release focuses on an upgrade to the Search API and improvements to the PHP runtime. Next week we’ll be at Zendcon, one of the largest conferences for PHP developers, where we’ll have some cool things to share about the PHP runtime.

https://cloud.google.com/products/app-engine

Monday, October 22, 2012

Redirect all request to specific site to new location with appengine

Let say you have web site with complex structure, what stop work for some reason, and you want redirect all request for you site to new location, instead of showing 404 error.

Example:

http://www.oldsite.com/about.html -> http://www.newsite.com/temporarily.html 
http://www.oldsite.com/product.php?id=12 -> http://www.newsite.com/temporarily.html 
http://oldsite.com/product.php?id=12 -> http://www.newsite.com/temporarily.html 
http://oldsite.com/group.php?id=12 -> http://www.newsite.com/temporarily.html

Make what with Google AppEngine. It's free and simple.

  1. You need to create an application on Google Appengine Platform
  2. Now you have http://YOU-APP-ID.appspot.com page where you place handler for you request
  3. For accessing to you app by you custom domain name (www.oldsite.com) instead YOU-APP-ID.appspot.com you need prove you right for what name and add what name in Domain Setup part of YOU-APP-ID application settings page.
  4. Add CNAME record www in DNS settings for oldsite.com and point it to "ghs.googlehosted.com."
  5. Now you need to create and deploy app for handling requests:
    1. Download actual version o Google App Engine SDK for Python
    2. Install SDK and create new app with YOU-APP-ID as application ID
    3. Edit file app.yaml:

    4. application: YOU-APP-ID
      version: 1
      runtime: python
      api_version: 1
      
      handlers:
      - url: .*
        script: main.py
    5. Add main.py:
      from google.appengine.ext import webapp
      from google.appengine.ext.webapp import util
      
      class Redirect (webapp.RequestHandler):
          def get(self):
              self.redirect('http://www.newsite.com/temporarily.html', permanent=True)
                  
      def main():
          application = webapp.WSGIApplication([
              ('/.*', Redirect)
              ],debug=True)
          util.run_wsgi_app(application)
      if __name__ == '__main__':
          main()
      
    6. Deploy app to Google
  6. Now you app redirect all request from www.oldsite.com/anything  to http://www.newsite.com/temporarily.html.
  7. In addition we need to redirect all non www request to request with www, sadly you can not do that redirect only by dns server, you need some magic on http server. In Lighttpd i solve what with adding in conf file:
    $HTTP["host"] =~ "^oldsite\.com$" { url.redirect = ( "^/(.*)" => "http://www.oldsite.com/$1" ) }

Done!

Monday, June 18, 2012

How to move beyond the App Engine free mail limitations

Appengine have very tough quota limits for sending mail to user (now it's about 100 recipients in day). It so small even for little apps. What you need to do for solve that kind of problem?
  1. Use you standalone server or VPS with installed php
  2. Create file mailgate.php which contain code below and upload file to you VPS.:
    ≶?php
     $MAILGATE_KEY = 'MYSUPERSECRETCODEPHRASE';
     $user_code  = $_POST['code'];
     if ($user_code != $MAILGATE_KEY) {echo("wrong pass");die();}
     $to   = $_POST['to'];
     $subject  = $_POST['subj'];
     $body   = $_POST['body'];
     $headers = 'From: MY NAME ' . "\r\n" .
     'Reply-To:  myemail@example.com' . "\r\n".
        'Content-type: text/plain; charset=utf-8' . "\r\n";
     if (mail($to, $subject, $body, $headers)) {echo("sent");} else {echo('error');die();}
    ?>
    
  3. on server side create function for easy sending mail
    def sendmail(to, subject, body):
        mailqueue = taskqueue.Queue('mailqueue')
        
        task = taskqueue.Task(url='/queue/sendmail', params={
                                                    'subject': subject,
                                                    'to': to,
                                                    'body': body})
        mailqueue.add(task)
    
  4. create file queue.yaml for properly sending mail (appengine limit count of request per minutes)
    queue:
    - name: mailqueue
      bucket_size: 1
      rate: 30/m
      retry_parameters:
        task_age_limit: 2d
    
  5. Create entry point '/queue/sendmail':
    class SendEmail(webapp.RequestHandler):
        def post(self):
            data = {
                'to':  unicode(self.request.get('to')).encode('utf-8'), 
                'subj': unicode(self.request.get('subject')).encode('utf-8'),
                'body': unicode(self.request.get('body')).encode('utf-8'),
                'code': settings.MAILGATE_KEY
                }
            data = urllib.urlencode(data)
            result = urlfetch.fetch(settings.MAILGATE_URL, deadline=55, payload = data, method = urlfetch.POST, headers={'Content-Type': 'application/x-www-form-urlencoded'})
            logging.info("Sending mail to " + self.request.get('to') + " " + result.content)
    
  6. Put in you settings.py file with definition for:
    • MAILGATE_URL (it's full URL where you you actual mailgate.php available)
    • MAILGATE_KEY (same as in mailgate.php)
  7. Use you custome sendmail instead of appengine default.
  8. Enjoy!

Thursday, September 22, 2011

Delete entry of Table Entry

To delete all entry for specific Model on Google App Engine use following code:
from google.appengine.ext import db
class Entry(db.Model):
    pass
db.delete(Entry.all().fetch(1000))