Create the sample APIs, using clean architecture.
Create by: codetoanbug.com
- install go: https://golang.org/doc/install
- install mysql: https://dev.mysql.com/downloads/mysql/
- install workbench: https://dev.mysql.com/downloads/workbench/
- You need start mysql server in your machine.
- Create user name for mysql: root & create password for root user.
- Create database mysql by command line:
export PATH=${PATH}:/usr/local/mysql/bin
mysql -u root -p
create database golang_api;
- Create .env file with contents below, you need change YOUR_PASSWORD_FOR_ROOT:
DB_USER=root
DB_PASS=YOUR_PASSWORD_FOR_ROOT
DB_HOST=localhost
DB_NAME=golang_api
JWT_SECRET=yourkey_keysecret
- Run this project with command line:
go get github.com/mashingan/smapping
go get github.com/golang/crypto
go get github.com/golang-jwt/jwt
go get github.com/joho/godotenv
go get github.com/gin-gonic/gin
go get -u gorm.io/gorm
go get gorm.io/driver/mysql
go run server.go
- Try to use postman and use your APIs POST method:
http://localhost:8080/api/auth/login
http://localhost:8080/api/auth/register
- Header:
key:Content-Type
value: application/json
- Body:
{
"password":"password",
"email":"[email protected]"
}
- User APIs GET method:
http://localhost:8080/api/user/profile
- Header:
key:Content-Type
value: application/json
key: Authorization
value: <token from login api>
- User APIs PUT method:
http://localhost:8080/api/user/profile
- Header:
key:Content-Type
value: application/json
key: Authorization
value: <token from login api>
- Body:
{
"name":"Code toan Bug",
"email":"[email protected]"
}
- If you get error:
Error: connect ECONNREFUSED 127.0.0.1:8080
Please change DB_PASS=YOUR_PASSWORD in .env file!
- If you want to deploy to vps server, you need change code in database-config.go, sample:
errEnv := godotenv.Load("/usr/local/src/golang_api/.env")
- Get books:
[GET] http://localhost:8080/api/books
- Insert book:
[POST] http://localhost:8080/api/books
{
"title":"sach hay",
"description":"sach hay ve van hoc"
}
- Update book:
[PUT] http://localhost:8080/api/books/1
{
"id":1,
"title":"sach hay",
"description":"sach hay ve van hoc nha"
}
- Delete book:
[DELETE] http://localhost:8080/api/books/2
- https://github.com/gin-gonic/gin
- https://github.com/joho/godotenv
- https://github.com/golang-jwt/jwt
- https://github.com/golang/crypto
- https://github.com/mashingan/smapping
This repository is released under an MIT license. See License.md for more information.