From c9aa15d12488e303ef475a14f984c91f1eb95390 Mon Sep 17 00:00:00 2001 From: cruizba Date: Mon, 18 Mar 2024 12:20:01 +0100 Subject: [PATCH] Add caddy-proxy docker image to allow customization --- caddy-proxy/Dockerfile | 23 +++++ caddy-proxy/README.md | 7 ++ caddy-proxy/entrypoint.sh | 26 +++++ caddy-proxy/go.mod | 3 + caddy-proxy/main.go | 164 ++++++++++++++++++++++++++++++++ caddy-proxy/templates/app502.go | 46 +++++++++ caddy-proxy/templates/caddy.go | 85 +++++++++++++++++ caddy-proxy/templates/index.go | 86 +++++++++++++++++ 8 files changed, 440 insertions(+) create mode 100644 caddy-proxy/Dockerfile create mode 100644 caddy-proxy/README.md create mode 100644 caddy-proxy/entrypoint.sh create mode 100644 caddy-proxy/go.mod create mode 100644 caddy-proxy/main.go create mode 100644 caddy-proxy/templates/app502.go create mode 100644 caddy-proxy/templates/caddy.go create mode 100644 caddy-proxy/templates/index.go diff --git a/caddy-proxy/Dockerfile b/caddy-proxy/Dockerfile new file mode 100644 index 0000000..c8b5adb --- /dev/null +++ b/caddy-proxy/Dockerfile @@ -0,0 +1,23 @@ +FROM golang:1.22.1 as builder + +ARG TARGETOS +ARG TARGETPLATFORM +ARG TARGETARCH + +WORKDIR /workspace +COPY . . +RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build + +FROM caddy/caddy:2.7.6-alpine + +ARG VERSION +RUN test -n "$VERSION" || (echo "VERSION arg is not set" && false) +ENV VERSION $VERSION + +COPY --from=builder /workspace/local-caddy-generate /usr/bin/local-caddy-generate +COPY --from=builder /workspace/entrypoint.sh /entrypoint.sh +RUN chmod +x /usr/bin/local-caddy-generate /entrypoint.sh + +# Run the binary. +ENTRYPOINT ["/entrypoint.sh"] +CMD ["/usr/bin/caddy", "run", "--config", "/config/caddy/Caddyfile"] diff --git a/caddy-proxy/README.md b/caddy-proxy/README.md new file mode 100644 index 0000000..8a5d447 --- /dev/null +++ b/caddy-proxy/README.md @@ -0,0 +1,7 @@ +# OpenVidu Local Deployment - Cadddy Proxy + +If you want to modify any of the rules at the caddy-proxy container, just build the image again and run the local deployment with the new image. + +```bash +docker build --build-arg VERSION=custom -t caddy-proxy . +``` diff --git a/caddy-proxy/entrypoint.sh b/caddy-proxy/entrypoint.sh new file mode 100644 index 0000000..94c740b --- /dev/null +++ b/caddy-proxy/entrypoint.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -e + +# Generate Caddyfile and index.html +CURRENT_DIR="$(pwd)" +TMP_DIR="/tmp/caddy-local" +mkdir -p "$TMP_DIR" +cd "$TMP_DIR" +/usr/bin/local-caddy-generate +if [ ! -f /var/www/index.html ]; then + mkdir -p /var/www + cp "$TMP_DIR/index.html" /var/www/index.html +fi +if [ ! -f /var/www/app502.html ]; then + mkdir -p /var/www + cp "$TMP_DIR/app502.html" /var/www/app502.html +fi +if [ ! -f /config/caddy/Caddyfile ]; then + mkdir -p /config/caddy + cp "$TMP_DIR/Caddyfile" /config/caddy/Caddyfile +fi +cd "$CURRENT_DIR" +rm -rf /tmp/caddy-local + +# Start Caddy +exec "$@" diff --git a/caddy-proxy/go.mod b/caddy-proxy/go.mod new file mode 100644 index 0000000..d8863f0 --- /dev/null +++ b/caddy-proxy/go.mod @@ -0,0 +1,3 @@ +module local-caddy-generate + +go 1.22.1 diff --git a/caddy-proxy/main.go b/caddy-proxy/main.go new file mode 100644 index 0000000..694f68b --- /dev/null +++ b/caddy-proxy/main.go @@ -0,0 +1,164 @@ +package main + +import ( + "bytes" + "fmt" + "html/template" + "local-caddy-generate/templates" + "os" + "strconv" + "strings" +) + +type TemplateData any + +var indexData = &templates.IndexData{} +var caddyData = &templates.CaddyData{} + +func main() { + err := Initialize() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + rawIndex, err := GenerateTemplate(templates.IndexTemplate, indexData) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = WriteStringToFile("index.html", rawIndex) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + rawCaddyfile, err := GenerateTemplate(templates.CaddyfileTemplate, caddyData) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = WriteStringToFile("Caddyfile", rawCaddyfile) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + rawApp502, err := GenerateTemplate(templates.App502Template, nil) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = WriteStringToFile("app502.html", rawApp502) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + +} + +func Initialize() error { + version := os.Getenv("VERSION") + if version == "" { + return fmt.Errorf("VERSION is not set") + } + + localDomain := os.Getenv("LOCAL_DOMAIN") + if localDomain == "" { + return fmt.Errorf("LOCAL_DOMAIN is not set") + } + + rawUseTLS := os.Getenv("USE_TLS") + if rawUseTLS == "" { + return fmt.Errorf("USE_TLS is not set") + } + useTLS, err := strconv.ParseBool(rawUseTLS) + if err != nil { + return fmt.Errorf("USE_TLS is not a boolean") + } + + livekitApiKey := os.Getenv("LIVEKIT_API_KEY") + if livekitApiKey == "" { + return fmt.Errorf("LIVEKIT_API_KEY is not set") + } + livekitApiSecret := os.Getenv("LIVEKIT_API_SECRET") + if livekitApiSecret == "" { + return fmt.Errorf("LIVEKIT_API_SECRET is not set") + } + openviduSecret := os.Getenv("OPENVIDU_SHIM_SECRET") + if openviduSecret == "" { + return fmt.Errorf("OPENVIDU_SHIM_SECRET is not set") + } + dashboadAdminUsername := os.Getenv("DASHBOARD_ADMIN_USERNAME") + if dashboadAdminUsername == "" { + return fmt.Errorf("DASHBOARD_ADMIN_USERNAME is not set") + } + dashboardAdminPassword := os.Getenv("DASHBOARD_ADMIN_PASSWORD") + if dashboardAdminPassword == "" { + return fmt.Errorf("DASHBOARD_ADMIN_PASSWORD is not set") + } + minioAccessKey := os.Getenv("MINIO_ACCESS_KEY") + if minioAccessKey == "" { + return fmt.Errorf("MINIO_ACCESS_KEY is not set") + } + minioSecretKey := os.Getenv("MINIO_SECRET_KEY") + if minioSecretKey == "" { + return fmt.Errorf("MINIO_SECRET_KEY is not set") + } + + indexData = &templates.IndexData{ + OpenViduVersion: version, + DomainName: localDomain, + IsLocalhost: localDomain == "localhost", + IsTLS: useTLS, + LiveKitApiKey: livekitApiKey, + LiveKitApiSecret: livekitApiSecret, + OpenViduSecret: openviduSecret, + DashboardAdminUsername: dashboadAdminUsername, + DashboardAdminPassword: dashboardAdminPassword, + MinioAdminKey: minioAccessKey, + MinioAdminSecret: minioSecretKey, + } + + caddyData = &templates.CaddyData{ + DomainName: localDomain, + IsLocalhost: localDomain == "localhost", + IsTLS: useTLS, + } + + return nil + +} + +func GenerateTemplate(templateString string, data TemplateData) (string, error) { + funcs := map[string]any{ + "contains": strings.Contains, + "hasPrefix": strings.HasPrefix, + "hasSuffix": strings.HasSuffix} + + tmpl, err := template.New("template").Funcs(funcs).Parse(templateString) + if err != nil { + return "", err + } + + var buf bytes.Buffer + if err := tmpl.Execute(&buf, data); err != nil { + return "", err + } + + return buf.String(), nil +} + +func WriteStringToFile(filePath string, data string) error { + file, err := os.Create(filePath) + if err != nil { + return err + } + defer file.Close() + + _, err = file.WriteString(data) + if err != nil { + return err + } + + return nil +} diff --git a/caddy-proxy/templates/app502.go b/caddy-proxy/templates/app502.go new file mode 100644 index 0000000..80dd296 --- /dev/null +++ b/caddy-proxy/templates/app502.go @@ -0,0 +1,46 @@ +package templates + +const App502Template = ` + + + + + 502 - Application Not Found + + + + + + +
+
+
502 - Bad Gateway
+

