52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
|
|
service "go.unistack.org/cms-service"
|
|
)
|
|
|
|
type App struct {
|
|
TLSCrt string `flag:"name=account.tls_crt,desc='tls crt file'"`
|
|
TLSKey string `flag:"name=account.tls_key,desc='tls key file'"`
|
|
Address string `flag:"name=account.address,desc='listen address',default=':0'"`
|
|
}
|
|
|
|
type Config struct {
|
|
App *App
|
|
Storage *service.ConfigStorage
|
|
Logger *service.ConfigLogger
|
|
Service *service.ConfigService
|
|
Core *service.ConfigCore
|
|
}
|
|
|
|
func NewConfig() *Config {
|
|
return &Config{
|
|
Service: &service.ConfigService{
|
|
Name: ServiceName,
|
|
Version: ServiceVersion,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *App) Validate() error {
|
|
_, port, err := net.SplitHostPort(c.Address)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid address: %s, err: %w", c.Address, err)
|
|
}
|
|
|
|
if port == "443" && (c.TLSCrt == "" || c.TLSKey == "") {
|
|
return fmt.Errorf("use secured connection without tls crt/key")
|
|
}
|
|
|
|
if v, err := strconv.Atoi(port); err != nil {
|
|
return fmt.Errorf("invalid address/port: %v", c.Address)
|
|
} else if v < 0 || v > 65535 {
|
|
return fmt.Errorf("invalid address/port: %v", c.Address)
|
|
}
|
|
|
|
return nil
|
|
}
|