Skip to content

Commit

Permalink
Merge pull request #339 from porters-xyz/develop
Browse files Browse the repository at this point in the history
Gateway Kit Proxy endpoint, and other changes
  • Loading branch information
scermat authored Aug 8, 2024
2 parents 82cc382 + 13dd448 commit 3076a88
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jspm_packages/
.env.test.local
.env.production.local
.env.local
*.env

# Next.js build output
.next
Expand All @@ -56,3 +57,4 @@ docs/pnpm-lock.yaml

# IGNORE BUN LOCKB FILES
**/bun.lockb
gateway/.vscode/*
7 changes: 6 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ services:
build:
context: ./services/postgres
image: porters-schema:latest
env_file:
- .env
networks:
- portal
command: tail -f /dev/null

frontend:
env_file:
Expand Down Expand Up @@ -81,7 +86,7 @@ services:
ports:
- "8080:8080"
env_file:
- ./.env
- ./.kit.env
networks:
- gateway

Expand Down
23 changes: 23 additions & 0 deletions gateway/proxy/kitMetricsKitRouter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package proxy

import (
"fmt"
"io"
"net/http"
)

func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl string) {
kitMetricsUrl := fmt.Sprintf("%s/metrics", proxyToUrl)

// Forward the request to the kit's /metrics endpoint
resp, err := http.Get(kitMetricsUrl)
if err != nil {
http.Error(w, "Unable to retrieve kit metrics", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

// Copy the response body to the proxy response
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
47 changes: 28 additions & 19 deletions gateway/proxy/mux.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package proxy

import (
"fmt"
"net/http"
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"

"porters/common"
"porters/common"
)

const (
Expand All @@ -17,30 +17,39 @@ const (
)

func PluckAppId(req *http.Request) string {
appId := mux.Vars(req)[APP_PATH]
return appId
appId := mux.Vars(req)[APP_PATH]
return appId
}

func PluckProductName(req *http.Request) string {
productName := mux.Vars(req)[PRODUCT_NAME]
return productName
productName := mux.Vars(req)[PRODUCT_NAME]
return productName
}

func addProxyRoutes(r *mux.Router) *mux.Router {
proxyHost := common.GetConfig(common.HOST)
host := fmt.Sprintf(`{%s}.%s`, PRODUCT_NAME, proxyHost)
subrouter := r.Host(host).Subrouter()
return subrouter
proxyHost := common.GetConfig(common.HOST)
host := fmt.Sprintf(`{%s}.%s`, PRODUCT_NAME, proxyHost)
subrouter := r.Host(host).Subrouter()
return subrouter
}

func addHealthcheckRoute(r *mux.Router) *mux.Router {
subrouter := r.PathPrefix("/health").Subrouter()
subrouter.HandleFunc("", healthHandler)
return subrouter
subrouter := r.PathPrefix("/health").Subrouter()
subrouter.HandleFunc("", healthHandler)
return subrouter
}

func addMetricsRoute(r *mux.Router) *mux.Router {
subrouter := r.PathPrefix("/metrics").Subrouter()
subrouter.Handle("", promhttp.Handler())
return subrouter
subrouter := r.PathPrefix("/metrics").Subrouter()
subrouter.Handle("", promhttp.Handler())
return subrouter
}

// Since the Gateway Kit is on an internal private network, with only the Gateway having access to it, we proxy a gateway-kit/metrics endpoint to expose the data to POKTScan
func addMetricsKitRoute(r *mux.Router, proxyToUrl string) *mux.Router {
subrouter := r.PathPrefix("/gateway-kit/metrics").Subrouter()
subrouter.HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
kitMetricsHandler(w, r, proxyToUrl)
})
return subrouter
}
1 change: 1 addition & 0 deletions gateway/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Start() {

_ = addHealthcheckRoute(router)
_ = addMetricsRoute(router)
_ = addMetricsKitRoute(router, proxyUrl)

port := fmt.Sprintf(":%d", common.GetConfigInt(common.PORT))
server = &http.Server{Addr: port, Handler: router}
Expand Down
10 changes: 9 additions & 1 deletion services/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ FROM node:21-alpine

WORKDIR /usr/src/app

COPY --chown=node:node ./ ./services/postgres/
COPY --chown=node:node . .

RUN apk add --no-cache bash

RUN npm install -g ts-node typescript @types/node prisma@latest @prisma/client

RUN npx prisma generate --schema=./schema.prisma

CMD ["npx", "prisma", "generate", "--schema=./schema.prisma"]
7 changes: 7 additions & 0 deletions services/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ and
This makes the docker image available in fly.io for the images dependent on it.
Currently the golang code doesn't generate any code from the schema, but this
could change in the future.


## Running locally

To run locally you may need to pull the `porters-schema` docker image manually through `docker pull docker.io/library/porters-schema:latest`

The migrations will run and set up the database based ont he `DATABASE_URL` connection string. To seed the database, bash into the container and run `ts-node seed.ts`

0 comments on commit 3076a88

Please sign in to comment.