Improve: provider api

This commit is contained in:
Dreamacro 2019-12-13 00:29:24 +08:00
parent 95e9ae2d8d
commit 0822b526d5
3 changed files with 32 additions and 4 deletions

View file

@ -142,6 +142,7 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
mapping := map[string]interface{}{} mapping := map[string]interface{}{}
json.Unmarshal(inner, &mapping) json.Unmarshal(inner, &mapping)
mapping["history"] = p.DelayHistory() mapping["history"] = p.DelayHistory()
mapping["name"] = p.Name()
return json.Marshal(mapping) return json.Marshal(mapping)
} }

View file

@ -60,6 +60,7 @@ type ProxyProvider interface {
Provider Provider
Proxies() []C.Proxy Proxies() []C.Proxy
HealthCheck() HealthCheck()
Update() error
} }
type ProxySchema struct { type ProxySchema struct {
@ -106,6 +107,10 @@ func (pp *ProxySetProvider) HealthCheck() {
} }
} }
func (pp *ProxySetProvider) Update() error {
return pp.pull()
}
func (pp *ProxySetProvider) Destroy() error { func (pp *ProxySetProvider) Destroy() error {
pp.mux.Lock() pp.mux.Lock()
defer pp.mux.Unlock() defer pp.mux.Unlock()
@ -124,8 +129,10 @@ func (pp *ProxySetProvider) Destroy() error {
func (pp *ProxySetProvider) Initial() error { func (pp *ProxySetProvider) Initial() error {
var buf []byte var buf []byte
var err error var err error
if _, err := os.Stat(pp.vehicle.Path()); err == nil { if stat, err := os.Stat(pp.vehicle.Path()); err == nil {
buf, err = ioutil.ReadFile(pp.vehicle.Path()) buf, err = ioutil.ReadFile(pp.vehicle.Path())
modTime := stat.ModTime()
pp.updatedAt = &modTime
} else { } else {
buf, err = pp.vehicle.Read() buf, err = pp.vehicle.Read()
} }
@ -180,9 +187,11 @@ func (pp *ProxySetProvider) pull() error {
return err return err
} }
now := time.Now()
hash := md5.Sum(buf) hash := md5.Sum(buf)
if bytes.Equal(pp.hash[:], hash[:]) { if bytes.Equal(pp.hash[:], hash[:]) {
log.Debugln("[Provider] %s's proxies doesn't change", pp.Name()) log.Debugln("[Provider] %s's proxies doesn't change", pp.Name())
pp.updatedAt = &now
return nil return nil
} }
@ -196,7 +205,6 @@ func (pp *ProxySetProvider) pull() error {
return err return err
} }
now := time.Now()
pp.updatedAt = &now pp.updatedAt = &now
pp.hash = hash pp.hash = hash
pp.setProxies(proxies) pp.setProxies(proxies)
@ -224,6 +232,10 @@ func (pp *ProxySetProvider) parse(buf []byte) ([]C.Proxy, error) {
proxies = append(proxies, proxy) proxies = append(proxies, proxy)
} }
if len(proxies) == 0 {
return nil, errors.New("File doesn't have any valid proxy")
}
return proxies, nil return proxies, nil
} }
@ -291,6 +303,10 @@ func (cp *CompatibleProvier) HealthCheck() {
} }
} }
func (cp *CompatibleProvier) Update() error {
return nil
}
func (cp *CompatibleProvier) Initial() error { func (cp *CompatibleProvier) Initial() error {
if cp.healthCheck != nil { if cp.healthCheck != nil {
go cp.healthCheck.process() go cp.healthCheck.process()

View file

@ -18,7 +18,8 @@ func proxyProviderRouter() http.Handler {
r.Route("/{name}", func(r chi.Router) { r.Route("/{name}", func(r chi.Router) {
r.Use(parseProviderName, findProviderByName) r.Use(parseProviderName, findProviderByName)
r.Get("/", getProvider) r.Get("/", getProvider)
r.Get("/healthcheck", doProviderHealthCheck) r.Put("/", updateProvider)
r.Get("/healthcheck", healthCheckProvider)
}) })
return r return r
} }
@ -35,7 +36,17 @@ func getProvider(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, provider) render.JSON(w, r, provider)
} }
func doProviderHealthCheck(w http.ResponseWriter, r *http.Request) { func updateProvider(w http.ResponseWriter, r *http.Request) {
provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider)
if err := provider.Update(); err != nil {
render.Status(r, http.StatusServiceUnavailable)
render.JSON(w, r, newError(err.Error()))
return
}
render.NoContent(w, r)
}
func healthCheckProvider(w http.ResponseWriter, r *http.Request) {
provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider) provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider)
provider.HealthCheck() provider.HealthCheck()
render.NoContent(w, r) render.NoContent(w, r)