实例介绍
【实例截图】
【核心代码】
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Part I. Basics
1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Type Safety 1
Concise Code 2
Performance 2
Modularity 3
A Solid Foundation 3
2. Haskell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Terminology 5
Tools 6
Language Pragmas 7
Overloaded Strings 8
Type Families 9
Template Haskell 10
QuasiQuotes 12
API Documentation 12
Summary 12
3. Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Hello, World 13
Routing 14
Handler Function 16
The Foundation 16
iii
Running 17
Resources and Type-Safe URLs 17
Non-HTML Responses 19
The Scaffolded Site 19
Development Server 20
Summary 20
4. Shakespearean Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Synopsis 21
Hamlet (HTML) 22
Lucius (CSS) 22
Cassius (CSS) 22
Julius (JavaScript) 22
Types 23
Type-Safe URLs 24
Syntax 25
Hamlet Syntax 26
Lucius Syntax 31
Cassius Syntax 33
Julius Syntax 33
Calling Shakespeare 33
Alternative Hamlet Types 35
Other Shakespeare 37
General Recommendations 38
5. Widgets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Synopsis 39
What’s in a Widget? 41
Constructing Widgets 42
Combining Widgets 43
Generating IDs 44
whamlet 44
Types 45
Using Widgets 46
Using Handler Functions 48
Summary 49
6. The Yesod Typeclass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Rendering and Parsing URLs 51
joinPath 53
cleanPath 53
defaultLayout 55
iv | Table of Contents
getMessage 56
Custom Error Pages 57
External CSS and JavaScript 58
Smarter Static Files 59
Authentication/Authorization 60
Some Simple Settings 61
Summary 61
7. Routing and Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Route Syntax 63
Pieces 64
Resource Name 66
Handler Specification 67
Dispatch 68
Return Type 68
Arguments 69
The Handler Functions 70
Application Information 71
Request Information 71
Short-Circuiting 71
Response Headers 72
I/O and Debugging 73
Query String and Hash Fragments 74
Summary 75
8. Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Synopsis 77
Kinds of Forms 79
Types 80
Converting 82
Creating AForms 82
Optional Fields 83
Validation 84
More Sophisticated Fields 85
Running Forms 86
i18n 87
Monadic Forms 87
Input Forms 90
Custom Fields 91
Values That Don’t Come from the User 93
Summary 95
Table of Contents | v
9. Sessions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
clientsession 97
Controlling Sessions 98
Session Operations 99
Messages 100
Ultimate Destination 102
Summary 104
10. Persistent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Synopsis 106
Solving the Boundary Issue 107
Types 108
Code Generation 109
PersistStore 112
Migrations 113
Uniqueness 116
Queries 117
Fetching by ID 117
Fetching by Unique Constraint 118
Select Functions 118
Manipulation 120
Insert 120
Update 122
Delete 123
Attributes 123
Relations 126
A Closer Look at Types 127
More Complicated, More Generic 128
Custom Fields 129
Persistent: Raw SQL 130
Integration with Yesod 132
More Complex SQL 134
Something Besides SQLite 134
Summary 135
11. Deploying Your Web App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Keter 137
Compiling 138
Files to Deploy 138
SSL and Static Files 139
Warp 139
Nginx Configuration 140
vi | Table of Contents
Server Process 142
Nginx FastCGI 142
Desktop 143
CGI on Apache 144
FastCGI on lighttpd 144
CGI on lighttpd 145
Part II. Advanced
12. RESTful Content. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Request Methods 149
Representations 150
JSON Conveniences 152
New Data Types 154
Other Request Headers 158
Summary 158
13. Yesod’s Monads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Monad Transformers 159
The Three Transformers 160
Example: Database-Driven Navbar 161
Example: Request Information 163
Performance and Error Messages 165
Adding a New Monad Transformer 166
Summary 170
14. Authentication and Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Overview 171
Authenticate Me 172
Email 176
Authorization 180
Summary 182
15. Scaffolding and the Site Template. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
How to Scaffold 183
File Structure 184
Cabal File 184
Routes and Entities 185
Foundation and Application Modules 185
Import 186
Handler Modules 187
Table of Contents | vii
widgetFile 187
defaultLayout 188
Static Files 188
Summary 189
16. Internationalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Synopsis 191
Overview 193
Message Files 194
Specifying Types 195
RenderMessage typeclass 195
Interpolation 196
Phrases, Not Words 197
17. Creating a Subsite. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Hello, World 199
18. Understanding a Request. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Handlers 203
Layers 204
Content 205
Short-Circuit Responses 206
Dispatch 206
toWaiApp, toWaiAppPlain, and warp 207
Generated Code 208
Complete Code 212
Summary 214
19. SQL Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Multiauthor Blog 215
Database Queries in Widgets 217
Joins 218
Esqueleto 219
Streaming 220
Summary 222
20. Yesod for Haskellers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Hello, Warp 225
What About Yesod? 230
The HandlerT Monad Transformer 232
(To)Content, (To)TypedContent 235
HasContentType and Representations 236
viii | Table of Contents
Convenience warp Function 238
Writing Handlers 238
Getting Request Parameters 238
Short-Circuiting 239
Streaming 239
Dynamic Parameters 241
Routing with Template Haskell 242
LiteApp 244
Shakespeare 245
The URL Rendering Function 247
Widgets 247
Details We Won’t Cover 248
Part III. Examples
21. Initializing Data in the Foundation Data Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Step 1: Define Your Foundation 252
Step 2: Use the Foundation 252
Step 3: Create the Foundation Value 252
Summary 253
22. Blog: i18n, Authentication, Authorization, and Database. . . . . . . . . . . . . . . . . . . . . . . . 255
23. Wiki: Markdown, Chat Subsite, Event Source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Subsite: Data 265
Subsite: Handlers 266
Subsite: Widget 269
Master Site: Data 271
Master Site: Instances 272
Master Site: Wiki Handlers 273
Master Site: Running 274
Summary 275
24. JSON Web Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Server 277
Client 278
25. Case Study: Sphinx-Based Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Sphinx Setup 281
Basic Yesod Setup 282
Searching 285
Table of Contents | ix
Streaming xmlpipe Output 288
Full Code 290
26. Visitor Counter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
27. Single-Process Pub/Sub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Foundation Data Type 299
Allocate a Job 300
Fork Our Background Job 300
View Progress 301
Complete Application 301
28. Environment Variables for Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
29. Route Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Alternative Approach: Hierarchical Routes 309
Part IV. Appendices
A. monad-control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
B. Web Application Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
C. Settings Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
D. http-conduit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
E. xml-conduit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论