基本上我正在构建类似于此GitHub项目的应用程序:https : //github.com/zackargyle/angularjs-django-rest-framework-seed
是否可以将后端和前端都部署到单个PaaS(例如Heroku / Elastic Beanstalk)上?
具有分离的REST后端和JavaScript前端似乎是一种更清洁/更具扩展性的处理方式,而不是像[django-angular]那样将它们混合在一起:((http://django-angular.readthedocs.org/en/latest/ index.html /),或将REST后端与Django应用程序混合使用,例如http://blog.mourafiq.com/post/55099429431/end-to-end-web-app-with-django-rest-framework
如果无法轻松地将其部署到Elastic Beanstalk上,是否有一种简单的方法以最少的配置将Django后端部署到Elastic Beanstalk上,并将AngularJS前端部署到Amazon EC2 / S3?
我意识到在此之前也有类似的讨论:Client JS + Django Rest Framework,但缺少更多具体细节。
我使用AngularJS作为客户端,使用django-rest-framework作为我的服务。我还具有相同类型的git设置,其中服务器和客户端代码是同一个存储库中的同级。我没有使用Heroku的任何经验,并且对beantalk并不陌生,但是我能够部署我的网站,并且可以在AWS beantalk上运行。
通过beantalk,我知道有两种方法来部署代码。
我使用python脚本自动化了zip的创建。亚马逊的演练提供了一个示例python zip。您必须正确地构建它,我的看起来大致像这样
app.zip
/.ebextensions/
/.elasticbeanstalk/
/app/ <-- my django-rest-framework project (settings.py, wsgi.py, etc.)
/restapi/ <-- my django-rest-framework application (my api)
/static/ <-- AngularJS results of 'grunt build' put here
/manage.py
/requirements.txt
我知道您并没有特别要求,但是.ebextensions /中的.config文件使我花了很长时间才能上班。可以将其格式化为YAML或JSON(起初可能会造成混淆,因为每个博客的显示方式都不相同)。这个博客帮了我很多忙,只需要小心使用container_commands:而不是command:。我为此花了几个小时...
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
option_settings:
"aws:elasticbeanstalk:container:python:environment":
"DJANGO_SETTINGS_MODULE": "app.settings"
"aws:elasticbeanstalk:container:python":
"WSGIPath": "app/wsgi.py"
"StaticFiles": "/static/=static/"
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
"aws:elasticbeanstalk:application:environment":
"AWS_SECRET_KEY": "<put your secret key here if you want to reference from env variable>"
"AWS_ACCESS_KEY_ID": "<put your access key here>"
"AWS_S3_Bucket": "<put your bucket here>"
在您创建的zip中(如果遵循django上的beantalk指南),在部署时,/ static /文件夹中的客户端代码会自动推送到s3。
这种设置并不完美,我计划进行一些微调,但可以正常工作。我遇到了一些尚未解决的缺点:
更新4-17-2014
我进一步完善了此设置,因此不再需要去mysite.com/static/加载index.html。为此,我使用了基于django类的视图来将index.html映射到网站的根目录。我的urls.py看起来像
urlpatterns = patterns('',
(r'^$', TemplateView.as_view(template_name="index.html")),
...
)
并且在我的settings.py中,我对TEMPLATE_DIRS进行了如下配置
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__) , '../static').replace('\\','/')
)
我使用../static,因为我的静态目录是我的应用程序目录的同级文件。
最后一步是更新我的Gruntfile.js,以便“ grunt build”将我的角度代码中的所有相对URL前缀为静态文件夹。我为此使用了grunt-text-replace。这是我的代码在/ dist文件夹中最小化后运行的最后一个任务。这种方法的缺点是,如果我将静态内容添加到脚本,bower_components,样式等之外的新子文件夹中,则必须更新此任务。
replace: {
replace_js_templates: {
src: ['dist/scripts/*.js'],
overwrite: true, // overwrite matched source files
replacements: [{
from: /templateUrl:\s*"/g,
to: 'templateUrl:"static/'
}]
},
replace_index: {
src: ['dist/index.html'],
overwrite: true, // overwrite matched source files
replacements: [{
from: /(src|href)="(bower_components|styles|scripts)/g,
to: '$1="static/$2'
}
]
}
},
现在django将为我的index.html页面提供服务,但/ static /目录中的所有其他内容都可以从CDN中受益。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句