Skip to content

Commit

Permalink
minor: optimize project config
Browse files Browse the repository at this point in the history
  • Loading branch information
jinrenjie committed Sep 16, 2024
1 parent eb7648e commit b2d95fa
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 38 deletions.
37 changes: 37 additions & 0 deletions .cdns.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
ns:
ip: 10.8.10.253

dns:
admin: george.dev
listen: 0.0.0.0:2553
nsname: dev
records:
- ca.svc.dev:
type: A
value: 10.0.88.254
- dns.svc.dev:
type: A
value: 10.0.88.253
protocol: both

http:
tls:
mode: acme # The tls mode support "acme" and "file".
domain: dns.svc.dev
listen: 0.0.0.0:8443

soa:
domain: dev
ingress:
ip: 10.8.10.252
logging:
level: INFO

providers:
acme:
email: [email protected]
server: https://ca.svc.dev/acme/acme/directory
storage: /Users/George/Develop/Go/src/cdns/certs
file:
tlsKey: /certs/domain.tld.key
tlsCert: /certs/domain.tld.crt
8 changes: 5 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# General configration
CDNS_ENV=production
CDNS_NS_IP=10.8.10.253
CDNS_SOA_DOMAIN=dev
CDNS_INGRESS_IP=10.8.10.252
CDNS_LOGGING_LEVEL=INFO

# DNS configration
CDNS_DNS_ADMIN=george.dev
CDNS_DNS_NSNAME=dev
CDNS_DNS_LISTEN=0.0.0.0:53
CDNS_DNS_PROTOCOL=both

# API configuration
CDNS_HTTP_TLS_MODE=acme
CDNS_HTTP_DOMAIN=cdns.svc.dev
CDNS_HTTP_LISTEN=0.0.0.0:443
CDNS_HTTP_USERNAME=traefik
CDNS_HTTP_PASSWORD=

# TLS ACME provider
CDNS_PROVIDERS_ACME_EMAIL=[email protected]
Expand Down
152 changes: 118 additions & 34 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"github.com/betterde/cdns/internal/journal"
"github.com/spf13/viper"
"os"
Expand All @@ -13,77 +14,76 @@ const TLSModeFile = "file"
var Conf *Config

type Config struct {
NS NS `yaml:"ns"`
SOA SOA `yaml:"soa"`
DNS DNS `yaml:"dns"`
HTTP HTTP `yaml:"http"`
Ingress Ingress `yaml:"ingress"`
Logging Logging `yaml:"logging"`
Providers Providers `yaml:"providers"`
NS NS `yaml:"ns" mapstructure:"NS"`
SOA SOA `yaml:"soa" mapstructure:"SOA"`
DNS DNS `yaml:"dns" mapstructure:"DNS"`
HTTP HTTP `yaml:"http" mapstructure:"HTTP"`
Ingress Ingress `yaml:"ingress" mapstructure:"INGRESS"`
Logging Logging `yaml:"logging" mapstructure:"LOGGING"`
Providers Providers `yaml:"providers" mapstructure:"PROVIDERS"`
}

type NS struct {
IP string `yaml:"ip"`
IP string `yaml:"ip" mapstructure:"IP"`
}

type Logging struct {
Level string `yaml:"level"`
Level string `yaml:"level" mapstructure:"LEVEL"`
}

type DNS struct {
Admin string `yaml:"admin"`
Listen string `yaml:"listen"`
NSName string `yaml:"nsname"`
Records map[string]Record `yaml:"records"`
Protocol string `yaml:"protocol"`
Admin string `yaml:"admin" mapstructure:"ADMIN"`
Listen string `yaml:"listen" mapstructure:"LISTEN"`
NSName string `yaml:"nsname" mapstructure:"NSNAME"`
Records map[string]Record `yaml:"records" mapstructure:"RECORDS"`
Protocol string `yaml:"protocol" mapstructure:"PROTOCOL"`
}

type HTTP struct {
TLS TLS `yaml:"tls"`
Domain string `yaml:"domain"`
Listen string `yaml:"listen"`
Username string `yaml:"username"`
Password string `yaml:"password"`
TLS TLS `yaml:"tls" mapstructure:"TLS"`
Domain string `yaml:"domain" mapstructure:"DOMAIN"`
Listen string `yaml:"listen" mapstructure:"LISTEN"`
}

