mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-18 23:44:40 +01:00
v0.5.0-rc5: check release
This commit is contained in:
@@ -3,6 +3,7 @@ package v1
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
U "github.com/yusing/go-proxy/api/v1/utils"
|
||||
"github.com/yusing/go-proxy/config"
|
||||
@@ -17,17 +18,19 @@ func CheckHealth(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var ok bool
|
||||
route := cfg.FindRoute(target)
|
||||
|
||||
switch route := cfg.FindRoute(target).(type) {
|
||||
case nil:
|
||||
switch {
|
||||
case route == nil:
|
||||
U.HandleErr(w, r, U.ErrNotFound("target", target), http.StatusNotFound)
|
||||
return
|
||||
case *R.HTTPRoute:
|
||||
ok = U.IsSiteHealthy(route.TargetURL.String())
|
||||
case *R.StreamRoute:
|
||||
case route.Type() == R.RouteTypeReverseProxy:
|
||||
ok = U.IsSiteHealthy(route.URL().String())
|
||||
case route.Type() == R.RouteTypeStream:
|
||||
entry := route.Entry()
|
||||
ok = U.IsStreamHealthy(
|
||||
string(route.Scheme.ProxyScheme),
|
||||
fmt.Sprintf("%s:%v", route.Host, route.Port.ProxyPort),
|
||||
strings.Split(entry.Scheme, ":")[1], // target scheme
|
||||
fmt.Sprintf("%s:%v", entry.Host, strings.Split(entry.Port, ":")[1]),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
U "github.com/yusing/go-proxy/api/v1/utils"
|
||||
"github.com/yusing/go-proxy/common"
|
||||
"github.com/yusing/go-proxy/config"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
"github.com/yusing/go-proxy/proxy/provider"
|
||||
)
|
||||
|
||||
@@ -32,25 +31,25 @@ func SetFileContent(w http.ResponseWriter, r *http.Request) {
|
||||
U.HandleErr(w, r, U.ErrMissingKey("filename"), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
content, err := E.Check(io.ReadAll(r.Body))
|
||||
if err.HasError() {
|
||||
content, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
U.HandleErr(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if filename == common.ConfigFileName {
|
||||
err = config.Validate(content)
|
||||
err = config.Validate(content).Error()
|
||||
} else {
|
||||
err = provider.Validate(content)
|
||||
err = provider.Validate(content).Error()
|
||||
}
|
||||
|
||||
if err.HasError() {
|
||||
if err != nil {
|
||||
U.HandleErr(w, r, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = E.From(os.WriteFile(path.Join(common.ConfigBasePath, filename), content, 0644))
|
||||
if err.HasError() {
|
||||
err = os.WriteFile(path.Join(common.ConfigBasePath, filename), content, 0644)
|
||||
if err != nil {
|
||||
U.HandleErr(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func Reload(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
|
||||
if err := cfg.Reload(); err.HasError() {
|
||||
if err := cfg.Reload().Error(); err != nil {
|
||||
U.HandleErr(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ import (
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
)
|
||||
|
||||
func HandleErr(w http.ResponseWriter, r *http.Request, err error, code ...int) {
|
||||
err = E.From(err).Subjectf("%s %s", r.Method, r.URL)
|
||||
func HandleErr(w http.ResponseWriter, r *http.Request, origErr error, code ...int) {
|
||||
err := E.From(origErr).Subjectf("%s %s", r.Method, r.URL)
|
||||
logrus.WithField("module", "api").Error(err)
|
||||
if len(code) > 0 {
|
||||
http.Error(w, err.Error(), code[0])
|
||||
http.Error(w, err.String(), code[0])
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, err.String(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func ErrMissingKey(k string) error {
|
||||
|
||||
@@ -44,7 +44,7 @@ func ReloadServer() E.NestedError {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return E.Failure("server reload").Subjectf("status code: %v", resp.StatusCode)
|
||||
}
|
||||
return E.Nil()
|
||||
return nil
|
||||
}
|
||||
|
||||
var HttpClient = &http.Client{
|
||||
|
||||
Reference in New Issue
Block a user