.,*/(#####(/*,. .,*((###(/*.
.*(%%%%%%%%%%%%%%#/. .*#%%%%####%%%%#/.
./#%%%%#(/,,...,,***. ....... *#%%%#*. ,(%%%#/.
.(#%%%#/. .*(#%%%%%%%##/,. ,(%%%#* ,(%%%#*.
.*#%%%#/. .......... .*#%%%%#(/((#%%%%(, ,/#%%%#(/#%%%#(,
./#%%%(* ,#%%%%%%%%(* .*#%%%#* .*#%%%#, *(%%%%%%%#(,.
./#%%%#* ,(((##%%%%(* ,/%%%%/. .(%%%#/ .*#%%%#(*/(#%%%#/,
,#%%%#(. ,#%%%(* ,/%%%%/. .(%%%#/ ,/%%%#/. .*#%%%(,
*#%%%%(*. ,#%%%(* .*#%%%#* ./#%%%#, ,(%%%#* .(%%%#*
,(#%%%%%##(((##%%%%(* .*#%%%%#(((##%%%%(, .*#%%%##(///(#%%%#/.
.*/###%%%%%%%###(/, .,/##%%%%%##(/,. .*(##%%%%%%##(*,
......... ...... .......
- Set values by exporting them into environment variables
export DB_DRIVER=postgres
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=user
export DB_PASS=password
export DB_NAME=go8_db
It is also possible to set them in env
file. Just make sure this file is ignored in .gitignore
because it should never be checked into source control.
- Fill in your database credentials in
.env
by making a copy ofenv.example
first.
cp env.example .env
Have a database ready either by installing them yourself or the following command. The docker-compose.yml
will use database credentials set in either .env
file or environment variables which is initialized in the previous step. Optionally, you may want redis as well.
docker-compose up -d postgres
# or
docker-compose up -d postgres redis
# or
docker compose -f docker-compose.local.yml up --build postgres
Once the database is up you may run the migration with the following command:
go run cmd/migrate/main.go
You will see a bunch of dependencies download for a first time run followed by the sql migration files
2023/02/05 19:23:48 connecting to database...
2023/02/05 19:23:48 connecting connected
2023/02/05 19:23:48 OK 20221213140051_create_books.sql
2023/02/05 19:23:48 OK 20221213140144_create_authors.sql
2023/02/05 19:23:48 OK 20221213140219_create_book_authors.sql
2023/02/05 19:23:48 goose: no migrations to run. current version: 20221213140219
Run the API with the following command.
go run cmd/go8/main.go
You will see the address of the API is running at.
2021/10/31 10:49:11 Starting API version: v0.1.0
2021/10/31 10:49:11 Connecting to database...
2021/10/31 10:49:11 Database connected
.,*/(#####(/*,. .,*((###(/*.
.*(%%%%%%%%%%%%%%#/. .*#%%%%####%%%%#/.
./#%%%%#(/,,...,,***. ....... *#%%%#*. ,(%%%#/.
.(#%%%#/. .*(#%%%%%%%##/,. ,(%%%#* ,(%%%#*.
.*#%%%#/. .......... .*#%%%%#(/((#%%%%(, ,/#%%%#(/#%%%#(,
./#%%%(* ,#%%%%%%%%(* .*#%%%#* .*#%%%#, *(%%%%%%%#(,.
./#%%%#* ,(((##%%%%(* ,/%%%%/. .(%%%#/ .*#%%%#(*/(#%%%#/,
,#%%%#(. ,#%%%(* ,/%%%%/. .(%%%#/ ,/%%%#/. .*#%%%(,
*#%%%%(*. ,#%%%(* .*#%%%#* ./#%%%#, ,(%%%#* .(%%%#*
,(#%%%%%##(((##%%%%(* .*#%%%%#(((##%%%%(, .*#%%%##(///(#%%%#/.
.*/###%%%%%%%###(/, .,/##%%%%%##(/,. .*(##%%%%%%##(*,
......... ...... .......
2021/10/31 10:49:11 Serving at 0.0.0.0:3080
To use, open a new terminal and follow examples in the examples/
folder
Create a book:
curl -v --location --request POST 'http://localhost:3080/api/v1/book' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "Test title",
"image_url": "https://example.com",
"published_date": "2020-07-31T15:04:05.123499999Z",
"description":
"test description"
}' \
| jq
Retrieve all books:
curl --location --request GET 'http://localhost:3080/api/v1/book' | jq
To see all available routes, run
go run cmd/route/main.go
To run all tests,
go test ./...
Create migration
migrate create -ext sql -dir database/migrations "name"