Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proxy the grafana, temporal to main ui #23

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pocketbase: air
ui: ./scripts/wait-for-it.sh -t 0 localhost:8090 && cd webapp && bun i && bun dev
ui: ./scripts/wait-for-it.sh -s -t 0 localhost:8090 && cd webapp && bun i && bun dev
docs: cd docs && bun i && bun run docs:dev
temporal: temporal server start-dev --db-filename pb_data/temporal.db
temporal: temporal server start-dev --ui-port 8080 --db-filename pb_data/temporal.db --ui-public-path /workflows
4 changes: 2 additions & 2 deletions deployment/grafana/provisioning/datasources/all.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apiVersion: 1

datasources:
- name: 'Temporal Prometheus'
- name: 'DIDimo prometheus'
type: 'prometheus'
org_id: 1
url: 'http://prometheus:9090'
is_default: true
version: 1
editable: true
editable: true
11 changes: 10 additions & 1 deletion docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
- TEMPORAL_OPENAPI_ENABLED=true
- TEMPORAL_UI_ENABLED=true
- TEMPORAL_ADDRESS=temporal:7233
- TEMPORAL_UI_PUBLIC_PATH=/workflows
# - TEMPORAL_CODEC_ENDPOINT=http://localhost:8081
image: temporalio/ui:2.32.0
ports:
Expand All @@ -51,8 +52,16 @@ services:
- GF_AUTH_DISABLE_LOGIN_FORM=true
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_SERVER_SERVE_FROM_SUB_PATH=true
- GF_SERVER_ROOT_URL=http://localhost:8085/monitoring
- GF_SECURITY_ALLOWED_CORS_ORIGINS=http://localhost:8090
- GF_SECURITY_CSRF_TRUSTED_ORIGINS=http://localhost:8090
- GF_SECURITY_COOKIE_SAMESITE=disabled
- GF_SECURITY_CONTENT_SECURITY_POLICY=false
- GF_SERVER_HTTP_PORT=8085
- GF_SECURITY_ACTIONS_ALLOW_POST_URL=/*
ports:
- 8085:3000
- 8085:8085
volumes:
- type: bind
source: ./deployment/grafana/provisioning/datasources
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ services:
ports:
- "8090:8090"
environment:
PORT: 5100
- ADDRESS_TEMPORAL=http://temporal-ui:8080
- ADDRESS_GRAFANA=http://grafana:8085
- ADDRESS_UI=http://localhost:5100
- PORT=5100
volumes:
- ./pb_data/:/pb_data
52 changes: 43 additions & 9 deletions pkg/routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package routes

Check warning on line 1 in pkg/routes/routes.go

View workflow job for this annotation

GitHub Actions / Lint

should have a package comment

import (
"encoding/json"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"

"github.com/forkbombeu/didimo/pkg/internal/pb"
"github.com/pocketbase/dbx"
Expand All @@ -16,9 +18,17 @@
)

func Setup(app *pocketbase.PocketBase) {
routes := map[string]string{
"/workflows/{path...}": getEnv("ADDRESS_TEMPORAL", "http://localhost:8080"),
"/monitoring/{path...}": getEnv("ADDRESS_GRAFANA", "http://localhost:8085"),
"/{path...}": getEnv("ADDRESS_UI", "http://localhost:5100"),
}

app.OnServe().BindFunc(func(se *core.ServeEvent) error {
se.Router.Any("/*", proxyHandler)
se.Router.Any("/", proxyHandler)
for path, target := range routes {
se.Router.Any(path, createReverseProxy(target))
}

se.Router.POST("/api/keypairoom-server", func(e *core.RequestEvent) error {
var body map[string]map[string]interface{}

Expand Down Expand Up @@ -88,11 +98,35 @@
})
}

func proxyHandler(req *core.RequestEvent) error {
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: "localhost:5100",
})
proxy.ServeHTTP(req.Response, req.Request)
return nil
func createReverseProxy(target string) func(r *core.RequestEvent) error {
return func(r *core.RequestEvent) error {
targetURL, err := url.Parse(target)
if err != nil {
return err
}
log.Printf("Proxying request: %s -> %s%s", r.Request.URL.Path, targetURL.String(), r.Request.URL.Path)

proxy := httputil.NewSingleHostReverseProxy(targetURL)
proxy.Director = func(req *http.Request) {
req.URL.Scheme = targetURL.Scheme
req.URL.Host = targetURL.Host
req.Header.Set("Host", targetURL.Host)
req.Header.Set("X-Forwarded-For", req.RemoteAddr)
if origin := req.Header.Get("Origin"); origin != "" {
req.Header.Set("Origin", origin)
}
if referer := req.Header.Get("Referer"); referer != "" {
req.Header.Set("Referer", referer)
}
}
proxy.ServeHTTP(r.Response, r.Request)
return nil
}
}

func getEnv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
Loading