- Create a simple Golang application that will serve a RESTful API with a resource (books). The endpoints should support Create, Read, Update and Delete operations.
- The data should be persisted in a database like SQLite, MySQL, PostgreSQL, etc.
- Add tests for you REST API.
- Create a dockerfile for your application.
Endpoints:
# Get all books
GET /books
# Get a specific book
GET /books/:id
# Create a book
POST /books
# Update a book
PUT /books/:id
# Delete a book
DELETE /books/:id
Model:
{
"id": 1,
"title": "The Alchemist",
"author": "Paulo Coelho",
"published": 1988
}
This package can be used to connect to a local SQLite database:
If you want an ORM (Object Relational Mapping) library, you can use GORM:
For creating a HTTP server, you can use echo:
Documentation for testing echo endpoints:
to run test cases
go test -v
Repository pattern is a design pattern that we will use in our application. It's basically a way of encapsulating the data access layer logic In the context of Go and databases, it's common to use repositories in order to abstract away the underlying data source from your business logic.
- https://www.linkedin.com/pulse/what-repository-pattern-alper-sara%C3%A7/
- https://medium.com/@pererikbergman/repository-design-pattern-e28c0f3e4a30
For Create a dockerfile for your application
To build the docker image run this command in your terminal from root directory of project:
docker build -t go-restapi .
After building the image, you can start it with this command:
docker run --name restapi -d -p 8080:8080 go-restapi