basic auth
HTTP basic auth中间件,提供简单的认证方式,建议只用于内部管理系统。
Example
package main
import (
"bytes"
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
"github.com/vicanso/hes"
)
func main() {
e := elton.New()
e.Use(middleware.NewBasicAuth(middleware.BasicAuthConfig{
Validate: func(account, pwd string, c *elton.Context) (bool, error) {
if account == "tree.xie" && pwd == "password" {
return true, nil
}
if account == "n" {
return false, hes.New("account is invalid")
}
return false, nil
},
}))
e.GET("/", func(c *elton.Context) (err error) {
c.BodyBuffer = bytes.NewBufferString("hello world")
return
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}