PingHandler struct

Fields:

  • svc (PingService)

Methods:

Pong


Parameters:
  • ctx *fasthttp.RequestCtx

Show/Hide Method Body
{
	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)
}

pingResponse struct

Fields:

  • Message ([]string) - json:"message"

init function

Show/Hide Function Body
{
	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",
					},
				},
			},
		}
	})

}

PingService interface

Methods:

Pong


Returns:
  • string

pingService struct

Implements:

  • PingService from ping

Methods:

Pong


Returns:
  • string

Show/Hide Method Body
{ return "pong" }

encoding/json import

Import example:

import "encoding/json"

strconv import

Import example:

import "strconv"

github.com/valyala/fasthttp import

Import example:

import "github.com/valyala/fasthttp"

github.com/mirkobrombin/go-module-router/v1/registry import

Import example:

import "github.com/mirkobrombin/go-module-router/v1/registry"

github.com/valyala/fasthttp import

Import example:

import "github.com/valyala/fasthttp"