commit 463dd449893f5dfaa161813d9aac18fbba9e356e Author: root Date: Tue Apr 30 14:04:51 2019 +0000 first diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..16ac1f4 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module git.trj.tw/golang/go-ddns-svc + +go 1.12 + +require ( + git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 + github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 // indirect + github.com/gin-gonic/gin v1.3.0 // indirect + github.com/golang/protobuf v1.3.1 // indirect + github.com/mattn/go-isatty v0.0.7 // indirect + github.com/ugorji/go v1.1.4 // indirect + gopkg.in/go-playground/validator.v8 v8.18.2 // indirect + gopkg.in/yaml.v2 v2.2.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..32735c0 --- /dev/null +++ b/go.sum @@ -0,0 +1,19 @@ +git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTOz37n6z5uLwm6fjKjnztbTCaQI= +git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= +github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..fd178db --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "flag" + "log" + + "git.trj.tw/golang/go-ddns-svc/module/config" + "git.trj.tw/golang/go-ddns-svc/module/option" +) + +func init() { + option.RegOptions() +} + +func main() { + var err error + opts := option.GetOptions() + + if opts.Help { + flag.Usage() + return + } + + err = config.LoadConfig(opts.Config) + if err != nil { + log.Fatal(err) + } +} diff --git a/module/config/config.go b/module/config/config.go new file mode 100644 index 0000000..d26026d --- /dev/null +++ b/module/config/config.go @@ -0,0 +1,95 @@ +package config + +import ( + "errors" + "io/ioutil" + "os" + "path" + "strconv" + + "git.trj.tw/golang/utils" + yaml "gopkg.in/yaml.v2" +) + +// Config - +type Config struct { + Port int `yaml:"port"` + AWS struct { + SharedConfig bool `yaml:"shared_config"` + AccessKey string `yaml:"access_key"` + SecretKey string `yaml:"secret_key"` + } `yaml:"aws"` +} + +var conf *Config + +// LoadConfig - +func LoadConfig(p ...string) (err error) { + fp := "" + if len(p) > 0 && len(p[0]) > 0 { + fp = p[0] + } else { + wd, err := os.Getwd() + if err != nil { + return err + } + fp = path.Join(wd, "config.yml") + } + + fp = utils.ParsePath(fp) + if exists := utils.CheckExists(fp, false); !exists { + return errors.New("config file not exists") + } + data, err := ioutil.ReadFile(fp) + if err != nil { + return err + } + + conf = &Config{} + err = yaml.Unmarshal(data, conf) + if err != nil { + return err + } + envOverride() + + return +} + +func envOverride() { + var str string + // set port + str = os.Getenv("PORT") + if len(str) > 0 { + num, err := strconv.Atoi(str) + if err == nil && num > 0 && num < 65536 { + conf.Port = num + } + } + + // set aws use shared config + str = os.Getenv("AWS_SHARED_CONF") + if len(str) > 0 { + if str == "1" { + conf.AWS.SharedConfig = true + } else if str == "0" { + conf.AWS.SharedConfig = false + } + } + + // set aws access key + str = os.Getenv("AWS_ACCESS_KEY") + if len(str) > 0 { + conf.AWS.AccessKey = str + } + + // set aws secret key + str = os.Getenv("AWS_SECRET_KEY") + if len(str) > 0 { + conf.AWS.SecretKey = str + } +} + +// GetConfig - +func GetConfig() *Config { + return conf +} diff --git a/module/option/option.go b/module/option/option.go new file mode 100644 index 0000000..81cfeb4 --- /dev/null +++ b/module/option/option.go @@ -0,0 +1,23 @@ +package option + +import "flag" + +// Options - +type Options struct { + Help bool + Config string +} + +var opts *Options + +// RegOptions - +func RegOptions() { + opts = &Options{} + flag.StringVar(&opts.Config, "config", "", "config file path - default: `pwd/config.yml`") + flag.StringVar(&opts.Config, "f", "", "config file path - default: `pwd/config.yml`") + flag.BoolVar(&opts.Help, "help", false, "show help") + flag.Parse() +} + +// GetOptions - +func GetOptions() *Options { return opts }