golang docker REST API sample
- go
- gorm
- gorilla
- crypto
- godotenv
- mysql
- jwt
Method | URI | Authority | |
---|---|---|---|
Confirming endpoint that only returns a strings | GET | /api/v1 | - |
Method | URI | Authority | |
---|---|---|---|
Signin | POST | /api/v1/signin | - |
Signup | POST | /api/v1/signup | - |
Method | URI | Authority | |
---|---|---|---|
Getting all todo list associated with user | GET | /api/v1/todo | Verified |
Getting a single Todo associated with Todo id | GET | /api/v1/todo/:id | Verified |
Creating a Todo | POST | /api/v1/todo | Verified |
Updating a Todo | PUT | /api/v1/todo/:id | Verified |
Deleting a Todo | DELETE | /api/v1/todo/:id | Verified |
- Create ".env" file directly under the root directory
- Copy the description of ".env.sample"
touch .env
- Go to the app directory and create a ".env" file
- Coty the description of "app/.env.sample"
cd app
touch .env
- Build
docker compose build
- Startup container
docker compose up
- Access go container
make backend-ssh
- Perform migration
- In the go container, execute the following commands.
make db-migrate
-
A table will be created, so connect to the DB(MySQL) and check it.
- "Sequel Ace" is the recommended connection application
- https://qiita.com/ucan-lab/items/b1304eee2157dbef7774
-
Perform seeding
-
In the go container, execute the following commands.
make db-seed
- Data is created, so connect to the DB(MySQL) and check it.
- Now that the data has been created in the DB, restart docker again to launch the API(first time only)
docker compose restart
- Connct to the following url and confirm that a response is returned
※Nothing should be run with the API started in docker.
make test
make lint
go-add-library name="[library name]"
// When specifying multiple libraries, enclose them in "", such as name="xxx yyy"
Since gorm does not have a rollback function, erase the entire DB with the following command
docker compose down -v
Then start docker and do the migration again to initialize the table
// build
docker-compose build
// start container
docker-compose up
// start container (background execution)
docker-compose up -d
// stop container
docker-compose down
// Stop container and delete volume (delete DB data)
docker-compose down -v
// Login to app container
docker exec -it 20211105_go_rest_server sh
// Login to DB container
docker exec -it 20211105_go_rest_db /bin/bash