Skip to content

Commit

Permalink
Support static files.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 5, 2024
1 parent e2b7c27 commit 3facf49
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
12 changes: 9 additions & 3 deletions proxy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package main

import (
"context"
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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()),
})
})

Expand Down
12 changes: 9 additions & 3 deletions proxy/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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, "+
Expand All @@ -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(),
Expand All @@ -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")
}
Expand Down
17 changes: 17 additions & 0 deletions proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})

Expand Down

0 comments on commit 3facf49

Please sign in to comment.