Let's start by defining a few endpoints for our service. We'll be implementing the logic for each of these endpoints later; for now, we'll ensure the following routes are present in the API and return 200 OK responses.
User Routes
For the purpose of these routes, you can assume the user's handle looks like @userName
. Don't worry about encoding/decoding the @
symbol in the URL.
POST /api/users/:handle
- Creates a new userGET /api/users/:handle/posts
- Returns all posts of a user by their handle, ordered by time
Post Routes
POST /api/posts
- Creates a new postGET /api/posts/:id
- Returns a post by its ID
Follower Routes
POST /api/follow/:handle
- Follows a user by their handleGET /api/following
- Returns all users you're followingDELETE /api/follow/:handle
- Unfollows a user by their handle
Feed Routes
GET /api/feed/:handle
- Returns all posts of the people the user is following, ordered by time.
Routes library basics
We'll be using the Routes library to define our endpoints.
There are two top-level functions for defining a service endpoint, Route.noCapture
and Route.route
. Use Route.noCapture
if your route does not capture any path parameters or Route.route
, if it extracts a path segment for use as a variable.
The Routes library provides a few functions, such as ok.text
or headers.add
, that help build HTTP responses. Combining the two, route definition and HTTP response construction, will create a complete endpoint:
api.createPost : '{Route} ()
api.createPost = do
use Parser / s
Route.noCapture POST (s "api" / s "posts")
ok.text "ok"
api.getPostById : '{Route} ()
api.getPostById =
do
use Parser / s
postId =
PostId
(Route.route
GET (s "api" / s "posts" / Parser.text))
ok.text "ok"
We recommend defining one function per route, and then combining them all together with Route.<|>
in a final call to Route.run
. Route.run
produces a function that takes an HttpRequest
and returns an HttpResponse
, which is the expected type for deploying an HTTP service.
Route.run : '{g, Route, Exception} ()
-> HttpRequest
->{g, Exception} HttpResponse
📚 Instructions
Define the remaining endpoints by having them return a 200 OK response for each route. The response body does not matter for now.
We've provided two route stubs to use as a reference for this part of the exercise:
api.getPostById
- "GET /api/posts/:id"api.createPost
- "POST /api/posts"
Deploy all your routes as an HTTP service inside the provided ex3_microblog.deploy
function. It should return the URI where the service is deployed.
cloud-start/main> edit ex3_microblog.deploy
When you've implemented all the routes, update
your codebase and run your solution.
cloud-start/main> run submit.ex3_microblog.pt1