1

I am trying to create simple mod_wsgi application with following structure.

Files:

  1. /var/www/test/application.wsgi - mod_wsgi process
  2. /var/www/test/hello.py - python application
  3. /var/www/test/static/index.html - static content
  4. /etc/apache2/sites-available/test.conf - application configuration

application.wsgi

   import sys
   activate_this = '/home/ubuntu/environments/test/bin/activate_this.py'
   execfile(activate_this, dict(__file__=activate_this))
   sys.path.insert(0, '/var/www/test')
   from hello import app as application

hello.py

   from flask import Flask
   app = Flask(__name__, static_url_path='')

   @app.route("/")
   def root():
       return app.send_static_file('index.html')

index.html

   <html><h1>testing</h1></html>

test.conf

  <VirtualHost *:80>
      ServerName localhost
      WSGIDaemonProcess hello user=ubuntu group=ubuntu threads=5
      WSGIScriptAlias / /var/www/test/application.wsgi
      WSGIScriptReloading On

      <Directory /var/www/test>
         WSGIProcessGroup hello
         WSGIApplicationGroup %{GLOBAL}
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Order allow,deny
         Allow from all
         Require all granted
      </Directory>

      ErrorLog /var/log/test/error.log
      LogLevel warn

      CustomLog /var/log/test/access.log combined
      ServerSignature Off
   </VirtualHost>

The above application works perfectly fine and it displays static content from index.html.

As soon as I introduce Blueprints, the application stops working

Now, the modified files are as follows:

hello.py

  from flask import Flask
  app = Flask(__name__, static_url_path='')

  @app.route("/")
  def root():
     return app.send_static_file('index.html')

  from views.authentication import authentication
  app.register_blueprint(authentication, url_prefix='/test/auth')

/var/www/test/views/authentication.py - facebook authentication

  from flask import Blueprint

  authentication = Blueprint('authentication', __name__)

  @authentication.route('/facebook/', method=['POST'])
  def facebook():
     ''' facebook login '''
     access_token_url = app.config.get('FACEBOOK_ACCESS_TOKEN_URL')
     graph_api_url = app.config.get('FACEBOOK_GRAPH_API_URL')

     pass

As soon as I introduce above Blueprint code, I start receiving 404 error (even with empty method body). Any idea, what could be the reason? I would appreciate any help or pointers.

tennis
  • 91
  • 1
  • 8

1 Answers1

0

You should check your error.log, it's always a good place to start the debugging. I assume there will be a line like this: ImportError: No module named views. That is because you have to put (at least an empty) file named __init__.py in your module directory (in your case it is the views). Here's the explanation from the Python Doc:

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

The second problem is the method=['POST']. It should be methods and you have to add the 'GET' method to reach this page "normally":

@authentication.route('/facebook/', methods=['GET', 'POST'])
def facebook():
    ''' facebook login'''
Dauros
  • 9,294
  • 2
  • 20
  • 28
  • Unfortunately, method vs. methods was a typo. At the same time, I don't receive any errors in logs as relevant code is in place. It's just that, the application's index.html contains dynamic javascript content (bower, bootstrap, etc...) and I am not sure what else is needed to get dynamic content served via python mod_wsgi application. If someone has experience with such mechanism, it would be interesting to find out "How to debug bower components on AWS instance"? – tennis Oct 22 '15 at 15:08
  • Did you make the other two modifications from my answer? You still need those (the `'GET'` method and the `__init__.py` file). For static files, usually you need to define an Alias in the Apache config file, [here's an example](https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/#serving-files). For dynamically generated javascripts no extra step required, just put those in a ` – Dauros Oct 22 '15 at 15:57