diff --git a/.github/workflows/api-server.yaml b/.github/workflows/api-server.yaml new file mode 100644 index 0000000..9fc750d --- /dev/null +++ b/.github/workflows/api-server.yaml @@ -0,0 +1,58 @@ +name: 🚀 Build/Release docker api-server +on: + workflow_dispatch: + pull_request: + branches: + - master + paths: + - '.github/workflows/api-server.yaml' + - 'api-server/**' + push: + branches: + - master + paths: + - '.github/workflows/api-server.yaml' + - 'api-server/**' + tags: + - 'api-server_v[0-9]+.[0-9]+.[0-9]+' +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true +env: + REGISTRY: ghcr.io +jobs: + docker-build: + permissions: + packages: write + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: docker login + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: set up buildx + uses: docker/setup-buildx-action@v3 + - name: docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ env.REGISTRY }}/${{ github.repository_owner }}/api-server + tags: | + type=raw,value=latest,enable={{is_default_branch}} + type=match,pattern=api-server_(v\d+.\d+.\d+),group=1 + type=raw,value={{branch}}-{{sha}}-{{date 'X'}},enable={{is_default_branch}} + type=ref,event=pr + - name: build docker images + uses: docker/build-push-action@v5 + with: + context: "{{defaultContext}}:api-server" + file: Dockerfile + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/api-server/Dockerfile b/api-server/Dockerfile index 244ad26..cc5f055 100644 --- a/api-server/Dockerfile +++ b/api-server/Dockerfile @@ -1,12 +1,19 @@ -# syntax=docker/dockerfile:1.2 -# Alpine +# syntax=docker/dockerfile:1.6 + +# golang builder +FROM golang:1.21 as builder +WORKDIR /usr/local/src/ +COPY . /usr/local/src/ +RUN make build + +# docker image FROM alpine -RUN apk --no-cache --no-progress add ca-certificates tzdata git \ +RUN apk --no-cache --no-progress add ca-certificates tzdata \ && rm -rf /var/cache/apk/* ARG TARGETPLATFORM -COPY ./dist/$TARGETPLATFORM/api-server / +COPY --from=builder /usr/local/src/dist/$TARGETPLATFORM/api-server / ENTRYPOINT ["/api-server"] EXPOSE 3000 diff --git a/api-server/go.mod b/api-server/go.mod index 73724cc..77321ca 100644 --- a/api-server/go.mod +++ b/api-server/go.mod @@ -1,3 +1,10 @@ module api-server go 1.21 + +require github.com/tidwall/gjson v1.17.0 + +require ( + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect +) diff --git a/api-server/go.sum b/api-server/go.sum new file mode 100644 index 0000000..2b213a0 --- /dev/null +++ b/api-server/go.sum @@ -0,0 +1,6 @@ +github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= +github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= diff --git a/api-server/main.go b/api-server/main.go index 25175ca..088a71c 100644 --- a/api-server/main.go +++ b/api-server/main.go @@ -2,12 +2,16 @@ package main import ( "flag" + "fmt" "log" "math/rand" "net/http" "os" + "regexp" "strconv" "time" + + "github.com/tidwall/gjson" ) func main() { @@ -56,6 +60,19 @@ func main() { } switch req.Method { case http.MethodGet, http.MethodPut: + pattern := regexp.MustCompile(`/(\d+)`) + + // Extract the integer value from the URL path + matches := pattern.FindStringSubmatch(req.URL.Path) + + // If the URL path contains an integer + if len(matches) > 1 { + results := gjson.GetBytes(data, fmt.Sprintf("*.%s", matches[1])) + rw.WriteHeader(http.StatusOK) + _, _ = rw.Write([]byte(results.Raw)) + return + } + rw.WriteHeader(http.StatusOK) _, _ = rw.Write(data) case http.MethodPost: diff --git a/apps/base/apps/customers/api-versioned.yaml b/apps/base/apps/customers/api-versioned.yaml index 3c013fb..7595471 100644 --- a/apps/base/apps/customers/api-versioned.yaml +++ b/apps/base/apps/customers/api-versioned.yaml @@ -27,8 +27,6 @@ spec: number: 3000 openApiSpec: path: /openapi.yaml - port: - number: 3000 --- apiVersion: hub.traefik.io/v1alpha1 @@ -49,8 +47,6 @@ spec: number: 3000 openApiSpec: path: /openapi.yaml - port: - number: 3000 --- apiVersion: hub.traefik.io/v1alpha1 @@ -71,8 +67,6 @@ spec: number: 3000 openApiSpec: path: /openapi.yaml - port: - number: 3000 --- apiVersion: hub.traefik.io/v1alpha1 @@ -98,8 +92,6 @@ spec: number: 3000 openApiSpec: path: /openapi.yaml - port: - number: 3000 --- apiVersion: hub.traefik.io/v1alpha1 @@ -120,8 +112,6 @@ spec: number: 3000 openApiSpec: path: /openapi.yaml - port: - number: 3000 cors: allowCredentials: true allowHeaders: diff --git a/apps/base/apps/customers/api.yaml b/apps/base/apps/customers/api.yaml index c76b1cb..2ec1a1d 100644 --- a/apps/base/apps/customers/api.yaml +++ b/apps/base/apps/customers/api.yaml @@ -12,8 +12,6 @@ spec: service: openApiSpec: path: /openapi.yaml - port: - number: 3000 name: customer-app port: - number: 3000 \ No newline at end of file + number: 3000 diff --git a/apps/base/apps/customers/customer-v2.yaml b/apps/base/apps/customers/customer-v2.yaml index 690a1d2..06207d4 100644 --- a/apps/base/apps/customers/customer-v2.yaml +++ b/apps/base/apps/customers/customer-v2.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml"] imagePullPolicy: Always volumeMounts: diff --git a/apps/base/apps/customers/customer-v3.yaml b/apps/base/apps/customers/customer-v3.yaml index 1a33ebd..a2c1efe 100644 --- a/apps/base/apps/customers/customer-v3.yaml +++ b/apps/base/apps/customers/customer-v3.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml"] imagePullPolicy: Always volumeMounts: diff --git a/apps/base/apps/customers/customer-v4.yaml b/apps/base/apps/customers/customer-v4.yaml index 2d1c7b7..ca4f597 100644 --- a/apps/base/apps/customers/customer-v4.yaml +++ b/apps/base/apps/customers/customer-v4.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml"] imagePullPolicy: Always volumeMounts: diff --git a/apps/base/apps/customers/customer.yaml b/apps/base/apps/customers/customer.yaml index 02b99fe..d4d0c8b 100644 --- a/apps/base/apps/customers/customer.yaml +++ b/apps/base/apps/customers/customer.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml", "-errorrate", "2"] imagePullPolicy: Always volumeMounts: diff --git a/apps/base/apps/employee/api.yaml b/apps/base/apps/employee/api.yaml index b390ad3..03e63ec 100644 --- a/apps/base/apps/employee/api.yaml +++ b/apps/base/apps/employee/api.yaml @@ -12,8 +12,6 @@ spec: service: openApiSpec: path: /openapi.yaml - port: - number: 3000 name: employee-app port: number: 3000 diff --git a/apps/base/apps/employee/employee.yaml b/apps/base/apps/employee/employee.yaml index c939a94..565941d 100644 --- a/apps/base/apps/employee/employee.yaml +++ b/apps/base/apps/employee/employee.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml", "-errorrate", "2"] imagePullPolicy: Always volumeMounts: @@ -83,7 +83,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml", "-errorrate", "40"] imagePullPolicy: Always volumeMounts: @@ -133,7 +133,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml", "-latency", "400ms"] imagePullPolicy: Always volumeMounts: diff --git a/apps/base/apps/flight/api.yaml b/apps/base/apps/flight/api.yaml index 7015465..91833d3 100644 --- a/apps/base/apps/flight/api.yaml +++ b/apps/base/apps/flight/api.yaml @@ -12,8 +12,6 @@ spec: service: openApiSpec: path: /openapi.yaml - port: - number: 3000 name: flight-app port: number: 3000 diff --git a/apps/base/apps/flight/flight.yaml b/apps/base/apps/flight/flight.yaml index 0de4dc5..9b0291f 100644 --- a/apps/base/apps/flight/flight.yaml +++ b/apps/base/apps/flight/flight.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml", "-errorrate", "2"] imagePullPolicy: Always volumeMounts: diff --git a/apps/base/apps/flight/spec.yaml b/apps/base/apps/flight/spec.yaml index f32490f..14ed7ef 100644 --- a/apps/base/apps/flight/spec.yaml +++ b/apps/base/apps/flight/spec.yaml @@ -23,7 +23,7 @@ data: /flights: get: summary: Retrieve flights - operationId: gettFlights + operationId: getFlights tags: - flights parameters: diff --git a/apps/base/apps/ticket/api.yaml b/apps/base/apps/ticket/api.yaml index 3bfdd09..a849227 100644 --- a/apps/base/apps/ticket/api.yaml +++ b/apps/base/apps/ticket/api.yaml @@ -12,8 +12,6 @@ spec: service: openApiSpec: path: /openapi.yaml - port: - number: 3000 name: ticket-app port: - number: 3000 \ No newline at end of file + number: 3000 diff --git a/apps/base/apps/ticket/ticket.yaml b/apps/base/apps/ticket/ticket.yaml index 05fdd47..f66fa5a 100644 --- a/apps/base/apps/ticket/ticket.yaml +++ b/apps/base/apps/ticket/ticket.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: api - image: mmatur/traefik-hub:kubecon + image: ghcr.io/traefik-workshops/api-server:v0.1.0 args: ["-data", "/api/api.json", "-openapi", "/public/openapi.yaml", "-errorrate", "1"] imagePullPolicy: Always volumeMounts: