The purpose of this article is to provide instructions on how to setup postgres database with our application.
This post belongs to the following series:
- How to Build an API Server in Go - Part 1: Basic Server
- How to Build an API Server in Go - Part 2: Simple Database
- How to Build an API Server in Go - Part 3: Postgres Database
- How to Build an API Server in Go - Part 4: Access Control
Database Setup
Before we begin, please download and install postgres database onto your computer.
Load up your terminal or postgres GUI and run the following code:
|
|
Afterwords we will setup our application structure. Copy and paste the following:
|
|
Data Layer
The project structure will remain the same as from part 2. We will begin with updating the files in the following order:
- mulberry-server/pkg/db/db.go
- mulberry-server/pkg/models/user.go
- mulberry-server/pkg/models/tsd.go
- mulberry-server/internal/respositories/user.go
- mulberry-server/internal/respositories/tsd.go
Models Package
Let’s begin rewriting the data layer by starting in the db.go file. Please note we will be utilizing a third-party library for handling communication with the postgres database.
db.go
|
|
user.go
|
|
tsd.go
|
|
Repositories Package
Now we will move to the repositories package and start with the user.go file:
user.go
|
|
tsd.go
|
|
Application Layer
We need to update the controllers package to support contexts. The following files need to be updated:
- mulberry-server/internal/controllers/user.go
- mulberry-server/internal/controllers/tsd.go
- mulberry-server/cmd/serve/main.go
Controllers Package
Begin with the user.go file:
user.go
|
|
tsd.go
|
|
Main Package
And finally with the main.go file:
main.go
|
|
Testing
Starting the Server
We are going to use environment variables to load configuration settings for our application.
export MULBERRY_DB_HOST=localhost
export MULBERRY_DB_PORT=5432
export MULBERRY_DB_USER=golang
export MULBERRY_DB_PASSWORD=123password
export MULBERRY_DB_NAME=mulberry_db
And now you can run the code:
go run cmd/serve/main.go
Making API Calls
Let’s begin by making a post. Please note that the timestamp must be formatted using “RFC3339” date/time standard - For more information please read this great article.
Register API
Let’s attempt to register. Start the server and run the following commands in your console:
$ http post 127.0.0.1:5000/api/v1/register email="fherbert@dune.com" \
password="the-spice-must-flow" \
name="Frank Herbert"
The output should be as follows:
HTTP/1.1 200 OK
Content-Length: 105
Content-Type: application/json
Date: Sun, 31 Jan 2021 05:56:09 GMT
{
"message": "You have successfully registered an account.",
"uuid": "9dd2cc0a-934d-4788-8304-1e0b82d9b6e6"
}
Login API
And for our grand finally, run the login command which works:
$ http post 127.0.0.1:5000/api/v1/login email="fherbert@dune.com" \
password="the-spice-must-flow"
Wonderful! The success output will be as follows:
HTTP/1.1 200 OK
Content-Length: 125
Content-Type: application/json
Date: Sun, 31 Jan 2021 05:56:52 GMT
{
"access_token": "TODO: WE WILL FIGURE OUT HOW TO DO THIS IN ANOTHER ARTICLE!",
"uuid": "9dd2cc0a-934d-4788-8304-1e0b82d9b6e6"
}
Create API
Run the following in your console:
$ http post 127.0.0.1:5000/api/v1/time-series-data instrument_uuid="lalala" \
value="123" \
timestamp="2021-01-30T10:20:10.000Z" \
user_uuid="9dd2cc0a-934d-4788-8304-1e0b82d9b6e6"
And you should get the following message:
HTTP/1.1 201 Created
Content-Length: 175
Content-Type: application/json
Date: Sun, 31 Jan 2021 05:58:06 GMT
{
"instrument_uuid": "lalala",
"timestamp": "2021-01-30T10:20:10Z",
"user_uuid": "9dd2cc0a-934d-4788-8304-1e0b82d9b6e6",
"uuid": "a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff",
"value": "123"
}
List API
Run the following in your console:
$ http get 127.0.0.1:5000/api/v1/time-series-data
And you should get the following message:
HTTP/1.1 200 OK
Content-Length: 145
Content-Type: application/json
Date: Sat, 30 Jan 2021 22:37:27 GMT
[
{
"instrument_uuid": "lalala",
"timestamp": "2021-01-30T10:20:10Z",
"user_uuid": "9dd2cc0a-934d-4788-8304-1e0b82d9b6e6",
"uuid": "a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff",
"value": 123
}
]
Retrieve API
Run the following in your console:
$ http get 127.0.0.1:5000/api/v1/time-series-datum/a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff
And you should get the following message:
HTTP/1.1 200 OK
Content-Length: 173
Content-Type: application/json
Date: Sun, 31 Jan 2021 06:00:47 GMT
{
"instrument_uuid": "lalala",
"timestamp": "2021-01-30T10:20:10Z",
"user_uuid": "9dd2cc0a-934d-4788-8304-1e0b82d9b6e6",
"uuid": "a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff",
"value": 123
}
Update API
Run the following in your console:
$ http put 127.0.0.1:5000/api/v1/time-series-datum/a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff \
instrument_uuid="lalala" \
value="321" \
timestamp="2021-01-30T10:20:10.000Z" \
user_uuid="9dd2cc0a-934d-4788-8304-1e0b82d9b6e6"
And you should get the following message:
HTTP/1.1 200 OK
Content-Length: 173
Content-Type: application/json
Date: Sun, 31 Jan 2021 06:01:14 GMT
{
"instrument_uuid": "lalala",
"timestamp": "2021-01-30T10:20:10Z",
"user_uuid": "9dd2cc0a-934d-4788-8304-1e0b82d9b6e6",
"uuid": "a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff",
"value": 321
}
Delete API
Run the following in your console:
$ http delete 127.0.0.1:5000/api/v1/time-series-datum/a8f355f4-3bb4-4741-b98b-a0dbcc7a34ff
And you should get the following message:
HTTP/1.1 200 OK
Content-Length: 0
Content-Type: application/json
Date: Sun, 31 Jan 2021 06:01:54 GMT
What’s next?
That’s it for this article. Hope you enjoyed it. What’s next?
- How do we handle sessions? Access tokens?
- How do we handle background processes?
- How do we handle pagination in our list API endpoint?
Now onward to the next part of our series: Part 4: Access Control ».