2011-12-12 13 views
5

Ora sto migrando la mia piccola app Google App Engine alla piattaforma Heroku. In realtà non utilizzo Bigtable e lo webapp2 riduce molto i miei costi di migrazione.Come posso gestire i file statici con Python webapp2 in Heroku?

Ora sono bloccato sulla gestione dei file statici.

Esiste qualche buona pratica? Se è così, guidami lì per favore.

Grazie in anticipo.

EDIT

Bene, ora sto usando paste per il mio assistente WSGI. E paste.StaticURLParser() dovrebbe essere quello che mi serve per implementare il gestore di file statico. Tuttavia non ho idea di come integrarlo con webapp2.WSGIApplication(). Qualcuno può aiutarmi?

Forse ho bisogno di sovrascrivere la classe webapp2.RequestHandler per caricare correttamente paste.StaticURLParser();

import os 
import webapp2 
from paste import httpserver 

class StaticFileHandler(webapp2.RequestHandler): 
    u"""Static file handler""" 

    def __init__(self): 
     # I guess I need to override something here to load 
     # `paste.StaticURLParser()` properly. 
     pass 

app = webapp2.WSGIApplication([(r'/static', StaticFileHandler)], debug=True) 


def main(): 
    port = int(os.environ.get('PORT', 5000)) 
    httpserver.serve(app, host='0.0.0.0', port=port) 

if __name__ == '__main__': 
    main() 

Qualsiasi aiuto sarebbe apprezzato!

risposta

6

Di seguito è riportato come funziona.

Immagino che affidarsi a un'app a cascata non sia l'opzione più efficiente, ma funziona abbastanza bene per le mie esigenze.

from paste.urlparser import StaticURLParser 
from paste.cascade import Cascade 
from paste import httpserver 
import webapp2 
import socket 


class HelloWorld(webapp2.RequestHandler): 
    def get(self): 
     self.response.write('Hello cruel world.') 

# Create the main app 
web_app = webapp2.WSGIApplication([ 
    ('/', HelloWorld), 
]) 

# Create an app to serve static files 
# Choose a directory separate from your source (e.g., "static/") so it isn't dl'able 
static_app = StaticURLParser("static/") 

# Create a cascade that looks for static files first, then tries the webapp 
app = Cascade([static_app, web_app]) 

def main(): 
    httpserver.serve(app, host=socket.gethostname(), port='8080') 

if __name__ == '__main__': 
    main() 
+0

Grazie per la risposta. Ci proverò più tardi! Non sapevo di "Cascade". – Japboy

+0

Puoi semplicemente servire file statici durante lo sviluppo usando una variabile come: se DEBUG: ecc. E in produzione usa nginx. –

+0

Grazie! cercavo una risposta a questo. – Amirshk

2

rende il codice più facile da implementare poiché è possibile utilizzare nginx per servire i supporti statici in produzione.

def main(): 
    application = webapp2.WSGIApplication(routes, config=_config, debug=DEBUG) 
    if DEBUG: 
    # serve static files on development 
    static_media_server = StaticURLParser(MEDIA_ROOT) 
    app = Cascade([static_media_server, application]) 
    httpserver.serve(app, host='127.0.0.1', port='8000') 
else: 
    httpserver.serve(application, host='127.0.0.1', port='8000') 
2

mi consideri in ritardo al gioco, ma in realtà mi piace DirectoryApp un po 'meglio. Gestisce le cose un po 'di più come AppEngine. Posso creare una directory "statica" nella mia directory src, e poi posso fare riferimento quelli del mio HTML (o altro) in questo modo: http: .. localhost: 8080// static/js jquery.js

static_app = DirectoryApp("static") 

# Create a cascade that looks for static files first, then tries the webapp 
app = Cascade([static_app,wsgi_app]) 

httpserver.serve(app, host='0.0.0.0', 
       port='8080') 
Problemi correlati