Manuelarte
Manuelarte

Reputation: 1810

Differents entry points with Google App Engine Python

I want to have an structure like this

http://localhost:8080/

http://localhost:8080/problem2/

http://localhost:8080/problem3/

...

I also want a python structure like this

-src

    - app.yaml
    - main.py
    - package_problem_2
        - main.py
    - package_problem_3
        - main.py

I would like to have differents main.py for the differents folder in my web. I mean, if I am in http://mydomain:8080 it is src/main.py the want that handle the requests. But, if I am in http://localhost:8080/problem2 it should be package_problem_2/main.py the one which handle the requests.

Is this possible?

Upvotes: 0

Views: 620

Answers (1)

Gerard
Gerard

Reputation: 638

Are you using webapp2 framework?

If so, read on...

You need four files. For simplicity, all are located in the root folder of your app:

app.yaml
urls.py
SampleController.py
Sample.html

on your app.yaml, you should have something like this:

handlers:
- url: /.*
  script: urls.ap

That tells appengine to route all url patterns to urls.py.

Then on your urls.py, you have this structure:

import webapp2
import SampleController

#For each new url structure, add it to router.
#The structure is [py filename].[class name inside py file]
app = webapp2.WSGIApplication(debug=True)
app.router.add((r'/', SampleController.SampleHandler))


def main():
    application.run()

if __name__ == "__main__":
    main()

On your problem, you have three structures: /, /problem2, /problem3. They would correspond to these:

app.router.add((r'/', SampleController.SampleHandler))
app.router.add((r'/problem2', SampleController.SampleHandler2))
app.router.add((r'/problem3', SampleController.SampleHandler3))

It's up to you to decide if they go to the same handler or not.

SampleController.py looks like this:

import webapp2
import os

class SampleHandler(webapp2.RequestHandler):
    def get(self):
        template_values = {
            'handler': 'We are in SampleHandler',
            'param2': param2 
        }
        path = os.path.join(os.path.dirname(__file__), 'Sample.html')
        self.response.out.write(template.render(path, template_values))


class SampleHandler2(webapp2.RequestHandler):
    def get(self):
        template_values = {
            'handler': 'We are in SampleHandler2',
            'param2': param2 
        }
        path = os.path.join(os.path.dirname(__file__), 'Sample.html')
        self.response.out.write(template.render(path, template_values))


class SampleHandler3(webapp2.RequestHandler):
    def get(self):
        template_values = {
            'handler': 'We are in SampleHandler3',
            'param2': param2 
        }
        path = os.path.join(os.path.dirname(__file__), 'Sample.html')
        self.response.out.write(template.render(path, template_values))

Notice they all go to the same Sample.html file.

Sample.html is just standard html code.

Upvotes: 2

Related Questions