2019-09-10 17:27:00 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/goodrain/rainbond/cmd/api/option"
|
|
|
|
httputil "github.com/goodrain/rainbond/util/http"
|
2019-09-16 17:35:18 +08:00
|
|
|
licutil "github.com/goodrain/rainbond/util/license"
|
2019-09-10 17:27:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// License -
|
|
|
|
type License struct {
|
|
|
|
cfg *option.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLicense -
|
|
|
|
func NewLicense(cfg *option.Config) *License {
|
|
|
|
return &License{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify parses the license to make the content inside it take effect.
|
|
|
|
func (l *License) Verify(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-09-16 17:35:18 +08:00
|
|
|
if !licutil.VerifyTime(l.cfg.LicensePath, l.cfg.LicSoPath) {
|
2019-09-10 17:27:00 +08:00
|
|
|
httputil.Return(r, w, 401, httputil.ResponseBody{
|
|
|
|
Bean: map[string]interface{}{
|
2019-09-16 17:35:18 +08:00
|
|
|
"msg": "invalid license",
|
|
|
|
"code": 10400,
|
2019-09-10 17:27:00 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|