Repository Initialization and Module Configuration
The objective is to scaffold a convention-driven REST API structure using the Iris web libray. This foundation consolidates recurring patterns to accelerate subsequent feature implementation.
Create a remote repository through your preferred version control platform. Clone the empty repository into a dedicated local workspace directory.
# Create workspace and initialize repository
mkdir api-framework-base
cd api-framework-base
git init
git remote add origin https://github.com/yourusername/api-framework-base.git
Bootstrap the Go module system and fetch the framework dependency:
go mod init api-framework-base
go get github.com/kataras/iris/v12@latest
Application Antry Point Design
Generate the primary bootstrap file. The following implementation demonstrates route grouping, automatic response compression, and structured JSON payload binding.
package main
import (
"log"
"github.com/kataras/iris/v12"
)
type CatalogEntry struct {
Identifier int `json:"identifier"`
Title string `json:"title"`
Status string `json:"status"`
}
func registerRoutes(app *iris.Application) {
// Enable global response compression
app.Use(iris.Compression)
// Establish a namespaced route group
ns := app.Party("/catalog")
{
ns.Get("/", retrieveAll)
ns.Post("/", publishItem)
}
}
func retrieveAll(ctx iris.Context) {
data := []CatalogEntry{
{Identifier: 101, Title: "Quantum Modules", Status: "active"},
{Identifier: 102, Title: "Neural Connectors", Status: "archived"},
{Identifier: 103, Title: "Data Mesh Services", Status: "active"},
}
ctx.StatusCode(iris.StatusOK)
ctx.JSON(data)
}
func publishItem(ctx iris.Context) {
var incoming CatalogEntry
if err := ctx.ReadJSON(&incoming); err != nil {
log.Printf("Binding error: %v", err)
ctx.StatusCode(iris.StatusBadRequest)
ctx.Stop()
return
}
incoming.Identifier = generateSequentialID()
incoming.Status = "pending_review"
log.Printf("Ingested payload: %+v", incoming)
ctx.StatusCode(iris.StatusCreated)
ctx.JSON(map[string]interface{}{
"message": "item registered successfully",
"result": incoming,
})
}
func generateSequentialID() int {
return 1004
}
func main() {
binder := iris.New()
registerRoutes(binder)
listenAddress := ":8090"
log.Printf("API gateway bound to %s", listenAddress)
if err := binder.Listen(listenAddress); err != nil {
log.Fatalf("Runtime failure: %v", err)
}
}
Service Execution and Endpoint Validation
Compile and launch the service directly from the commend line:
go run main.go
A successful initialization prints the binding interface. Verify routing and serialization by issuing a request through a browser or CLI tool:
GET http://127.0.0.1:8090/catalog
Receiving a well-formed JSON array confirms that the middleware pipeline, route table, and context managers are operating correctly.