How to Add a New Endpoint
This guide shows how to add an API endpoint to Txlog Server. The example is an endpoint with the name "ping".
Step 1: Define the Route
Open main.go and add the route:
go
// In main.go, inside the v1Group block:
v1Group.GET("/ping", v1API.GetPing())Step 2: Create the Controller
Create the file controllers/api/v1/ping_controller.go. Write the handler, with comments for the Swagger documentation:
go
package v1
import (
"net/http"
"github.com/gin-gonic/gin"
)
// GetPing returns a simple pong response
//
// @Summary Ping
// @Description Returns pong
// @Tags utility
// @Accept json
// @Produce json
// @Success 200 {object} map[string]string
// @Router /v1/ping [get]
func GetPing() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}
}Step 3: Update the Documentation
Generate the Swagger documentation again:
bash
make docStep 4: Test the Endpoint
Start the server and call the endpoint:
bash
curl http://localhost:8080/v1/ping