Creating an Authentication API with Golang
You need to install Go on your PC or MacOS and set your Go workspace first
- The first need Go installed (Latest {stable} version is awesome)
- To get started, Create a new project then, Create a new file called
main.go
and enter the following starter code:
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
- To be able to handle GET and POST requests, we create a
GetMethod
andPostMethod
:
func PostMethod(c *gin.Context) {
fmt.Println("\napi.go 'PostMethod' called")
message := "PostMethod called"
c.JSON(http.StatusOK, message)
}
func GetMethod(c *gin.Context) {
fmt.Println("\napi.go 'GetMethod' called")
message := "GetMethod called"
c.JSON(http.StatusOK, message)
}
- We need to create a
Gin
router for handling all the requests:
func main() {
router := gin.Default()
}
- Following this up, we add in the
GetMethod
andPostMethod
:
func main() {
router := gin.Default()
router.POST("/", PostMethod)
router.GET("/", GetMethod)
}
- we add the port for the server to listen to and run the server: Just top the below code in
Gin
func main() {
router := gin.Default()
router.POST("/", PostMethod)
router.GET("/", GetMethod)
listenPort := "4000"
router.Run(":"+listenPort)
}
- Now, run this code to test if your server is working.
-
Now that you’ve set up our basic web server, we can start adding in the elements for our authentication API.
-
let's start with
func main()
:
func main() {
router := gin.Default()
subRouterAuthenticated := router.Group("/api/v1/PersonId", gin.BasicAuth(gin.Accounts{
"admin": "adminpass",
}))
listenPort := "1357"
router.Run(":"+listenPort)
}
- passing the query string parameters
func main() {
router := gin.Default()
subRouterAuthenticated := router.Group("/api/v1/PersonId", gin.BasicAuth(gin.Accounts{
"admin": "adminpass",
}))
subRouterAuthenticated.GET("/:IdValue", GetMethod)
listenPort := "1357"
router.Run(":"+listenPort)
}
- It fetches and prints the Person
IdValue
from the query string parameter passed in the API URL:
func GetMethod(c *gin.Context) {
fmt.Println("\n'GetMethod' called")
IdValue := c.Params.ByName("IdValue")
message := "GetMethod Called With Param: " + IdValue
c.JSON(http.StatusOK, message)
ReqPayload := make([]byte, 1024)
ReqPayload, err := c.GetRawData()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Request Payload Data: ", string(ReqPayload))
}
- First, run your Go application
go run main.go
- Download ngrok
- Extract the ngrok executable in a folder on your server.
- Start ngrok on port
1357
(which is the port you selected in your code).
- p.s you can configure your port
./ngrok http 1357
- The result:
ngrok by @emmyc (Ctrl+C to quit)
Session Status online
Session Expires 7 hours, 12 minutes
Version 2.3.35
Region Netherlands (nl)
Web Interface http://127.0.0.1:4040
Forwarding http://ca6d2c4cee3e.ngrok.io -> http://localhost:4000
Forwarding https://ca6d2c4cee3e.ngrok.io -> http://localhost:4000
-
This will generate a random dynamic URL where you can test your API and log in with your login details to test if it works.
-
After you log in, it will show:
GetMethod Called With Param: Id456
- Great It works!