diff --git a/proxy/api.go b/proxy/api.go index 26799c907b3..b12fddf240a 100644 --- a/proxy/api.go +++ b/proxy/api.go @@ -5,7 +5,9 @@ package main import ( "context" + "fmt" "net/http" + "os" "strings" "sync" "time" @@ -235,9 +237,13 @@ func (v *systemAPI) Run(ctx context.Context) error { apiError(ctx, w, r, err) } - apiResponse(ctx, w, r, map[string]string{ - "signature": Signature(), - "version": Version(), + type Response struct { + Code int `json:"code"` + PID string `json:"pid"` + } + + apiResponse(ctx, w, r, &Response{ + Code: 0, PID: fmt.Sprintf("%v", os.Getpid()), }) }) diff --git a/proxy/env.go b/proxy/env.go index dfe582014cb..8ec2b0e8254 100644 --- a/proxy/env.go +++ b/proxy/env.go @@ -21,7 +21,7 @@ func loadEnvFile(ctx context.Context) error { } else { envFile := path.Join(workDir, ".env") if _, err := os.Stat(envFile); err == nil { - if err := godotenv.Overload(envFile); err != nil { + if err := godotenv.Load(envFile); err != nil { return errors.Wrapf(err, "load %v", envFile) } } @@ -50,6 +50,8 @@ func buildDefaultEnvironmentVariables(ctx context.Context) { setEnvDefault("PROXY_SRT_SERVER", "20080") // The API server of proxy itself. setEnvDefault("PROXY_SYSTEM_API", "12025") + // The static directory for web server. + setEnvDefault("PROXY_STATIC_FILES", "../trunk/research") // The load balancer, use redis or memory. setEnvDefault("PROXY_LOAD_BALANCER_TYPE", "memory") @@ -79,7 +81,7 @@ func buildDefaultEnvironmentVariables(ctx context.Context) { "PROXY_FORCE_QUIT_TIMEOUT=%v, PROXY_GRACE_QUIT_TIMEOUT=%v, "+ "PROXY_HTTP_API=%v, PROXY_HTTP_SERVER=%v, PROXY_RTMP_SERVER=%v, "+ "PROXY_WEBRTC_SERVER=%v, PROXY_SRT_SERVER=%v, "+ - "PROXY_SYSTEM_API=%v, PROXY_DEFAULT_BACKEND_ENABLED=%v, "+ + "PROXY_SYSTEM_API=%v, PROXY_STATIC_FILES=%v, PROXY_DEFAULT_BACKEND_ENABLED=%v, "+ "PROXY_DEFAULT_BACKEND_IP=%v, PROXY_DEFAULT_BACKEND_RTMP=%v, "+ "PROXY_DEFAULT_BACKEND_HTTP=%v, PROXY_DEFAULT_BACKEND_API=%v, "+ "PROXY_DEFAULT_BACKEND_RTC=%v, PROXY_DEFAULT_BACKEND_SRT=%v, "+ @@ -89,7 +91,7 @@ func buildDefaultEnvironmentVariables(ctx context.Context) { envForceQuitTimeout(), envGraceQuitTimeout(), envHttpAPI(), envHttpServer(), envRtmpServer(), envWebRTCServer(), envSRTServer(), - envSystemAPI(), envDefaultBackendEnabled(), + envSystemAPI(), envStaticFiles(), envDefaultBackendEnabled(), envDefaultBackendIP(), envDefaultBackendRTMP(), envDefaultBackendHttp(), envDefaultBackendAPI(), envDefaultBackendRTC(), envDefaultBackendSRT(), @@ -98,6 +100,10 @@ func buildDefaultEnvironmentVariables(ctx context.Context) { ) } +func envStaticFiles() string { + return os.Getenv("PROXY_STATIC_FILES") +} + func envDefaultBackendSRT() string { return os.Getenv("PROXY_DEFAULT_BACKEND_SRT") } diff --git a/proxy/http.go b/proxy/http.go index 7f66c8ee164..4bfa133b976 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -91,6 +91,17 @@ func (v *httpServer) Run(ctx context.Context) error { apiResponse(ctx, w, r, &res) }) + // The static web server, for the web pages. + var staticServer http.Handler + if staticFiles := envStaticFiles(); staticFiles != "" { + if _, err := os.Stat(staticFiles); err != nil { + return errors.Wrapf(err, "invalid static files %v", staticFiles) + } + + staticServer = http.FileServer(http.Dir(staticFiles)) + logger.Df(ctx, "Handle static files at %v", staticFiles) + } + // The default handler, for both static web server and streaming server. logger.Df(ctx, "Handle / by %v", addr) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -132,6 +143,12 @@ func (v *httpServer) Run(ctx context.Context) error { return } + // Serve by static server. + if staticServer != nil { + staticServer.ServeHTTP(w, r) + return + } + http.NotFound(w, r) })