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 = ` + +
+ + +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.
+OpenVidu Version: {{ .OpenViduVersion }}
+{{ .OpenViduSecret }}{{ .LiveKitApiKey }}{{ .LiveKitApiSecret }}{{ .DashboardAdminUsername }}{{ .DashboardAdminPassword }}{{ .MinioAdminKey }}{{ .MinioAdminSecret }}If you want to access this deployment with http(s)://localhost:4443, just change the LOCAL_DOMAIN variable to localhost in the .env file.
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.
If you want to disable TLS, just change the USE_TLS variable to false in the .env file.
If you want to enable TLS, just change the USE_TLS variable to true in the .env file.