first version
This commit is contained in:
+48
-2
@@ -102,13 +102,19 @@ func QueryDomain(name []string) ([]*ZoneData, error) {
|
||||
}
|
||||
|
||||
// GetRecord -
|
||||
func GetRecord(id *string, names []string) (*[]route53.ResourceRecordSet, error) {
|
||||
func GetRecord(id *string, names []string) ([]*route53.ResourceRecordSet, error) {
|
||||
if id == nil {
|
||||
return nil, errors.New("no id input")
|
||||
}
|
||||
if len(names) == 0 {
|
||||
return nil, errors.New("no record names")
|
||||
}
|
||||
records := make([]string, 0)
|
||||
for _, v := range names {
|
||||
if len(v) > 0 {
|
||||
records = append(records, v)
|
||||
}
|
||||
}
|
||||
|
||||
input := &route53.ListResourceRecordSetsInput{}
|
||||
input.SetHostedZoneId(*id)
|
||||
@@ -116,6 +122,46 @@ func GetRecord(id *string, names []string) (*[]route53.ResourceRecordSet, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(resOut.ResourceRecordSets) == 0 {
|
||||
return nil, errors.New("no record sets")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
sets := make([]*route53.ResourceRecordSet, 0)
|
||||
for _, it := range resOut.ResourceRecordSets {
|
||||
for _, name := range records {
|
||||
if *it.Name == name {
|
||||
sets = append(sets, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sets, nil
|
||||
}
|
||||
|
||||
// UpdateRecord -
|
||||
func UpdateRecord(zoneID *string, sets []*route53.ResourceRecordSet) error {
|
||||
if len(sets) == 0 {
|
||||
return errors.New("no change set input")
|
||||
}
|
||||
changeSets := make([]*route53.Change, 0)
|
||||
|
||||
for _, v := range sets {
|
||||
changeSets = append(changeSets, &route53.Change{
|
||||
ResourceRecordSet: v,
|
||||
Action: aws.String("UPSERT"),
|
||||
})
|
||||
}
|
||||
|
||||
changeInput := &route53.ChangeResourceRecordSetsInput{
|
||||
HostedZoneId: zoneID,
|
||||
ChangeBatch: &route53.ChangeBatch{
|
||||
Changes: changeSets,
|
||||
},
|
||||
}
|
||||
_, err := client.R53.ChangeResourceRecordSets(changeInput)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -13,8 +13,9 @@ import (
|
||||
|
||||
// Config -
|
||||
type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
AWS struct {
|
||||
Port int `yaml:"port"`
|
||||
VerifyValue string `yaml:"verify_value"`
|
||||
AWS struct {
|
||||
SharedConfig bool `yaml:"shared_config"`
|
||||
SharedName string `yaml:"shared_name"`
|
||||
AccessKey string `yaml:"access_key"`
|
||||
@@ -67,6 +68,11 @@ func envOverride() {
|
||||
}
|
||||
}
|
||||
|
||||
str = os.Getenv("VERIFY_VALUE")
|
||||
if len(str) > 0 {
|
||||
conf.VerifyValue = str
|
||||
}
|
||||
|
||||
// set aws use shared config
|
||||
str = os.Getenv("AWS_SHARED_CONF")
|
||||
if len(str) > 0 {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// Context -
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
}
|
||||
|
||||
// CustomMiddle func
|
||||
type CustomMiddle func(*Context)
|
||||
|
||||
// PatchCtx - patch context to custom middleware
|
||||
func PatchCtx(handler CustomMiddle) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := &Context{Context: c}
|
||||
handler(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// BindData client body data
|
||||
func (c *Context) BindData(i interface{}) error {
|
||||
bind := binding.Default(c.Request.Method, c.ContentType())
|
||||
return c.ShouldBindWith(i, bind)
|
||||
}
|
||||
|
||||
// CustomRes -
|
||||
func (c *Context) CustomRes(status int, msg interface{}) {
|
||||
c.AbortWithStatusJSON(status, msg)
|
||||
}
|
||||
|
||||
// LoginFirst -
|
||||
func (c *Context) LoginFirst(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "login first"}
|
||||
}
|
||||
c.AbortWithStatusJSON(401, msg)
|
||||
}
|
||||
|
||||
// DataFormat -
|
||||
func (c *Context) DataFormat(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "data format error"}
|
||||
}
|
||||
c.AbortWithStatusJSON(400, msg)
|
||||
}
|
||||
|
||||
// NotFound -
|
||||
func (c *Context) NotFound(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "not found"}
|
||||
}
|
||||
c.AbortWithStatusJSON(404, msg)
|
||||
}
|
||||
|
||||
// Forbidden -
|
||||
func (c *Context) Forbidden(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "forbidden"}
|
||||
}
|
||||
c.AbortWithStatusJSON(403, msg)
|
||||
}
|
||||
|
||||
// Success -
|
||||
func (c *Context) Success(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "success"}
|
||||
}
|
||||
|
||||
if str, ok := msg.(string); ok {
|
||||
c.String(200, str)
|
||||
c.Abort()
|
||||
} else {
|
||||
c.AbortWithStatusJSON(200, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// ServerError -
|
||||
func (c *Context) ServerError(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "internal error"}
|
||||
}
|
||||
c.AbortWithStatusJSON(500, msg)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RemoteIP -
|
||||
func RemoteIP(req *http.Request) string {
|
||||
ips := make([]string, 0)
|
||||
if headerAddr := req.Header.Get(textproto.CanonicalMIMEHeaderKey("X-Forwarded-For")); len(headerAddr) > 0 {
|
||||
ips = strings.Split(headerAddr, ",")
|
||||
}
|
||||
if len(ips) > 0 && len(ips[0]) > 0 {
|
||||
rip, _, err := net.SplitHostPort(ips[0])
|
||||
if err != nil {
|
||||
return ips[0]
|
||||
}
|
||||
return rip
|
||||
}
|
||||
|
||||
ip := req.RemoteAddr
|
||||
if rip, _, err := net.SplitHostPort(ip); err == nil {
|
||||
return rip
|
||||
} else {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user