/
Restful.hs
451 lines (356 loc) · 15.5 KB
/
Restful.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Snap.Restful
( CRUD (..)
, Resource (..)
, DBId (..)
, initRest
, resourceRouter
, resourceRoutes
, rootPath
-- , crudPath
, indexPath
, createPath
, showPath
, newPath
, editPath
, updatePath
, destroyPath
, itemActionPath
, templatePath
, resourceSplices
, itemSplices
, itemSplices'
, redirToItem
, HasFormlet(..)
, validDate
, simpleDateFormlet
, HasSplices(..)
, prefixSplices
, liftSplices
, relativeRedirect
) where
------------------------------------------------------------------------------
import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.Trans
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Char (toUpper)
import Data.Default
import Data.Int
import qualified Data.Map as M
import Data.Readable
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Time
import Data.Typeable
import Data.Word
import GHC.Generics
import Relational.Types
import Snap.Core
import Snap.Snaplet
import Snap.Snaplet.Heist
import System.Locale
import Text.Digestive
import Text.Templating.Heist
------------------------------------------------------------------------------
class HasFormlet a where
formlet :: Monad m => Formlet Text m a
instance HasFormlet String where formlet = string
instance HasFormlet Text where formlet = text
instance HasFormlet Int where formlet = stringRead "must be an integer"
instance HasFormlet Integer where formlet = stringRead "must be an integer"
instance HasFormlet Float where formlet = stringRead "must be a float"
instance HasFormlet Double where formlet = stringRead "must be a double"
instance HasFormlet Int8 where
formlet = stringRead "must be an integer"
instance HasFormlet Int16 where
formlet = stringRead "must be an integer"
instance HasFormlet Int32 where
formlet = stringRead "must be an integer"
instance HasFormlet Int64 where
formlet = stringRead "must be an integer"
instance HasFormlet Word8 where
formlet = stringRead "must be a positive integer"
instance HasFormlet Word16 where
formlet = stringRead "must be a positive integer"
instance HasFormlet Word32 where
formlet = stringRead "must be a positive integer"
instance HasFormlet Word64 where
formlet = stringRead "must be a positive integer"
instance HasFormlet PK32 where
formlet d = PK32 <$> stringRead "must be a primary key" (unPK32 <$> d)
instance HasFormlet PK64 where
formlet d = PK64 <$> stringRead "must be a primary key" (unPK64 <$> d)
instance HasFormlet FK32 where
formlet d = FK32 <$> stringRead "must be a foreign key" (unFK32 <$> d)
instance HasFormlet FK64 where
formlet d = FK64 <$> stringRead "must be a foreign key" (unFK64 <$> d)
validDate :: Text -> Result Text Day
validDate = maybe (Error "invalid date") Success .
parseTime defaultTimeLocale "%F" . T.unpack
dayText :: Day -> Text
dayText = T.pack . formatTime defaultTimeLocale "%F"
------------------------------------------------------------------------------
-- | A simple formlet for dates that
simpleDateFormlet :: (Monad m)
=> Maybe Day -> Form Text m Day
simpleDateFormlet d = validate validDate $
text (dayText <$> d)
------------------------------------------------------------------------------
-- | Type class for uniform creation of splices. For primitives that don't
-- have field names the splices should be a list with one element and an empty
-- string for the tag name.
class HasSplices a where
splices :: (Monad m) => a -> [(Text, Splice m)]
instance HasSplices String where
splices x = [("", textSplice $ T.pack x)]
instance HasSplices Text where
splices x = [("", textSplice x)]
instance HasSplices Int where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Integer where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Float where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Double where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Int8 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Int16 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Int32 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Int64 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Word8 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Word16 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Word32 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices Word64 where
splices x = [("", textSplice $ T.pack $ show x)]
instance HasSplices PK32 where
splices = splices . unPK32
instance HasSplices PK64 where
splices = splices . unPK64
instance HasSplices FK32 where
splices = splices . unFK32
instance HasSplices FK64 where
splices = splices . unFK64
instance HasSplices Day where
splices = splices . dayText
instance HasSplices a => HasSplices (Maybe a) where
splices Nothing = [("", textSplice "")]
splices (Just x) = splices x
liftSplices :: [(Text, HeistT (Handler b b) a)]
-> [(Text, SnapletHeist b v a)]
liftSplices = map (second liftHeist)
------------------------------------------------------------------------------
-- | Adds a prefix to the tag names for a list of splices. If the existing
-- tag name is empty, then the new tag name is just the prefix. Otherwise the
-- new tag name is the prefix followed by an underscore followed by the
-- existing name.
prefixSplices :: Text -> [(Text, Splice m)] -> [(Text, Splice m)]
prefixSplices pre = map f
where
f (t,v) = if T.null t then (pre,v) else (T.concat [pre,"_",t], v)
data CRUD = RIndex
-- ^ Gets an item index
| RShow
-- ^ Gets a single item
| RNew
-- ^ Get the form for creating a new item
| REdit
-- ^ Get the form for editing an item
| RCreate
-- ^ Create a new item
| RUpdate
-- ^ Update an item
| RDestroy
-- ^ Delete an item
deriving (Eq,Show,Read,Ord)
newtype DBId = DBId { unDBId :: Word64 }
deriving (Eq,Show,Read,Ord,Num,Generic,Typeable)
instance Default DBId where
def = DBId 0
instance Readable DBId where fromText = return . DBId <=< fromText
data Resource b v a = Resource {
rName :: Text
-- ^ A name for this resource
, rRoot :: Text
-- ^ URL root for this resource
, rHandlers :: [(CRUD, Handler b v a)]
-- ^ Standard CRUD handlers
, rResourceActions :: [(Text, Handler b v a)]
-- ^ Additional resource level handlers
, rItemActions :: [(Text, Handler b v a)]
-- ^ Additional resource instance/item level handlers
}
instance Default (Resource b v a) where
def = Resource "items" "/items" [] [] []
------------------------------------------------------------------------------
-- | One-stop convenience function to enable RESTful resources in your
-- application. Call this function from your initializer passing it all of
-- your resources and it will add the routes and splices for you.
initRest :: HasHeist b => [Resource b v ()] -> Initializer b v ()
initRest resources = do
let splices = concatMap resourceSplices resources
routes = concatMap resourceRoutes resources
addSplices splices
addRoutes routes
------------------------------------------------------------------------------
resourceRoutes :: Resource b v a -> [(ByteString, Handler b v a)]
resourceRoutes r@Resource{..} =
map (mkCrudRoute r) rHandlers ++
map (mkResourceRoute r) rResourceActions ++
map (mkItemRoute r) rItemActions
------------------------------------------------------------------------------
resourceRouter :: Resource b v a -> Handler b v a
resourceRouter = route . resourceRoutes
------------------------------------------------------------------------------
mkItemRoute Resource{..} (act, h) =
(T.encodeUtf8 $ T.intercalate "/" [rRoot, ":id", act], h)
------------------------------------------------------------------------------
mkResourceRoute Resource{..} (act, h) =
(T.encodeUtf8 $ T.intercalate "/" [rRoot, act], h)
------------------------------------------------------------------------------
mkCrudRoute :: MonadSnap m
=> Resource b v a -> (CRUD, m a) -> (ByteString, m a)
mkCrudRoute r@Resource{..} (crud, h) =
case crud of
RIndex -> (T.encodeUtf8 rRoot, ifTop $ method GET h)
RCreate -> ( T.encodeUtf8 rRoot
, ifTop $ method POST (setCreateAction h))
RShow -> ( T.encodeUtf8 $ T.intercalate "/" [rRoot, ":id"]
, ifTop $ method GET h)
RNew -> ( T.encodeUtf8 $ T.intercalate "/" [rRoot, "new"]
, ifTop $ method GET (setCreateAction h))
REdit -> ( T.encodeUtf8 $ T.intercalate "/" [rRoot, ":id", "edit"]
, ifTop $ method GET (setEditAction h))
RUpdate -> ( T.encodeUtf8 $ T.intercalate "/" [rRoot, ":id"]
, ifTop $ method POST (setEditAction h))
RDestroy -> ( T.encodeUtf8 $ T.intercalate "/" [rRoot, ":id", "destroy"]
, ifTop $ method POST h)
where
setCreateAction h = setFormAction (createPath r) h
setEditAction h = do
_id <- getParam "id"
maybe h (\i -> setFormAction (updatePath r (DBId i)) h) (fromBS =<<_id)
------------------------------------------------------------------------------
-- | Return heist template location for given crud action
templatePath Resource{..} crud =
case crud of
RIndex -> B.intercalate "/" [r, "index"]
RCreate -> error "Create action does not get a template."
RShow -> B.intercalate "/" [r, "show"]
RNew -> B.intercalate "/" [r, "new"]
REdit -> B.intercalate "/" [r, "edit"]
RUpdate -> error "Update action does not get a template."
RDestroy -> error "Destroy action does not get a template."
where
r = T.encodeUtf8 rRoot
------------------------------------------------------------------------------
--crudPath :: Resource b v a -> CRUD -> DBId -> Text
--crudPath Resource{..} crud DBId{..} =
-- case crud of
-- RIndex -> rRoot
-- RCreate -> rRoot
-- RShow -> T.intercalate "/" [rRoot, showT unDBId]
-- RNew -> T.intercalate "/" [rRoot, "new"]
-- REdit -> T.intercalate "/" [rRoot, showT unDBId, "edit"]
-- RUpdate -> T.intercalate "/" [rRoot, showT unDBId]
-- RDestroy -> T.intercalate "/" [rRoot, showT unDBId, "destroy"]
-- x -> error $ "Unimplemented crudpath " ++ show x
------------------------------------------------------------------------------
itemActionPath Resource{..} t DBId{..} =
T.intercalate "/" [rRoot, showT unDBId, t]
------------------------------------------------------------------------------
indexPath :: Resource b v a -> Text
indexPath r = rRoot r
------------------------------------------------------------------------------
createPath :: Resource b v a -> Text
createPath r = rRoot r
------------------------------------------------------------------------------
newPath :: Resource b v a -> Text
newPath r = T.intercalate "/" [rRoot r, "new"]
------------------------------------------------------------------------------
rootPath :: Resource b v a -> Text
rootPath = indexPath
------------------------------------------------------------------------------
editPath :: Resource b v a -> DBId -> Text
editPath r (DBId _id) = T.intercalate "/" [rRoot r, showT _id, "edit"]
------------------------------------------------------------------------------
showPath :: Resource b v a -> DBId -> Text
showPath r (DBId _id) = T.intercalate "/" [rRoot r, showT _id]
------------------------------------------------------------------------------
updatePath :: Resource b v a -> DBId -> Text
updatePath r (DBId _id) = T.intercalate "/" [rRoot r, showT _id]
------------------------------------------------------------------------------
destroyPath :: Resource b v a -> DBId -> Text
destroyPath r (DBId _id) = T.intercalate "/" [rRoot r, showT _id, "destroy"]
------------------------------------------------------------------------------
resourceSplices :: Resource b v a -> [(Text, SnapletSplice b v)]
resourceSplices r@Resource{..} =
[ (T.concat [rName, "NewPath"], liftHeist . textSplice $ newPath r)
, (T.concat [rName, "IndexPath"], liftHeist . textSplice $ indexPath r)
, (T.concat [rName, "CreatePath"], liftHeist . textSplice $ createPath r)
, (T.concat [rName, "Path"], liftHeist . textSplice $ rootPath r)
-- This splice is designed to be used in create and update forms to specify
-- the correct action URL.
, ("RESTFormAction", getFormAction)
]
setFormAction a = localRequest f
where
f req = req { rqParams = M.insert "RESTFormAction" [T.encodeUtf8 a]
(rqParams req) }
getFormAction = do
p <- liftHandler $ getParam "RESTFormAction"
maybe (return []) (liftHeist . textSplice . T.decodeUtf8) p
------------------------------------------------------------------------------
itemSplices :: Monad m => Resource b v a -> DBId -> [(Text, Splice m)]
itemSplices r@Resource{..} dbid =
map (mkItemActionSplice r dbid . fst) rItemActions ++
[ (T.concat [rName, "ItemEditPath"], textSplice $ editPath r dbid)
, (T.concat [rName, "ItemShowPath"], textSplice $ showPath r dbid)
, (T.concat [rName, "ItemUpdatePath"], textSplice $ updatePath r dbid)
, (T.concat [rName, "ItemDestroyPath"], textSplice $ destroyPath r dbid)
, (T.concat [rName, "ItemNewPath"], textSplice $ newPath r)
, (T.concat [rName, "ItemIndexPath"], textSplice $ indexPath r)
, (T.concat [rName, "ItemCreatePath"], textSplice $ createPath r)
]
------------------------------------------------------------------------------
itemSplices':: Resource b v a -> DBId -> [(Text, SnapletSplice b v)]
itemSplices' r = map (second liftHeist) . itemSplices r
------------------------------------------------------------------------------
mkItemActionSplice :: Monad m
=> Resource b v a -> DBId -> Text -> (Text, Splice m)
mkItemActionSplice r@Resource{..} dbid t =
( T.concat [rName, "Item", cap t, "Path"]
, textSplice $ itemActionPath r t dbid)
------------------------------------------------------------------------------
-- | Redirect to given item's default show page
redirToItem :: MonadSnap m => Resource b v a -> DBId -> m a
redirToItem r dbid = redirect . T.encodeUtf8 $ showPath r dbid
------------------------------------------------------------------------------
showT :: Show a => a -> Text
showT = T.pack . show
------------------------------------------------------------------------------
cap t =
case T.uncons t of
Just (h, rest) -> T.cons (toUpper h) rest
Nothing -> t
relativeRedirect :: MonadSnap m => B.ByteString -> m b
relativeRedirect _path = do
root <- withRequest (return . rqContextPath)
redirect $ root `B.append` _path