type Record struct {
Type string `yaml:"type"`
Value string `yaml:"value"`
Type string `yaml:"type" mapstructure:"TYPE"`
Value string `yaml:"value" mapstructure:"VALUE"`
}

type Ingress struct {
IP string `yaml:"ip"`
IP string `yaml:"ip" mapstructure:"IP"`
}

type Providers struct {
ACME ACME `yaml:"acme"`
File File `yaml:"file"`
ACME ACME `yaml:"acme" mapstructure:"ACME"`
File File `yaml:"file" mapstructure:"FILE"`
}

type TLS struct {
Mode string `yaml:"mode"`
Mode string `yaml:"mode" mapstructure:"MODE"`
}

type SOA struct {
Domain string `yaml:"domain"`
Domain string `yaml:"domain" mapstructure:"DOMAIN"`
}

type ACME struct {
Email string `yaml:"email"`
Server string `yaml:"server"`
Storage string `yaml:"storage"`
Email string `yaml:"email" mapstructure:"EMAIL"`
Server string `yaml:"server" mapstructure:"SERVER"`
Storage string `yaml:"storage" mapstructure:"STORAGE"`
}

type File struct {
TLSKey string `yaml:"tlsKey"`
TLSCert string `yaml:"tlsCert"`
TLSKey string `yaml:"tlsKey" mapstructure:"TLSKEY"`
TLSCert string `yaml:"tlsCert" mapstructure:"TLSCERT"`
}

func Parse(file string) {
if file != "" {
viper.SetConfigFile(file)
} else {
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName(".cdns")
viper.AddConfigPath("/etc/cdns")
}
Expand All @@ -92,10 +92,94 @@ func Parse(file string) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("CDNS")

var notFoundError viper.ConfigFileNotFoundError

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
journal.Logger.Sugar().Errorf("Failed to read configuration file: %s", err)
os.Exit(1)
if err := viper.ReadInConfig(); err != nil && errors.As(err, &notFoundError) {
viper.SetDefault("DNS.LISTEN", "0.0.0.0:53")
viper.SetDefault("DNS.PROTOCOL", "both")
viper.SetDefault("HTTP.LISTEN", "0.0.0.0:443")
viper.SetDefault("LOGGING.LEVEL", "DEBUG")

err = viper.BindEnv("NS.IP", "CDNS_NS_IP")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("DNS.ADMIN", "CDNS_DNS_ADMIN")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("DNS.NSNAME", "CDNS_DNS_NSNAME")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("DNS.LISTEN", "CDNS_DNS_LISTEN")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("DNS.PROTOCOL", "CDNS_DNS_PROTOCOL")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("SOA.DOMAIN", "CDNS_SOA_DOMAIN")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("HTTP.TLS.MODE", "CDNS_HTTP_TLS_MODE")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("HTTP.DOMAIN", "CDNS_HTTP_DOMAIN")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("HTTP.LISTEN", "CDNS_HTTP_LISTEN")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("INGRESS.IP", "CDNS_INGRESS_IP")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("LOGGING.LEVEL", "CDNS_LOGGING_LEVEL")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("PROVIDERS.ACME.EMAIL", "CDNS_PROVIDERS_ACME_EMAIL")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("PROVIDERS.ACME.SERVER", "CDNS_PROVIDERS_ACME_SERVER")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("PROVIDERS.ACME.STORAGE", "CDNS_PROVIDERS_ACME_STORAGE")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("PROVIDERS.FILE.TLSKEY", "CDNS_PROVIDERS_FILE_TLSKEY")
if err != nil {
journal.Logger.Sugar().Error(err)
}

err = viper.BindEnv("PROVIDERS.FILE.TLSCERT", "CDNS_PROVIDERS_FILE_TLSCERT")
if err != nil {
journal.Logger.Sugar().Error(err)
}
}

// read in environment variables that match
Expand Down
6 changes: 5 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ services:
- ca.svc.dev:10.0.8.253
environment:
# General configuration
- CDNS_ENV=production
- CDNS_NS_IP=
- CDNS_SOA_DOMAIN=dev
- CDNS_INGRESS_IP=
- CDNS_LOGGING_LEVEL=INFO

# DNS configuration
- CDNS_DNS_ADMIN=
- CDNS_DNS_NSNAME=
- CDNS_DNS_LISTEN=0.0.0.0:53
- CDNS_DNS_PROTOCOL=both

Expand Down

0 comments on commit b2d95fa

Please sign in to comment.