St3114
St3114

Reputation: 55

How can I create random urls in gae?

I want to create random urls. i mean , let first url be myapp.appspot.com/868DF7.html it ll be connected to test.py in app.yaml. when user opens this url, test.py ll generate new url. I ll save the next url at datastore. and the previous url will not be accessible. I guess the word is "disposable".

Is that possible to create dynamic url like this?

my current test.py : it gets previous string from datastore and creates next url and saves it.

    import webapp2
    from google.appengine.ext import db
    from google.appengine.api import users

    import jinja2
    import os
    import uuid

    class Saved(db.Model):
        urls=db.StringProperty()
        date = db.DateTimeProperty(auto_now_add=True)

    def _rKey(r_name=None):
        return db.Key.from_path("r", r_name or 'default_r')
    class MainPage(webapp2.RequestHandler):
        def get(self):
            r_name="none"
            saveds_query = Saved.all().ancestor(
                _rKey(r_name)).order('-date')
            urls = saveds_query.fetch(10)
            q=db.Query(Saved).order('-date')
            print "previous url:", q[0].urls
            print "<br>"


            save = Saved(parent=_rKey(r_name))


            save.urls=str(uuid.uuid4().get_hex().upper()[0:6])+".html"
            print "next url:",save.urls
            save.put()    

     APP = webapp2.WSGIApplication([
        ('/give/', MainPage),    
     ], debug=True)

and app.yaml

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /.*
  script: helloworld.APP

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: "2.6"

rest of the files are pretty much the same guestbook example of google. https://github.com/GoogleCloudPlatform/appengine-guestbook-python/blob/master/guestbook.py

Upvotes: 0

Views: 121

Answers (2)

Andrei Volgin
Andrei Volgin

Reputation: 41089

app.yaml lists "rules" that a server uses to match handlers. These rules may use wildcards and regular expressions. For example, you can add the following rule:

- url: /movie/.*

A server would interpret this rule to send all matching URLs to a file or a script that you specify. For example, the following URLs would match this rule:

myApp.appspot.com/movie/1234
myApp.appspot.com/movie/1234.mp4

You can use regular expressions to be as specific as you need in your matching rules.

Upvotes: 1

DT Rush
DT Rush

Reputation: 171

You should define a handler that is active on a wildcard or a regular expression that matches the format you chose for the random strings.

When you get a request there, have the handler check the specific route that was used, and validate it against (as you correctly noted) the Datastore, Cloud SQL, your own Redis server, etc., which stores resources or references to resources that should be accessible from that special route.

You would then have the handler serve the resource, or at that point, if you want/need, validate authentication somehow (hopefully OAuth of their google account)

Upvotes: 1

Related Questions