{
n, err := strconv.Atoi(string(ctx.QueryArgs().Peek("times")))
if err != nil || n < 1 {
n = 1
}
out := make([]string, n)
for i := 0; i < n; i++ {
out[i] = h.svc.Pong()
}
payload, _ := json.Marshal(pingResponse{Message: out})
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetContentType("application/json")
_, _ = ctx.Write(payload)
}
{
registry.RegisterService("PingService",
func(_ map[string]any) any { return pingService{} })
registry.RegisterHandler("PingHandler",
func(s map[string]any) any {
return &PingHandler{svc: s["PingService"].(PingService)}
})
registry.RegisterRoutes(func() []registry.Route {
return []registry.Route{
{
Method: fasthttp.MethodGet,
Path: "/api/v1/ping",
HandlerName: "PingHandler.Pong",
Meta: map[string]any{
"summary": "Ping – returns one or more \"pong\" strings",
"description": "Optional query-param `times` repeats the reply. Example: `/api/v1/ping?times=3`",
"parameters": []any{
map[string]any{
"name": "times",
"in": "query",
"required": false,
"schema": map[string]any{
"type": "integer",
"minimum": 1,
},
},
},
"responses": map[int]any{
200: "PingResponse",
},
},
},
}
})
}
{ return "pong" }
import "encoding/json"
import "strconv"
import "github.com/valyala/fasthttp"
import "github.com/mirkobrombin/go-module-router/v1/registry"
import "github.com/valyala/fasthttp"