This fast-moving guide introduces web application development with Haskell and Yesod, a potent language/framework combination that supports high-performing applications that are modular, type-safe, and concise. Fully updated for Yesod 1.4, this second edition shows you how Yesod handles widgets, forms, persistence, and RESTful content. Author Michael Snoyman also introduces various Haskell tools to supplement your basic knowledge of the language.
By the time you finish this book, you'll create a production-quality web application with Yesod's ready-to-use scaffolding. You'll also examine several real-world examples, including a blog, a wiki, a JSON web service, and a Sphinx search server.
Build a simple application to learn Yesod's foundation datatype and Web Application Interface (WAI)
Output HTML, CSS, and Javascript with Shakespearean template languages
Get an indepth look at Yesod's core monads for producing cleaner, more modular code
Probe Yesod's internal workings: learn the request handling process for a typical application
Build forms on top of widgets by implementing the yesod-form declarative API
Learn how Yesod and Haskell handle persistence and session data
Serve an HTML page and a machine-friendly JSON page from the same URL
Author(s): Michael Snoyman
Edition: 2
Publisher: O'Reilly Media
Year: 2015
Language: English
Pages: 396
Tags: functional programming
Cover
Table of Contents
Preface
Who This Book Is For
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Part I. Basics
Chapter 1. Introduction
Type Safety
Concise Code
Performance
Modularity
A Solid Foundation
Chapter 2. Haskell
Terminology
Tools
Language Pragmas
Overloaded Strings
Type Families
Template Haskell
QuasiQuotes
API Documentation
Summary
Chapter 3. Basics
Hello, World
Routing
Handler Function
The Foundation
Running
Resources and Type-Safe URLs
Non-HTML Responses
The Scaffolded Site
Development Server
Summary
Chapter 4. Shakespearean Templates
Synopsis
Hamlet (HTML)
Lucius (CSS)
Cassius (CSS)
Julius (JavaScript)
Types
Type-Safe URLs
Syntax
Hamlet Syntax
Lucius Syntax
Cassius Syntax
Julius Syntax
Calling Shakespeare
Alternative Hamlet Types
Other Shakespeare
General Recommendations
Chapter 5. Widgets
Synopsis
What’s in a Widget?
Constructing Widgets
Combining Widgets
Generating IDs
whamlet
Types
Using Widgets
Using Handler Functions
Summary
Chapter 6. The Yesod Typeclass
Rendering and Parsing URLs
joinPath
cleanPath
defaultLayout
getMessage
Custom Error Pages
External CSS and JavaScript
Smarter Static Files
Authentication/Authorization
Some Simple Settings
Summary
Chapter 7. Routing and Handlers
Route Syntax
Pieces
Resource Name
Handler Specification
Dispatch
Return Type
Arguments
The Handler Functions
Application Information
Request Information
Short-Circuiting
Response Headers
I/O and Debugging
Query String and Hash Fragments
Summary
Chapter 8. Forms
Synopsis
Kinds of Forms
Types
Converting
Creating AForms
Optional Fields
Validation
More Sophisticated Fields
Running Forms
i18n
Monadic Forms
Input Forms
Custom Fields
Values That Don’t Come from the User
Summary
Chapter 9. Sessions
clientsession
Controlling Sessions
Session Operations
Messages
Ultimate Destination
Summary
Chapter 10. Persistent
Synopsis
Solving the Boundary Issue
Types
Code Generation
PersistStore
Migrations
Uniqueness
Queries
Fetching by ID
Fetching by Unique Constraint
Select Functions
Manipulation
Insert
Update
Delete
Attributes
Relations
A Closer Look at Types
More Complicated, More Generic
Custom Fields
Persistent: Raw SQL
Integration with Yesod
More Complex SQL
Something Besides SQLite
Summary
Chapter 11. Deploying Your Web App
Keter
Compiling
Files to Deploy
SSL and Static Files
Warp
Nginx Configuration
Server Process
Nginx + FastCGI
Desktop
CGI on Apache
FastCGI on lighttpd
CGI on lighttpd
Part II. Advanced
Chapter 12. RESTful Content
Request Methods
Representations
JSON Conveniences
New Data Types
Other Request Headers
Summary
Chapter 13. Yesod’s Monads
Monad Transformers
The Three Transformers
Example: Database-Driven Navbar
Example: Request Information
Performance and Error Messages
Adding a New Monad Transformer
Summary
Chapter 14. Authentication and Authorization
Overview
Authenticate Me
Email
Authorization
Summary
Chapter 15. Scaffolding and the Site Template
How to Scaffold
File Structure
Cabal File
Routes and Entities
Foundation and Application Modules
Import
Handler Modules
widgetFile
defaultLayout
Static Files
Summary
Chapter 16. Internationalization
Synopsis
Overview
Message Files
Specifying Types
RenderMessage typeclass
Interpolation
Phrases, Not Words
Chapter 17. Creating a Subsite
Hello, World
Chapter 18. Understanding a Request
Handlers
Layers
Content
Short-Circuit Responses
Dispatch
toWaiApp, toWaiAppPlain, and warp
Generated Code
Complete Code
Summary
Chapter 19. SQL Joins
Multiauthor Blog
Database Queries in Widgets
Joins
Esqueleto
Streaming
Summary
Chapter 20. Yesod for Haskellers
Hello, Warp
What About Yesod?
The HandlerT Monad Transformer
(To)Content, (To)TypedContent
HasContentType and Representations
Convenience warp Function
Writing Handlers
Getting Request Parameters
Short-Circuiting
Streaming
Dynamic Parameters
Routing with Template Haskell
LiteApp
Shakespeare
The URL Rendering Function
Widgets
Details We Won’t Cover
Part III. Examples
Chapter 21. Initializing Data in the Foundation Data Type
Step 1: Define Your Foundation
Step 2: Use the Foundation
Step 3: Create the Foundation Value
Summary
Chapter 22. Blog: i18n, Authentication, Authorization, and Database
Chapter 23. Wiki: Markdown, Chat Subsite, Event Source
Subsite: Data
Subsite: Handlers
Subsite: Widget
Master Site: Data
Master Site: Instances
Master Site: Wiki Handlers
Master Site: Running
Summary
Chapter 24. JSON Web Service
Server
Client
Chapter 25. Case Study: Sphinx-Based Search
Sphinx Setup
Basic Yesod Setup
Searching
Streaming xmlpipe Output
Full Code
Chapter 26. Visitor Counter
Chapter 27. Single-Process Pub/Sub
Foundation Data Type
Allocate a Job
Fork Our Background Job
View Progress
Complete Application
Chapter 28. Environment Variables for Configuration
Chapter 29. Route Attributes
Alternative Approach: Hierarchical Routes
Part IV. Appendices
Appendix A. monad-control
Overview
Intuition
Types
MonadTransControl
MonadControlIO
Real-Life Examples
Lost State
More Complicated Cases
Appendix B. Web Application Interface
The Interface
Response Body
Request Body
Hello, World
Resource Allocation
Streaming Response
Middleware
Appendix C. Settings Types
Appendix D. http-conduit
Synopsis
Concepts
Request
Manager
Response
http and httpLbs
Appendix E. xml-conduit
Synopsis
Types
The Four Types of Nodes
Documents
Events
Text.XML
A Note About File Paths
Cursor
xml-hamlet
xml2html
Index
About the Author