Development for Google AppEngine: From the Beginning

    Recently I asked a question - would it be interesting for habra-people to read about how to write under Google AppEngine? Habralyudi said yes, interestingly - well, I decided to start. In this article we will consider the following issues:
    1. Ultra-short introduction to Google AppEngine (GAE), what it is and what it is eaten with
    2. GAE features and limitations
    3. Super-brief overview of GAE application architecture
    4. Develop a minimal authentication application
    5. The development of more complex, real-world application based MyTwiLinks
    Let's start from the very beginning, from the simplest - absolutely for noobs (which I was when I started to deal with GAE), gradually moving towards more complex concepts, ending with dirty hacks on the topic “what to do if you can’t, but really need” (almost for sure we will not be in time within the framework of this post, but we will definitely get there).



    What is Google AppEngine?


    According to the official Getting Started , GAE is an infrastructure that allows you to run your web applications on Google’s servers. GAE solves so many standard web development tasks, such as:
    1. Organizing a data warehouse and searching for it is not exactly SQL, but it seems.
    2. The possibility of absolutely transparent expansion in the event of an increase in load - a correctly designed application should scale at all without any involvement from the user
    3. User authentication through Google Authentication - the task is solved in two lines exactly.
    4. Managing task queues and starting tasks on a schedule.
    GAE allows you to use two languages ​​- Python and Java. In this article, we will focus on Python as being simpler and more transparent for GAE.

    Features of development under GAE


    There are several important features that you need to consider - they distinguish the GAE environment from other web frameworks (strictly speaking, GAE is not a framework, it is a whole infrastructure).
    1. Other computers on the network can only be reached via HTTP (s). The reverse is also true - applications running on GAE are available only over HTTP (s) and only over standard ports.
    2. No write access to the file system. The maximum that the application can do is read the files that were downloaded with the application itself. To organize data storage, you should use either GAE Datastore (more on that later) or memcache.
    3. There are only three application scenarios:
      • Response to HTTP (s) request, in a particular case - user request
      • Scheduled tasks launch (cron)
      • Queue of tasks.
      It is important to note that the last two types are subtypes of the first - any task is launched as an HTTP request to a specific address, the only difference is who launches it - user, task queue manager or cron. The duration of one task should not exceed 30 seconds - also an important limitation, and you have to live with it.

    The above is an ultra-short course of a very young GAE fighter, then we will rush into the battle and there, along the way, we will figure it out.

    Create a GAE application


    To simplify the life of simple developers as much as possible, Google has released a small application called Google AppEngine Launcher - it allows you to create applications locally and run them on the user's local machine. If you use a Mac (as well as so many googles) then you have a beautiful GUI, for Linux there is a set of console scripts that are simple to the extreme. Initially, I promised to talk about how MyTwiLinks was made , but then I realized that without basic knowledge it would be quite difficult. So let's start with something simple, for example ...






    Application created. Now we can do, in general, two things: launch it and see what was created there? Let's run it. We press the “Run” button, and when it becomes available - Browse: Nothing, in general, surprising - everything is as expected. Now it's time to see how it all works. For convenience, I recommend setting your editor in the GAE Launcher settings: I recommend TextMate as the most advanced and extensible, in your case, it can be something else. Click the Edit button (choosing our created application) and get ... The minimum GAE project is three files. Right now we are even interested in two - and since it is automatically generated. Let's start with :









    app.yamlmain.pyindex.yamlapp.yaml

    	application: helloworld
    	version: 1
    	runtime: python
    	api_version: 1
    	handlers:
    	- url:. *
    	  script: main.py
    

    What does this file say in general?
    1. application - as it’s not hard to guess, this is the name of the application, this is how GAE will know it (and it is this name that will be used to upload to Google’s servers)
    2. version- GAE supports a very flexible mechanism for working with versions; at the same time, a significant number of code versions can be downloaded (and work!) (while only one of them is default)
    3. api_version - at the moment - a constant, should be “1”
    4. handlers- and this is where all the fun begins. In this section, we list all the URLs to which the application should “respond” and the scripts that will process these requests. Url .*is “everything else”, it should always go last on the list.

    Let's try to implement an extremely simple application - for all users who go to the page it will offer to log in via Google, and record everyone who has been logged in; it will allow the administrator to view the list of users who went there. Let's start with authorization.

    	#! / usr / bin / env python
    	from google.appengine.ext import webapp
    	from google.appengine.ext.webapp import util
    	from google.appengine.api import users
    	class MainHandler (webapp.RequestHandler):
    	  def get (self):
    	       user = users.get_current_user ()
    	       if not user:
    	           resp = ("Welcome to HelloWorld - please authorize to continue"
    	                      % users.create_login_url ("/"))
    	       else:
    	           resp = "Dear% s, thanks for authorising!" % user.nickname ()
    	       self.response.out.write (resp)
    	def main ():
    	  application = webapp.WSGIApplication ([('/', MainHandler)],
    	                                       debug = True)
    	  util.run_wsgi_app (application)
    	if __name__ == '__main__':
    	  main ()
    


    Well, these are not the two lines that I promised - but you must admit, most of it was spent on “beauty” like communicating with the user. Two expressions that really matter are user = users.get_current_user()and users.create_login_url("/"). The first checks to see if we have an active user session. The second creates a URL that will lead the user to the login form, and in case of success - back to the application (the only parameter of the function create_login_url). And what did we do?

    We display the request form: Authorization form - GAE generates automatically; if the application runs on Google servers, then instead you will see the standard form “Sign in with your Google Account”: And, finally, the result:








    We wrote the simplest application with authorization, and we did not need to write almost anything. This is one of the advantages of GAE - for 99% of the applications of the infrastructure that it provides, it should be completely and completely.

    Total


    Here is the first article. My initial forecasts that everything could be fit easily and simply in one post turned out to be somewhat optimistic - and so it turned out quite a lot, and we only touched on the actual writing of the code. In the next article (which, I think, will be written soon - if it will be interesting for the people of the world) we will end our application, we will figure out how to store data and how to reach it, and upload it to Google servers. The created project is available here - we will modify it along the way.

    Update: Second part - habrahabr.ru/blogs/gae/81920
    Update 2: Third part - habrahabr.ru/blogs/gae/81933 The

    list of recommended literature will consist of one link: App Engine Python Overview- read, look, sort it out. This documentation sufficed me, although it took some time. There will be questions - ask, and, of course, I look forward to your feedback and suggestions for continuation.

    Also popular now: