Recently, I asked a question - would it be interesting for habra people to read about how to write under Google AppEngine? Habralyudi said yes, I wonder - well, I decided to start. In this article we will consider the following questions:
- Ultra-short introduction to Google AppEngine (GAE), what it is and what it is eaten with
- Features and limitations of gae
- A super-brief overview of the architecture of a GAE application
- Minimal Application Development with Authentication
- Developing a more sophisticated, real-world application based on 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 if you can, but not very much” we will certainly not have time in this post, but we’ll get there for sure).
What is Google AppEngine?
If you believe the official
Getting Started , GAE is the infrastructure that allows you to run your web applications on Google servers. GAE solves many standard web development tasks, such as:
- The organization of data storage and search for them is not exactly SQL, but it seems.
- The possibility of absolutely transparent expansion in the event of an increase in load - a properly developed application should scale without any involvement from the user
- Authorization of users through Google Authentication - the problem is solved in two lines exactly.
- Manage task queues and start tasks on a schedule.
GAE allows you to use two languages ​​- Python and Java. In this article we will focus on Python, as more simple and transparent for GAE.
')
Features of development under GAE
There are several important features that need to be taken into account - they distinguish GAE from other web frameworks (strictly speaking, GAE is a framework and is not - this is a whole infrastructure).
- Other computers on the network can only be reached via HTTP (s) only. The reverse is also true - applications running on GAE are available only over HTTP (s) and only over standard ports.
- No write access to the file system. The maximum that an application can do is to read the files that were downloaded along with the application itself. To organize data storage, you should use either GAE Datastore (about this later), or memcache.
- There are only three scenarios of the application:
- Reply to HTTP (s) request, in particular - user request
- Running tasks on a schedule (cron)
- Queue of tasks.
It is important to note that the last two types are sub-types of the first — any task is launched as an HTTP request to a specific address, the only difference is who runs it — the user, the task queue manager or cron. The duration of a single 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 fighter GAE, then we rush into battle and there, along the way, we'll figure it out.
Create a GAE application
To simplify the lives 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 are using a Mac (just like so many google's), then there is a nice GUI for you, for Linux there is a set of console scripts that are simple to the extreme.
Initially, I promised to tell you 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 ...
An application has been created. Now we can do, in general, two things: run it and see what was created there? Perhaps run. Press the "Run" button, and when it becomes available - Browse:
Nothing surprising, as expected. Now it's time to see how it all works. For convenience, I recommend specifying your editor in the GAE Launcher settings:
I recommend TextMate as the most advanced and extensible, in your case it may be something else. Press the Edit button (selecting our created application) and get ...
Minimum GAE project - three files. Right now, we are even interested in two -
app.yaml
and
main.py
, since
index.yaml
automatically generated. Let's start with
app.yaml
:
application: helloworld
version: 1
runtime: python
api_version: 1
handlers:
- url:. *
script: main.py
What does this file say?
application
- as it is not difficult to guess, this is the name of the application, this is how GAE will know it (and this name will be used to upload to Google)version
- GAE supports a very flexible versioning mechanism; at the same time, a significant number of code versions (and only one of them is default) can be downloaded (and work!)api_version
- at the moment - constant, should be “1”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 .*
- “everything else”, he should always go last in the list.
Let's try to implement a very simple application - for all users who will go to the page it will offer to log in via Google, and record all who have been authorized; to the administrator, it will allow viewing the list of users who have logged in there. Let's start with the 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 <a href='%s'> authorize </a> to continue"
% users.create_login_url ("/"))
else:
resp = "Dear% s, thanks for authorizing!" % 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, this is not the two lines that I promised - but you must admit, most of it was still spent on “beautiful” things, like communicating with the user. The two expressions that really matter are
user = users.get_current_user()
and
users.create_login_url("/")
. The first one checks if we have an active user session. The second creates a URL that leads the user to the authorization form, and if successful, back to the application (the only parameter of the
create_login_url
function). And what did we do?
Display the request form:
The authorization form is generated by GAE automatically; If the application runs on Google’s servers, instead you’ll see the standard “Sign in with your Google Account” form:
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 enough completely.
Total
Here is the first article. My initial predictions that everything can be easily and simply fit in one post turned out to be somewhat optimistic - and so much has already happened, and we only touched upon the actual writing of the code. In the next article (which I think will be written soon - if the habra users are interested) we will finish our application, figure out how to store data and how to get to them, and upload it to Google servers. The created project is available
here - we will refine it along the way.
Update: The second part -
habrahabr.ru/blogs/gae/81920
Update 2: The third part -
habrahabr.ru/blogs/gae/81933
The list of recommended literature will consist of one link:
App Engine Python Overview - read, look, understand. This documentation was enough for me, although it took some time. There will be questions - ask, and, of course, I look forward to your feedback and suggestions for continuation.