Setting service

If your application requires configurations that might change or predefined, you need to use setting service. You should save your settings in SettingsEntity, then use this service to fetch it.

Register the service into your main.go file:

registry.ServiceProviderSetting()
1

Access the service:

service.DI().Setting()
1

Use case

Imagine you need to restrict access to login page after certain number of failed login attempts. You can simply store this value in SettingsEntity and fetch it using this service:

package save 
import (
 "service"
)

func SaveConfig(){
    ormService := service.DI().ORMEngine()
	ormService.Flush(&entity.SettingsEntity{
		Key:       "user.login.threshold",
		Value:     "3",
		ValueType: entity.SettingsValueTypeAll.SettingsValueTypeNumber,
	})
}
1
2
3
4
5
6
7
8
9
10
11
12
13

Then later in your login package, you can retrieve this value and use it:

package login
import (
    "errors"
    "service"
)

func Login(currentCount uint64) error {
    ormService := service.DI().ORMEngine()
    allowed, found := service.DI().Setting().GetUint64(ormService, "user.login.threshold")
    if found && currentCount> allowed{
        return errors.New("too many login attempt")
    }  
    return nil  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Last Updated: 3/10/2022, 8:38:05 AM
Contributors: h-khodadadeh, Saman Shahroudi