Learning golang framework gin. Stack:
- Go
- MySQL
- Docker
Implements:
- Gin
- Gorm MySQL CRUD (currently: create, read)
- JWT Authentication
- Docker using Dockerfile
There is nothing in http://localhost:8080 yet, but you can do :
- Register/Login as an Author
- Save/Get Your Articles
Use Postman for completes endpoints: https://www.getpostman.com/collections/e66fcfef85e70524e3ac
An example of POST
request to save new Article with its Author to http://localhost:8080/api/articles
{
"title": "First Post Title",
"content": "Lorem ipsum this is article content",
"slug": "post-1",
"status": 1
}
Make sure you are already authenticated, otherwise it will respond with 400. To authenticate, you need to :
- Register, make request to http://localhost:8080/author/register
{ "name": "Fajar", "email": "user@mail.com", "status": 1, "username": "user", "password": "user" }
- Login, make request to http://localhost:8080/author/login
{ "username": "user", "password": "user" }
After you clone this repo
git clone https://github.com/fajarbc/learn-gin.git && cd learn-gin
Then, before you could run the app
- install/downloads the dependecies first.
go mod download
- make database
go_articles
- (optional) For development, hot reloading using Air
- Install Air
go get github.com/cosmtrek/air@latest
- Add alias to
.bashrc
- File check where
.bashrc
should beecho ~ // output: C:/Users/fajarbc
- Check your air executable, in my case i found it on
C:\Users\fajarbc\go\bin\bin
- Next, go to
C:/Users/fajarbc
and add this line to file.bashrc
(create it yourself if it's not created yet) :alias air='~/go/bin/bin/air'
~
equalsC:\Users\fajarbc
- File check where
- Check in your terminal by type
air -v
- Install Air
- Don't forget to setup your env by copy file
.env.example
and rename it to.env
and then you can start to change the variables inside it.
There are 4 ways to run the app. Once it runs, you could reach http://localhost:8080
- Using go run, just run this in terminal
go run server.go
- Using air (Install step 3), it will automatically hot reload your golang app, i found this the most comfortable way for developing process for me so far. Just type this to start
air
- Using go build, there are 2 steps :
- Build the binary artifact
- Windows
go build -o server.exe
- Linux
go build -o server
- Windows
- Run the binary artifact
- Build the binary artifact
- Using Dockerfile, there are 2 steps :
- Build docker image
docker build --tag learn-gin:v0.1 .
- Run docker image as container (also expose port 8080 and detach mode)
docker run -d -p 8080:8080 learn-gin:v0.1
- Build docker image