Skip to content

Commit

Permalink
chore: delete ini config file
Browse files Browse the repository at this point in the history
  • Loading branch information
notacommonperson committed Jan 21, 2024
1 parent 6ce195b commit a3e42be
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 104 deletions.
23 changes: 0 additions & 23 deletions example/client/kitex_client.ini

This file was deleted.

57 changes: 7 additions & 50 deletions example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"context"
"fmt"
"encoding/json"
"log"
"os"
"os/signal"
Expand All @@ -25,23 +25,18 @@ import (
"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
kitexclient "github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/circuitbreak"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpctimeout"
fileclient "github.com/kitex-contrib/config-file/client"
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
"github.com/kitex-contrib/config-file/utils"
"gopkg.in/ini.v1"
)

const (
filepath = "kitex_client.ini"
key = "ClientName/ServiceName"
serviceName = "ServiceName"
clientName = "echo"
INI parser.ConfigType = "ini"
filepath = "kitex_client.json"
key = "ClientName/ServiceName"
serviceName = "ServiceName"
clientName = "echo"
)

// Customed by user
Expand All @@ -50,43 +45,7 @@ type MyParser struct{}
// one example for custom parser
// if the type of client config is json or yaml,just using default parser
func (p *MyParser) Decode(kind parser.ConfigType, data []byte, config interface{}) error {
cfg, err := ini.Load(data)
if err != nil {
return fmt.Errorf("load config error: %v", err)
}

cfm := make(parser.ClientFileManager, 0)
cfc := &parser.ClientFileConfig{
Timeout: make(map[string]*rpctimeout.RPCTimeout, 0),
Retry: make(map[string]*retry.Policy, 0),
Circuitbreaker: make(map[string]*circuitbreak.CBConfig, 0),
}

timeout := &rpctimeout.RPCTimeout{}
circ := &circuitbreak.CBConfig{}
ret := &retry.Policy{}
stop := &retry.StopPolicy{}
cb := &retry.CBPolicy{}

cfg.Section(key).MapTo(timeout)
cfg.Section("ClientName/ServiceName.Circuitbreaker.Echo").MapTo(circ)
cfg.Section("ClientName/ServiceName.Retry.*").MapTo(ret)
cfg.Section("ClientName/ServiceName.Retry.*.FailurePolicy.StopPolicy").MapTo(stop)
cfg.Section("ClientName/ServiceName.Retry.*.CBPolicy").MapTo(cb)
stop.CBPolicy = *cb
ret.FailurePolicy = &retry.FailurePolicy{
StopPolicy: *stop,
}

cfc.Timeout[clientName] = timeout
cfc.Circuitbreaker[clientName] = circ
cfc.Retry[clientName] = ret

cfm[key] = cfc

v := config.(*parser.ClientFileManager)
*v = cfm
return err
return json.Unmarshal(data, config)
}

func main() {
Expand All @@ -111,9 +70,7 @@ func main() {
}()

// customed by user
params := &parser.ConfigParam{
Type: INI,
}
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
Expand Down
3 changes: 0 additions & 3 deletions example/server/kitex_server.ini

This file was deleted.

30 changes: 6 additions & 24 deletions example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"context"
"fmt"
"encoding/json"
"log"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
Expand All @@ -28,16 +28,14 @@ import (
"github.com/kitex-contrib/config-file/parser"
fileserver "github.com/kitex-contrib/config-file/server"
"github.com/kitex-contrib/config-file/utils"
"gopkg.in/ini.v1"
)

var _ api.Echo = &EchoImpl{}

const (
filepath = "kitex_server.ini"
key = "ServiceName"
serviceName = "ServiceName"
INI parser.ConfigType = "ini"
filepath = "kitex_server.json"
key = "ServiceName"
serviceName = "ServiceName"
)

// EchoImpl implements the last service interface defined in the IDL.
Expand All @@ -55,20 +53,7 @@ type MyParser struct{}
// one example for custom parser
// if the type of client config is json or yaml,just using default parser
func (p *MyParser) Decode(kind parser.ConfigType, data []byte, config interface{}) error {
cfg, err := ini.Load(data)
if err != nil {
return fmt.Errorf("load config error: %v", err)
}

sfm := make(parser.ServerFileManager, 0)

sfc := &parser.ServerFileConfig{}
cfg.MapTo(sfc)
sfm[key] = sfc

v := config.(*parser.ServerFileManager)
*v = sfm
return err
return json.Unmarshal(data, config)
}

func main() {
Expand All @@ -86,10 +71,7 @@ func main() {
defer fw.StopWatching()

// customed by user
params := &parser.ConfigParam{
Type: INI,
}

params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
Expand Down
6 changes: 3 additions & 3 deletions parser/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (

// ClientFileConfig is config of a client/service pair
type ClientFileConfig struct {
Timeout map[string]*rpctimeout.RPCTimeout `mapstructure:"timeout" ini:"timeout"` // key: method, "*" for default
Retry map[string]*retry.Policy `mapstructure:"retry" ini:"retry"` // key: method, "*" for default
Circuitbreaker map[string]*circuitbreak.CBConfig `mapstructure:"circuitbreaker" ini:"circuitbreaker"` // key: method
Timeout map[string]*rpctimeout.RPCTimeout `mapstructure:"timeout"` // key: method, "*" for default
Retry map[string]*retry.Policy `mapstructure:"retry"` // key: method, "*" for default
Circuitbreaker map[string]*circuitbreak.CBConfig `mapstructure:"circuitbreaker"` // key: method
}

// ClientFileManager is a map of client/service pairs to ClientFileConfig
Expand Down
2 changes: 1 addition & 1 deletion parser/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import "github.com/cloudwego/kitex/pkg/limiter"

// ServerFileConfig is config of a service
type ServerFileConfig struct {
Limit limiter.LimiterConfig `mapstructure:"limit" ini:"ServiceName.Limit"`
Limit limiter.LimiterConfig `mapstructure:"limit"`
}

// ServerFileManager is a map of service names to ServerFileConfig
Expand Down

0 comments on commit a3e42be

Please sign in to comment.