logo资料库

Building Web Applications with Erlang.2012.英文.pdf

第1页 / 共154页
第2页 / 共154页
第3页 / 共154页
第4页 / 共154页
第5页 / 共154页
第6页 / 共154页
第7页 / 共154页
第8页 / 共154页
资料共154页,剩余部分请下载后查看
Table of Contents
Preface
Who This Book Is For
Learning Erlang
Before You Start
What You Will Learn
The Limits of This Book
Help! It Doesn’t Compile or Run!
Diagnosing the Error
What Version of Erlang and Yaws Are You Running?
Is Everything Loaded Correctly?
Are You Calling Everything Correctly?
Is Mnesia Running with Correct Tables?
Is the Example Just Plain Wrong?
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Chapter 1. Building Scalable Systems with Erlang and REST
Why Erlang?
Erlang’s Advantages
Lack of Types
OTP—For More Than Just Telecom!
Why Web Services? Why REST?
New Opportunities for Scaling and Resilience
Cloud Computing
System Architecture and Erlang Scaling
Scaling up versus scaling out
Amdahl’s law
Data Storage Options
Mnesia
CouchDB
MongoDB
Redis
Riak
Chapter 2. Getting Started with Yaws
Working with Yaws
Starting Yaws
Serving Static Files
Compiling, Loading, and Running Code
Clustering Yaws
Dynamic Content in Yaws
EHTML
Headers and Redirects
Templates
ErlyDTL
Logging
Erlang OTP error_logger
Chapter 3. Appmods: Dynamic Content in Yaws
Appmod Configuration
When the URI Does Not Correspond to a File
Cookies
Session Handling
Access Control
Interacting with Erlang Services and Business Logic Layers
Chapter 4. Implementing REST
Decoding a Request
Extracting the User’s Request
Response and Headers
Building the Response
JSON
XML
Responding to the REST Request
A Full Example
Chapter 5. File Upload
The File Upload Request
Saving to Disk
Putting It All Together
Storage in a Distributed System
Saving to Amazon S3
Chapter 6. WebSockets
The WebSocket Request
Basic WebSocket Handler
Advanced WebSocket Handler
Chapter 7. Streaming
Simple Streaming
Chapter 8. Using the HTTP Client
Making a Request
Using OAuth
Facebook Canvas
Chapter 9. Building an Application with OTP
Directory Structure
Building an Application Server
The Generic Server
The Multicast Server
Interfacing the Server with the Web
Some Client-Side Code
Let’s Have Some Adult Supervision Around Here!
A Little Optimization
Bundling as an Application
The App File
Wrapping Up OTP
Appendix A. Installing Erlang and Yaws
Appendix B. Beyond Yaws
Web Servers
Cowboy
MochiWeb
Misultin
Web Frameworks
Chicago Boss
Nitrogen
Zotonic
Appendix C. Interfacing with Ruby and Python
Ruby
Python
Appendix D. Using Erlang with Emacs
Distel
Flymake Mode
Gen Server Template
Building Web Applications with Erlang Zachary Kessin Beijing•Cambridge•Farnham•Köln•Sebastopol•Tokyo
Building Web Applications with Erlang by Zachary Kessin Copyright © 2012 Zachary Kessin. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Simon St. Laurent Production Editor: Melanie Yarbrough Proofreader: Emily Quill Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Revision History for the First Edition: 2012-06-04 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449309961 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Building Web Applications with Erlang, the cover image of a Silver Moony, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-30996-1 [LSI] 1338840029
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Why Erlang? Erlang’s Advantages Lack of Types OTP—For More Than Just Telecom! 1. Building Scalable Systems with Erlang and REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 2 3 4 4 6 6 7 9 Cloud Computing System Architecture and Erlang Scaling Data Storage Options Why Web Services? Why REST? New Opportunities for Scaling and Resilience Working with Yaws Starting Yaws Serving Static Files Compiling, Loading, and Running Code Clustering Yaws 2. Getting Started with Yaws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 16 16 17 18 20 21 24 25 26 26 30 31 EHTML Headers and Redirects Dynamic Content in Yaws Erlang OTP error_logger Templates ErlyDTL Logging 3. Appmods: Dynamic Content in Yaws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 34 34 35 Appmod Configuration When the URI Does Not Correspond to a File Cookies iii
Session Handling Access Control Interacting with Erlang Services and Business Logic Layers 36 38 39 4. 5. Response and Headers Building the Response Implementing REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 41 Decoding a Request 41 43 45 47 49 51 51 Responding to the REST Request A Full Example Extracting the User’s Request JSON XML File Upload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 The File Upload Request 59 61 Saving to Disk 63 Putting It All Together 65 Storage in a Distributed System Saving to Amazon S3 66 6. WebSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 70 72 74 The WebSocket Request Basic WebSocket Handler Advanced WebSocket Handler 7. Streaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 79 Simple Streaming 8. Using the HTTP Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 83 86 86 Making a Request Using OAuth Facebook Canvas 9. Building an Application with OTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 92 93 93 96 101 102 104 Directory Structure Building an Application Server The Generic Server The Multicast Server Interfacing the Server with the Web Some Client-Side Code Let’s Have Some Adult Supervision Around Here! iv | Table of Contents
A Little Optimization Bundling as an Application The App File Wrapping Up OTP 108 114 115 117 A. Installing Erlang and Yaws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 B. Beyond Yaws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 C. Interfacing with Ruby and Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 D. Using Erlang with Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Table of Contents | v
分享到:
收藏