We are going to build the IMDB of books. Or actually, only the review section. In this assignment you will implement a couple of endpoints and interact with the PostgreSQL database. You can view the OpenAPI specification for the book review API on ReDoc.
First, we need to be able to connect to our database from our computer. To do so we can use kubectl port-forward to forward local port 5432 to port 5432 in the Kubernetes cluster:
# Port forward syntax
kubectl -n <namespace> port-forward <resource> <ports..>
# Connect to pod in 'testing' with name 'http-pod-123', map localhost:5432->remote:5432
kubectl -n testing port-forward pods/http-pod-123 5432:5432
# Connect to service in 'testing' with name 'test-svc', map localhost:9000->remote:5432
kubectl -n testing port-forward svc/test-svc 9000:5432
<aside> 💡 Connect to localhost:PORT with psql to verify if the connection works
</aside>
As there is no way to insert new books we need to start with some testing data for a couple of books. You can import the books dataset with psql or by executing in the pod and running the SQL statements manually.
No-one likes to write SQL queries by hand which is why were going to use an ORM in Go. Checkout the GORM documentation to get started. Now, connect to the PostgreSQL cluster and make sure everything works.
• Use db.AutoMigrate(..) to automatically create a table for the Review model