分类 后端 下的文章

在go中写配置文件 这样写

Cfg, err = ini.Load("conf/app.ini")

或者配置模板目录这样写

 router.LoadHTMLGlob("templates/*")

然后单元测试的时候,偏偏又找不到文件目录。

  1. 设置系统全局路径

    router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"),"/src/gin_curd/templates/*"))
    

2.利用模式 重写路径

if mode := gin.Mode(); mode == gin.TestMode {

router.LoadHTMLGlob("./../templates/*")

} else {

router.LoadHTMLGlob("templates/*")

}

参考:

golang单元测试时关于相对路径的处理问题?

聊一聊,Golang “相对”路径问题

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

当golang的对象需要和json做转换的时候,我们就经常用到这个特性。
有两点注意的地方:

1.如果一个域不是以大写字母开头的,那么转换成json的时候,这个域是被忽略的。

2.如果没有使用json:"name"tag,那么输出的json字段名和域名是一样的。

总结一下,json:"name"格式串是用来指导json.Marshal/Unmarshal,在进行json串和golang对象之间转换的时候映射字段名使用的。

空接口(empty interface)

空接口比较特殊,它不包含任何方法:

interface{}

在Go语言中,所有其它数据类型都实现了空接口。

var v1 interface{} = 1
var v2 interface{} = "abc"
var v3 interface{} = struct{ X int }{1}

如果函数打算接收任何数据类型,则可以将参考声明为interface{}。最典型的例子就是标准库fmt包中的Print和Fprint系列的函数:

func Fprint(w io.Writer, a ...interface{}) (n int, err error) 
func Fprintf(w io.Writer, format string, a ...interface{})
func Fprintln(w io.Writer, a ...interface{})
func Print(a ...interface{}) (n int, err error)
func Printf(format string, a ...interface{})
func Println(a ...interface{}) (n int, err error)

注意,[]T不能直接赋值给[]interface{}

t := []int{1, 2, 3, 4}
var s []interface{} = t

编译时会输出下面的错误:

cannot use t (type []int) as type []interface {} in assignment

我们必须通过下面这种方式:

t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
    s[i] = v
}

使用iptables封锁

#!/bin/bash

#iptables封锁
max=500    #最大访问量
logdir=/usr/local/nginx12/logs/access.log  #nginx访问日志文件路径
confdir=/data/test.conf  # ip黑名单
port=80
drop_ip=""

today=$(date +"%d/%b/%Y") # 今天的日志

#循环遍历日志文件取出访问量大于500的ip
for drop_ip  in $(cat $logdir |grep $today| awk '{print $1}' | sort|uniq -c |sort -rn |awk '{if($1>500)print $2}')
do
  grep  -q  "$drop_ip" $confdir && eg=1 || eg=0;
  if (( $eg==0 ));then
     echo $drop_ip  >> $confdir
     iptables -I INPUT -p tcp --dport $port -s $drop_ip -j DROP
     echo ">>>>> `date '+%Y-%m-%d %H%M%S'` - 发现攻击ip ->  $drop_ip " >> /data/test.log  #记录log
  fi
done

解锁iptables

#!/bin/bash
iptables -F INPUT #清空 filter表INPUT所有规则
#iptables -F    #清空所有规则

完整代码

black_ip.sh

#!/bin/bash

#iptables封锁
max=500    #最大访问量
logdir=/usr/local/nginx12/logs/access.log  #nginx访问日志文件路径
confdir=/data/test.conf  # ip黑名单
port=80
drop_ip=""

today=$(date +"%d/%b/%Y") # 今天的日志

#循环遍历日志文件取出访问量大于500的ip
for drop_ip  in $(cat $logdir |grep $today| awk '{print $1}' | sort|uniq -c |sort -rn |awk '{if($1>500)print $2}')
do
  grep  -q  "$drop_ip" $confdir && eg=1 || eg=0;
  if (( $eg==0 ));then
     echo $drop_ip  >> $confdir
     iptables -I INPUT -p tcp --dport $port -s $drop_ip -j DROP
     echo ">>>>> `date '+%Y-%m-%d %H%M%S'` - 发现攻击ip ->  $drop_ip " >> /data/test.log  #记录log
  fi
done

# 解锁iptables
release_time=$(date "+%H")
if [[ $release_time -eq "23" ]]
then
    iptables -F INPUT #清空 filter表INPUT所有规则
    #iptables -F    #清空所有规则
fi
*/1 * * * * /bin/sh black_ip.sh