App Engine throws 404 Not Found for any path but root

前端 未结 1 992
情深已故
情深已故 2021-01-26 19:49

I want to split my App Engine implementation into several files. So I wrote in my app.yaml file:

runtime: python27
api_version: 1
threadsafe: true

handlers:

-          


        
1条回答
  •  离开以前
    2021-01-26 20:20

    Each app.yaml route script config (like script: imageuploader.app) requires a python file with a matching name (imageuploader.py in this case) which is defining an app called app (just like the main.py does) with app routes being, of course, a subset of (or equal to) the corresponding path from the app.yaml file (/imageuploader in this case).

    So, problems with your implementation:

    • the main.py app path is / - doesn't match the corresponding /search path in app.yaml
    • none of the 3 app path patterns in imageuploader.py matches the corresponding /imageuploader path in app.yaml
    • the / path in app.yaml is actually mapped to the static resource index.html, so it'll be served by GAE directly - no point adding a handler for it as that handler should never be invoked (you may want to revisit the static resources mapping).

    Your attempt using /.* mapped to main.app could only have worked before you removed the handler code now in imageuploader.py, but should not be working with the main.py content from your question.

    Assuming your patterns in the app.yaml are the ones you desire, these would be the app configs you'd need (to continue with your approach):

    main.py:

    app = webapp2.WSGIApplication([
        ('/search', MainPage),
    ], debug=True)
    

    imageuploader.py:

    app = webapp2.WSGIApplication([
        ('/imageuploader', ...),  # TODO: revisit mappings
    ], debug=True)
    

    But I'd suggest a simpler to manage approach, using webapp2's lazy loading of the handler code.

    You keep a single, generic mapping in the app.yaml, which would match any url pattern in the main.py file:

    - url: /.*
      script: main.app
    

    You have a single app route mapping definition in main.py, but which can lazy-load handlers from both main.py as well as other modules/files. For example something like this:

    app = webapp2.WSGIApplication([    
        ('/upload', 'imageuploader.PhotoUploadFormHandler'),
        ('/upload_photo', 'imageuploader.PhotoUploadHandler'),
        ('/view_photo/([^/]+)?', 'imageuploader.ViewPhotoHandler'),
        ('/main_page', MainPage),
    ], debug=True)
    

    A middle-ground approach is also possible, allowing you to completely de-couple imageuploader.py from main.py but with a slightly more careful url pattern choice.

    You could have in app.yaml:

    - url: /photo.*
      script: imageuploader.app
    

    And in imageuploader.py have url patterns matching that /photo.* one:

    app = webapp2.WSGIApplication([
        ('/photo_upload_form', PhotoUploadFormHandler),
        ('/photo_upload', PhotoUploadHandler),
        ('/photo_view/([^/]+)?', ViewPhotoHandler),
    ], debug=True)
    

    0 讨论(0)
提交回复
热议问题