diff --git a/.gitignore b/.gitignore index 9fadc87a..ac7a8eea 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ jspm_packages/ .env.test.local .env.production.local .env.local +*.env # Next.js build output .next @@ -56,3 +57,4 @@ docs/pnpm-lock.yaml # IGNORE BUN LOCKB FILES **/bun.lockb +gateway/.vscode/* diff --git a/docker-compose.yaml b/docker-compose.yaml index d7c5c2b6..b9bea7e2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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: @@ -81,7 +86,7 @@ services: ports: - "8080:8080" env_file: - - ./.env + - ./.kit.env networks: - gateway diff --git a/gateway/proxy/kitMetricsKitRouter.go b/gateway/proxy/kitMetricsKitRouter.go new file mode 100644 index 00000000..a7381181 --- /dev/null +++ b/gateway/proxy/kitMetricsKitRouter.go @@ -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) +} diff --git a/gateway/proxy/mux.go b/gateway/proxy/mux.go index 4d9ff927..25f70d0a 100644 --- a/gateway/proxy/mux.go +++ b/gateway/proxy/mux.go @@ -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 ( @@ -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 } diff --git a/gateway/proxy/proxy.go b/gateway/proxy/proxy.go index f0ddae82..72d1be2f 100644 --- a/gateway/proxy/proxy.go +++ b/gateway/proxy/proxy.go @@ -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} diff --git a/services/postgres/Dockerfile b/services/postgres/Dockerfile index e34fc18c..c988dd81 100644 --- a/services/postgres/Dockerfile +++ b/services/postgres/Dockerfile @@ -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"] \ No newline at end of file diff --git a/services/postgres/README.md b/services/postgres/README.md index 0fe0e3a6..ccd7170c 100644 --- a/services/postgres/README.md +++ b/services/postgres/README.md @@ -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` \ No newline at end of file