starter pack for creating gokit project
Add library event for wrapping publish and subscribe nats
Library for publishing event to nats (begin and commit) as a middleware in transport
//Create publisher
eventPublisher := event.NewPublisher("nats_connection", "logger")
//Implementation in transport or as endpoint Go-Kit
eventPublisher.Store("domain", "model", "eventtype", "topic/subject", "event_source",
func(ctx context.Context, request interface{}) (response interface{}, err error) {
result, err := service.Create(reqData.Name, reqData.Code)
if err != nil {
return nil, err
}
return result, nil
},
func(metaBuilder interface{}) interface{} {
//filter data or build meta data in here before data published into nats
return metaBuilder
},
)
Description :
NewPublisher
Param | Description |
---|---|
nats_connection | nats connection type stan.Conn |
logger | logger for logging type from gokit log |
.Store
Param | Description |
---|---|
domain | Your domain ex: account, authorization |
model | Your model from your domain |
eventtype | Event Type ex: create, update |
topic/subject | Topic/Subject for nats |
event_source | Event Source befor this event |
func | Enpoint gokit |
MetaBuilder | func(metaBuilder interface{}) interface{} |
Library for subscribe event from nats.
assessmentApproveSub := event.NewSubscriber("nats_connection", "topic/subject", "qGroup", "durable_name", "startAt", "logger", func(msg *stan.Msg) {
var tmp map[string]interface{}
if err := json.Unmarshal(msg.Data, &tmp); err != nil {
logger.Log(err)
}
logger.Log("nats", fmt.Sprintf("Incoming message from topic/subject with data %s", tmp))
}).Subscribe()
Description :
NewSubscriber
Param | Description |
---|---|
nats_connection | nats connection type stan.Conn |
topic/subject | Topic/Subject for nats |
qGroup | Queue Group fill this with your domain name |
durable_name | Durable subscription ex :authorization-sub |
startat | Start at |
(avaliable option : | |
all, | |
seqno ex:sqno:100, | |
time ex:time:1559291755, | |
since (click here for information) ex:since:2h | |
logger | logger for logging type from gokit log |
func(msg *stan.Msg) | Handler incoming message |
Library for getting configuration. This library will get k/v from 2 paths in Vault.
- From config/global
- From config/{user_defined_path}, existing keys from config/global will be replaced.
//Defining configuration struct
type Config struct {
DebugAddress string
NatsAddress string
}
//Defining default configuration
var defaultConfig = &Config{
DebugAddress: ":9080",
NatsAddress: "nats://localhost:4222",
}
func main() {
error err
//Create Vault connection
vaultConn, err := vault.New()
//Get Configuration from Vault as map[string]string
cfg, err := vaultConn.GetEnvOrDefaultConfig("path", "defaultConfig")
}
Description :
Param | Description |
---|---|
path | specific path in config |
defaultConfig <map[string]string> | default configuration if k/v not found |
Library to write k/v with encrypted value.
//Create Vault connection
vaultConn, err := vault.New()
//Write k/v
encryptedValue, err := vaultConn.WriteEncrypted("transitkey", "path", "value")
Description :
Param | Description |
---|---|
transitkey | key to encrypt value |
path | secret path in Vault |
value <[]byte> | secret value |
Library to read k/v with encrypted value.
//Create Vault connection
vaultConn, err := vault.New()
//Write k/v
value, err := vaultConn.ReadEncrypted("transitkey", "path")
Description :
Param | Description |
---|---|
transitkey | key to decrypt value |
path | secret path + key in Vault |