Skip to main content

Implementing Counter Handler

Storage for counter

Before implementing handler we want to store our current counter somewhere. For the purpose of this tutorial we would store it in memory, but where should we define it?

Every handler needs some common dependencies, like logger, config, clients, and for that Mify provides MifyRequestContext struct (not for ExpressJS yet, though). It also supports custom dependencies, and this is where we can put our counter, so let's do it!

Here are the files you need to update for each language:

type ServiceExtra struct {
    // Append your dependencies here
    Counter int
}

func NewServiceExtra(ctx *core.MifyServiceContext) (*ServiceExtra, error) {
    // Here you can do your custom service initialization, prepare dependencies
    extra := &ServiceExtra{
        // Here you can initialize your dependencies
        Counter: 0,
    }
    return extra, nil
}

Getting counter in handler

Now we can finally implement the handler:

// CounterNextGet - get next number
func (s *CounterNextApiService) CounterNextGet(ctx *core.MifyRequestContext) (openapi.ServiceResponse, error) {
    svcCtx := apputil.GetServiceExtra(ctx.ServiceContext()) // get custom dependencies from context
    currentNumber := svcCtx.Counter

    svcCtx.Counter++

    return openapi.Response(200, openapi.CounterNextResponse{
        Number: int32(currentNumber),
    }), nil
}

Add import for apputil.GetServiceExtra and remove unused ones:

import (
    "example.com/namespace/counting-project/go-services/internal/counting-backend/apputil"
    "example.com/namespace/counting-project/go-services/internal/counting-backend/generated/api"
    "example.com/namespace/counting-project/go-services/internal/counting-backend/generated/core"
)