未验证 提交 6ac00afa 编写于 作者: H hongming

fix: null pointer exception

Signed-off-by: Nhongming <talonwan@yunify.com>
上级 02269bb9
...@@ -36,7 +36,7 @@ import ( ...@@ -36,7 +36,7 @@ import (
) )
type Auth struct { type Auth struct {
Rule Rule Rule *Rule
Next httpserver.Handler Next httpserver.Handler
} }
......
...@@ -38,6 +38,7 @@ func Setup(c *caddy.Controller) error { ...@@ -38,6 +38,7 @@ func Setup(c *caddy.Controller) error {
c.OnStartup(func() error { c.OnStartup(func() error {
rule.RedisClient, err = redis.NewRedisClient(rule.RedisOptions, nil) rule.RedisClient, err = redis.NewRedisClient(rule.RedisOptions, nil)
// ensure redis is connected when startup
if err != nil { if err != nil {
return err return err
} }
...@@ -56,9 +57,9 @@ func Setup(c *caddy.Controller) error { ...@@ -56,9 +57,9 @@ func Setup(c *caddy.Controller) error {
return nil return nil
} }
func parse(c *caddy.Controller) (Rule, error) { func parse(c *caddy.Controller) (*Rule, error) {
rule := Rule{ExceptedPath: make([]string, 0)} rule := &Rule{ExceptedPath: make([]string, 0)}
if c.Next() { if c.Next() {
args := c.RemainingArgs() args := c.RemainingArgs()
...@@ -68,57 +69,57 @@ func parse(c *caddy.Controller) (Rule, error) { ...@@ -68,57 +69,57 @@ func parse(c *caddy.Controller) (Rule, error) {
switch c.Val() { switch c.Val() {
case "path": case "path":
if !c.NextArg() { if !c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
rule.Path = c.Val() rule.Path = c.Val()
if c.NextArg() { if c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
case "token-idle-timeout": case "token-idle-timeout":
if !c.NextArg() { if !c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
if timeout, err := time.ParseDuration(c.Val()); err != nil { if timeout, err := time.ParseDuration(c.Val()); err != nil {
return rule, c.ArgErr() return nil, c.ArgErr()
} else { } else {
rule.TokenIdleTimeout = timeout rule.TokenIdleTimeout = timeout
} }
if c.NextArg() { if c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
case "redis-url": case "redis-url":
if !c.NextArg() { if !c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
options := &redis.RedisOptions{RedisURL: c.Val()} options := &redis.RedisOptions{RedisURL: c.Val()}
if err := options.Validate(); len(err) > 0 { if err := options.Validate(); len(err) > 0 {
return rule, c.ArgErr() return nil, c.ArgErr()
} else { } else {
rule.RedisOptions = options rule.RedisOptions = options
} }
if c.NextArg() { if c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
case "secret": case "secret":
if !c.NextArg() { if !c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
rule.Secret = []byte(c.Val()) rule.Secret = []byte(c.Val())
if c.NextArg() { if c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
case "except": case "except":
if !c.NextArg() { if !c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
rule.ExceptedPath = strings.Split(c.Val(), ",") rule.ExceptedPath = strings.Split(c.Val(), ",")
...@@ -128,17 +129,21 @@ func parse(c *caddy.Controller) (Rule, error) { ...@@ -128,17 +129,21 @@ func parse(c *caddy.Controller) (Rule, error) {
} }
if c.NextArg() { if c.NextArg() {
return rule, c.ArgErr() return nil, c.ArgErr()
} }
} }
} }
default: default:
return rule, c.ArgErr() return nil, c.ArgErr()
} }
} }
if c.Next() { if c.Next() {
return rule, c.ArgErr() return nil, c.ArgErr()
}
if rule.RedisOptions == nil {
return nil, c.Err("redis-url must be specified")
} }
return rule, nil return rule, nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册