github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/infra/conf/api.go (about)

     1  package conf
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/xmplusdev/xmcore/app/commander"
     7  	loggerservice "github.com/xmplusdev/xmcore/app/log/command"
     8  	observatoryservice "github.com/xmplusdev/xmcore/app/observatory/command"
     9  	handlerservice "github.com/xmplusdev/xmcore/app/proxyman/command"
    10  	routerservice "github.com/xmplusdev/xmcore/app/router/command"
    11  	statsservice "github.com/xmplusdev/xmcore/app/stats/command"
    12  	ruleservice "github.com/xmplusdev/xmcore/app/router/command"
    13  	"github.com/xmplusdev/xmcore/common/serial"
    14  )
    15  
    16  type APIConfig struct {
    17  	Tag      string   `json:"tag"`
    18  	Services []string `json:"services"`
    19  }
    20  
    21  func (c *APIConfig) Build() (*commander.Config, error) {
    22  	if c.Tag == "" {
    23  		return nil, newError("API tag can't be empty.")
    24  	}
    25  
    26  	services := make([]*serial.TypedMessage, 0, 16)
    27  	for _, s := range c.Services {
    28  		switch strings.ToLower(s) {
    29  		case "reflectionservice":
    30  			services = append(services, serial.ToTypedMessage(&commander.ReflectionConfig{}))
    31  		case "handlerservice":
    32  			services = append(services, serial.ToTypedMessage(&handlerservice.Config{}))
    33  		case "loggerservice":
    34  			services = append(services, serial.ToTypedMessage(&loggerservice.Config{}))
    35  		case "statsservice":
    36  			services = append(services, serial.ToTypedMessage(&statsservice.Config{}))
    37  		case "observatoryservice":
    38  			services = append(services, serial.ToTypedMessage(&observatoryservice.Config{}))
    39  		case "routingservice":
    40  			services = append(services, serial.ToTypedMessage(&routerservice.Config{}))
    41  		case "ruleservice":
    42  			services = append(services, serial.ToTypedMessage(&ruleservice.Config{}))	
    43  		}
    44  	}
    45  
    46  	return &commander.Config{
    47  		Tag:     c.Tag,
    48  		Service: services,
    49  	}, nil
    50  }