forked from goodrain/rainbond
/
license.go
37 lines (32 loc) · 814 Bytes
/
license.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package middleware
import (
"net/http"
"github.com/goodrain/rainbond/cmd/api/option"
httputil "github.com/goodrain/rainbond/util/http"
licutil "github.com/goodrain/rainbond/util/license"
)
// 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) {
if !licutil.VerifyTime(l.cfg.LicensePath, l.cfg.LicSoPath) {
httputil.Return(r, w, 401, httputil.ResponseBody{
Bean: map[string]interface{}{
"msg": "invalid license",
"code": 10400,
},
})
return
}
next.ServeHTTP(w, r)
})
}