OpenVidu Application Not Found

+
+

If you are developing an application and run it locally at port 5442, you will see here your application, under + the same domain and TLS certificate as OpenVidu.

+
+
+ + + + +` diff --git a/caddy-proxy/templates/caddy.go b/caddy-proxy/templates/caddy.go new file mode 100644 index 0000000..65bb938 --- /dev/null +++ b/caddy-proxy/templates/caddy.go @@ -0,0 +1,85 @@ +package templates + +type CaddyData struct { + DomainName string + IsLocalhost bool + IsTLS bool +} + +const CaddyfileTemplate = ` +# Minio +{{- if hasSuffix .DomainName ".openvidu-local.dev" }} +http{{if .IsTLS}}s{{end}}://*.openvidu-local.dev:9000, http{{if .IsTLS}}s{{end}}://openvidu-local.dev:9000 { +{{- else }} +http{{if .IsTLS}}s{{end}}://{{.DomainName}}:9000 { +{{- end }} + {{if .IsTLS}}{{if hasSuffix .DomainName "openvidu-local.dev"}}tls internal { + get_certificate http https://certs.openvidu-local.dev/caddy.pem + }{{else}}tls internal{{end}}{{end}} + reverse_proxy http://minio:9000 +} + +# General +{{- if hasSuffix .DomainName ".openvidu-local.dev" }} +http{{if .IsTLS}}s{{end}}://*.openvidu-local.dev:4443, http{{if .IsTLS}}s{{end}}://openvidu-local.dev:4443 { +{{- else }} +http{{if .IsTLS}}s{{end}}://{{.DomainName}}:4443 { +{{- end }} + {{if .IsTLS}}{{if hasSuffix .DomainName "openvidu-local.dev"}}tls internal { + get_certificate http https://certs.openvidu-local.dev/caddy.pem + }{{else}}tls internal{{end}}{{end}} + + # Api + @openvidu path /twirp/* /rtc/* /rtc + handle @openvidu { + reverse_proxy http://openvidu:7880 + } + + # Minio console + redir /minio-console /minio-console/ + handle_path /minio-console/* { + uri strip_prefix /minio-console + reverse_proxy http://minio:9001 + } + + # OpenVidu Dashboard + redir /dashboard /dashboard/ + handle_path /dashboard/* { + rewrite * {path} + reverse_proxy http://dashboard:5000 + } + + # OpenVidu Call (Default App) + redir /openvidu-call /openvidu-call/ + handle_path /openvidu-call/* { + rewrite * {path} + reverse_proxy http://default-app:5442 + } + + # Default / + handle_path /* { + root * /var/www/ + file_server + } +} + +# Your OpenVidu App +{{- if hasSuffix .DomainName ".openvidu-local.dev" }} +http{{if .IsTLS}}s{{end}}://*.openvidu-local.dev:8000, http{{if .IsTLS}}s{{end}}://openvidu-local.dev:8000 { +{{- else }} +http{{if .IsTLS}}s{{end}}://{{.DomainName}}:8000 { +{{- end }} + {{if .IsTLS}}{{if hasSuffix .DomainName "openvidu-local.dev"}}tls internal { + get_certificate http https://certs.openvidu-local.dev/caddy.pem + }{{else}}tls internal{{end}}{{end}} + handle_errors { + @502 expression {http.error.status_code} == 502 + rewrite @502 /app502.html + file_server { + root /var/www + } + } + reverse_proxy http://host.docker.internal:5442 +} + +` diff --git a/caddy-proxy/templates/index.go b/caddy-proxy/templates/index.go new file mode 100644 index 0000000..97b0665 --- /dev/null +++ b/caddy-proxy/templates/index.go @@ -0,0 +1,86 @@ +package templates + +type IndexData struct { + OpenViduVersion string + DomainName string + IsLocalhost bool + IsTLS bool + DashboardAdminUsername string + DashboardAdminPassword string + MinioAdminKey string + MinioAdminSecret string + LiveKitApiKey string + LiveKitApiSecret string + OpenViduSecret string +} + +const IndexTemplate = ` + + + + + OpenVidu Local Deployment + + + + + + +
+
+

Welcome to OpenVidu Local Deployment

+

OpenVidu Version: {{ .OpenViduVersion }}

+ +
+ +
+ {{- if not .IsLocalhost }} +

If you want to access this deployment with http(s)://localhost:4443, just change the LOCAL_DOMAIN variable to localhost in the .env file.

+ {{- else }} +

If you want to access this deployment with http(s)://openvidu-local.dev:4443, just change the LOCAL_DOMAIN variable to openvidu-local.dev in the .env file.

+ {{- end }} + {{- if .IsTLS }} +

If you want to disable TLS, just change the USE_TLS variable to false in the .env file.

+ {{- else }} +

If you want to enable TLS, just change the USE_TLS variable to true in the .env file.

+ {{- end }} +
+
+ +`