go-proxy ls-route now query api server first, then fallback to read from config file

This commit is contained in:
yusing 2024-09-30 15:56:03 +08:00
parent b38d7595a7
commit 9065d990e5
4 changed files with 28 additions and 9 deletions

View file

@ -124,9 +124,3 @@ jobs:
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
- name: Tag as latest
if: startsWith(github.ref, 'refs/tags/') && !contains(github.ref_name, '-')
run: |
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest

View file

@ -5,11 +5,11 @@ RUN apk add --no-cache tzdata
WORKDIR /src
# Only copy go.mod and go.sum initially for better caching
COPY go.mod go.sum /src
COPY go.mod go.sum /src/
# Utilize build cache
RUN --mount=type=cache,target="/go/pkg/mod" \
go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get
go mod download -x
ENV GOCACHE=/root/.cache/go-build

View file

@ -91,7 +91,14 @@ func main() {
printJSON(cfg.Value())
return
case common.CommandListRoutes:
printJSON(cfg.RoutesByAlias())
routes, err := apiUtils.ListRoutes()
if err.HasError() {
log.Printf("failed to connect to api server: %s", err)
log.Printf("falling back to config file")
printJSON(cfg.RoutesByAlias())
} else {
printJSON(routes)
}
return
case common.CommandDebugListEntries:
printJSON(cfg.DumpEntries())

View file

@ -1,6 +1,7 @@
package utils
import (
"encoding/json"
"fmt"
"io"
"net/http"
@ -29,3 +30,20 @@ func ReloadServer() E.NestedError {
}
return nil
}
func ListRoutes() (map[string]map[string]any, E.NestedError) {
resp, err := httpClient.Get(fmt.Sprintf("%s/v1/list/routes", common.APIHTTPURL))
if err != nil {
return nil, E.From(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, E.Failure("list routes").Extraf("status code: %v", resp.StatusCode)
}
var routes map[string]map[string]any
err = json.NewDecoder(resp.Body).Decode(&routes)
if err != nil {
return nil, E.From(err)
}
return routes, nil
}