9.6. HTTP Server
Part 1: Ping and Logger
Tasks
- Run a server and implement a handler which returns
pongon the endpoint/ping. - Create a middleware to log every request. Log the path, method, duration and the IP of the client of the request.
Your log should look similar to this:
2022/04/11 10:03:08 remote=192.168.1.143 path=/foo method=GET duration=13.765874ms
Tips
See 5.4. HTTP Server.
Standard library packages
Measure duration
start := time.Now()
// perform action
duration := time.Now().Sub(start)
Part 2: User API
Tasks
Create a JSON REST API where you can create, list and delete users.
The API should provide the following endpoints:
POST /user: create a userGET /user: returns all usersGET /user/<ID>: returns a single user by IDDELETE /user/<ID>: delete user by ID
The user should look like this:
{
"name": "john",
"full_name": "John Doe",
"followers": 13
}
You can decide how you would like to store the users. One option would be to store them only in-memory for example in a map map[int]User. Another option would be to store serialize the users into a file (e.g. with JSON).
Tips
See 5.4. HTTP Server.
Part 3: User API Client
Tasks
Create CLI tool to list, create and delete users:
# create user
./usercli create bob
./usercli create bob "Bob Meier"
./usercli create bob "Bob Meier" 34
# list all users
./usercli get
# show specific user
./usercli get bob
# delete user
./usercli delete bob
Tips
Take a look at the github-info-client example to get an idea on how to implement this.
Solutions
- Part 1: Ping and Log Middleware: https://github.com/acend/go-basics-training-examples/tree/master/http-server
- Part 2 + 3: User API: https://github.com/acend/go-basics-training-examples/tree/master/user-api
Last modified May 29, 2024: Merge pull request #205 from acend/renovate/actions-checkout-digest (cc47d9a)