From 6c69e9871f5373e80e69ca53b00c0ff749b72a95 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 2 May 2019 11:51:48 +0800 Subject: [PATCH] add aws apis --- .gitignore | 2 ++ config.default.yml | 5 +++++ go.mod | 9 +++++++- go.sum | 26 +++++++++++++++++++++++ main.go | 8 +++++++ module/apis/aws/aws.go | 46 +++++++++++++++++++++++++++++++++++++++++ module/config/config.go | 8 ++++++- route/routes/routes.go | 24 +++++++++++++++++++++ 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 config.default.yml create mode 100644 module/apis/aws/aws.go create mode 100644 route/routes/routes.go diff --git a/.gitignore b/.gitignore index 1377554..38c7f8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *.swp +config.yml +.vscode/ diff --git a/config.default.yml b/config.default.yml new file mode 100644 index 0000000..08240bb --- /dev/null +++ b/config.default.yml @@ -0,0 +1,5 @@ +port: 10230 +aws: + shared_config: true + access_key: '' + secret_key: '' diff --git a/go.mod b/go.mod index 16ac1f4..a11cefe 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,18 @@ go 1.12 require ( git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 + github.com/aws/aws-sdk-go v1.19.22 github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 // indirect - github.com/gin-gonic/gin v1.3.0 // indirect + github.com/gin-gonic/gin v1.3.0 github.com/golang/protobuf v1.3.1 // indirect + github.com/json-iterator/go v1.1.6 // indirect github.com/mattn/go-isatty v0.0.7 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/stretchr/testify v1.3.0 // indirect github.com/ugorji/go v1.1.4 // indirect + golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 // indirect + gopkg.in/go-playground/assert.v1 v1.2.1 // 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 index 32735c0..4152292 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,44 @@ 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/aws/aws-sdk-go v1.19.22 h1:YwhJ3kPPVARpyqV826VzR2C5hvTYLJeRhAR09KnPxjY= +github.com/aws/aws-sdk-go v1.19.22/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= 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/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 h1:KaQtG+aDELoNmXYas3TVkGNYRuq8JQ1aa7LJt8EXVyo= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 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= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= 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= diff --git a/main.go b/main.go index fd178db..48b80b8 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "flag" + "fmt" "log" "git.trj.tw/golang/go-ddns-svc/module/config" "git.trj.tw/golang/go-ddns-svc/module/option" + "git.trj.tw/golang/go-ddns-svc/route/routes" ) func init() { @@ -25,4 +27,10 @@ func main() { if err != nil { log.Fatal(err) } + conf := config.GetConfig() + + engine := routes.NewEngine() + routes.SetRoutes() + + engine.Run(fmt.Sprintf(":%d", conf.Port)) } diff --git a/module/apis/aws/aws.go b/module/apis/aws/aws.go new file mode 100644 index 0000000..2b187ec --- /dev/null +++ b/module/apis/aws/aws.go @@ -0,0 +1,46 @@ +package aws + +import ( + "errors" + + "git.trj.tw/golang/go-ddns-svc/module/config" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/route53" +) + +// AWSClient - +type AWSClient struct { + Session *session.Session + Config *aws.Config + Services struct { + R53 *route53.Route53 + } +} + +var client *AWSClient + +// NewAWS - +func NewAWS() error { + conf := config.GetConfig() + awsConf := &aws.Config{} + awsConf.Region = aws.String("us-east-1") + var sess *session.Session + + if conf.AWS.SharedConfig { + if len(conf.AWS.SharedName) == 0 { + return errors.New("aws shared config name empty") + } + awsConf.Credentials = credentials.NewSharedCredentials("", conf.AWS.SharedName) + } else { + awsConf.Credentials = credentials.NewStaticCredentials(conf.AWS.AccessKey, conf.AWS.SecretKey, "") + } + sess = session.New(awsConf) + + client = &AWSClient{} + client.Session = sess + client.Config = awsConf + + return nil +} diff --git a/module/config/config.go b/module/config/config.go index d26026d..4284e82 100644 --- a/module/config/config.go +++ b/module/config/config.go @@ -16,6 +16,7 @@ type Config struct { Port int `yaml:"port"` AWS struct { SharedConfig bool `yaml:"shared_config"` + SharedName string `yaml:"shared_name"` AccessKey string `yaml:"access_key"` SecretKey string `yaml:"secret_key"` } `yaml:"aws"` @@ -50,7 +51,7 @@ func LoadConfig(p ...string) (err error) { if err != nil { return err } - envOverride() + envOverride() return } @@ -76,6 +77,11 @@ func envOverride() { } } + str = os.Getenv("AWS_SHARED_NAME") + if len(str) > 0 { + conf.AWS.SharedName = str + } + // set aws access key str = os.Getenv("AWS_ACCESS_KEY") if len(str) > 0 { diff --git a/route/routes/routes.go b/route/routes/routes.go new file mode 100644 index 0000000..df4e18f --- /dev/null +++ b/route/routes/routes.go @@ -0,0 +1,24 @@ +package routes + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +var e *gin.Engine + +// NewEngine - +func NewEngine() *gin.Engine { + e = gin.New() + e.Use(gin.Logger()) + e.Use(gin.Recovery()) + return e +} + +// SetRoutes - +func SetRoutes() { + e.GET("/", func(c *gin.Context) { + c.String(http.StatusOK, "OK") + }) +}