Last active
September 24, 2015 14:00
-
-
Save ToJans/0066cb7478d3409ca6c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE TypeOperators #-} | |
module StorageServer where | |
import Models | |
import Servant.API | |
import ServantHelpers | |
import StorageDB | |
type ProjectAPI = Get '[JSON] ProjectList -- GET / -> return ProjectList | |
:<|> Capture "pId" ProjectId :> Get '[JSON] Project -- GET /:id -> return Project | |
:<|> ReqBody '[JSON] Project :> Post '[JSON] Project -- POST / -> return Project (requestBody JSON Project) | |
:<|> Capture "pId" ProjectId :> Delete '[JSON] ProjectId -- DELETE /:id -> return ProjectId | |
type TenantAPI = | |
Get '[JSON] Tenants -- GET / -> return Tenants | |
:<|> Capture "tId" TenantId :> Get '[JSON] Tenant -- GET /:id -> return Tenant | |
:<|> ReqBody '[JSON] Tenant :> Post '[JSON] Tenant -- POST / -> return Tenant (requestBody JSON Tenant) | |
:<|> Capture "tId" TenantId :> Delete '[JSON] TenantId -- DELETE /:id -> return TenantId | |
type AdminAPI = "builddatabase" :> Get '[JSON] String -- GET /builddatabase -> return String | |
type StorageAPI = "tenants" :> TenantAPI -- /tenants/ -> parent of TenantAPI | |
:<|> "tenants" :> Capture "tId" TenantId :> "projects" :> ProjectAPI -- /tenants/:id/projects -> Parent of ProjectAPI | |
:<|> "admin" :> AdminAPI -- /admin/ -> parent of AdminAPI | |
projectServer :: TenantId -> Server ProjectAPI -- implement the code for ProjectAPI | |
projectServer tId = -- the tenantId comes from the parent | |
liftIO (getProjectListForTenant tId) -- GET / -> return ProjectList | |
:<|> liftIOMaybeToExceptT err404 . findProject tId -- GET /:id -> return Project | |
:<|> liftIOMaybeToExceptT err400 . insertProject -- POST / -> return Project (requestBody JSON Project) | |
:<|> liftIO . deleteProject tId -- DELETE /:id -> return ProjectId | |
tenantServer :: Server TenantAPI | |
tenantServer = projectServer | |
:<|> liftIO getTenants | |
:<|> liftIOMaybeToExceptT err404 . findTenant | |
:<|> liftIOMaybeToExceptT err400 . insertTenant | |
:<|> liftIO . deleteTenant | |
adminServer :: Server AdminAPI | |
adminServer = liftIO buildDatabase | |
storageAPI :: Proxy StorageAPI | |
storageAPI = Proxy | |
storageServer :: Server StorageAPI | |
storageServer = tenantServer :<|> adminServer | |
-- $ curl -H "Content-Type: application/json" -X POST -d '{"tenantId":1,"tenantName":"facebook"}' http://localhost:8081/tenants/ | |
-- {"tenantName":"facebook","tenantId":2} | |
-- $ curl -H "Content-Type: application/json" -X POST -d '{"projectId":1,"projectTenantId":1,"projectDescription":"a project","projectContent" | |
-- :"some value"}' http://localhost:8081/tenants/1/projects | |
-- {"projectTenantId":1,"projectContent":"some value","projectId":1,"projectDescription":"a project"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment