Skip to content

The simple Rest APIs using golang + jwt authen and more..

License

Notifications You must be signed in to change notification settings

lexuanquynh/go_api

Repository files navigation

golang_api

Create the sample APIs, using clean architecture.

Create by: codetoanbug.com

Require:

Step to run this project:

  • 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":"codetoanbug@gmail.com"
}
  • 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":"codetoanbug@gmail.com"
}
  • 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")

APIs for books:

  • 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

Third party libraries:

License

This repository is released under an MIT license. See License.md for more information.

About

The simple Rest APIs using golang + jwt authen and more..

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages