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

let oryx support external/outside redis and srs service #225

Closed
Closed
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
3 changes: 1 addition & 2 deletions platform/dvr-local-disk.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// Copyright (c) 2022-2023 Winlin
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//
package main

import (
Expand All @@ -21,6 +19,7 @@ import (
"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
Expand Down
14 changes: 10 additions & 4 deletions platform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path"
Expand All @@ -16,10 +17,10 @@ import (
"strings"
"syscall"
"time"
_ "net/http/pprof"

"github.com/ossrs/go-oryx-lib/errors"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
Expand Down Expand Up @@ -108,9 +109,14 @@ func doMain(ctx context.Context) error {
setEnvDefault("GO_PPROF", "off")

// Migrate from mgmt.
setEnvDefault("REDIS_HOST", "127.0.0.1")
setEnvDefault("REDIS_PORT", "6379")
setEnvDefault("MGMT_LISTEN", "2022")

// SRS HOST
setEnvDefault("SRS_HOST", "127.0.0.1")
setEnvDefault("SRS_HTTP_STREAM_PORT", "8080")

// For HTTPS.
setEnvDefault("HTTPS_LISTEN", "2443")
setEnvDefault("AUTO_SELF_SIGNED_CERTIFICATE", "on")
Expand All @@ -127,15 +133,15 @@ func doMain(ctx context.Context) error {

logger.Tf(ctx, "load .env as MGMT_PASSWORD=%vB, GO_PPROF=%v, "+
"SRS_PLATFORM_SECRET=%vB, CLOUD=%v, REGION=%v, SOURCE=%v, SRT_PORT=%v, RTC_PORT=%v, "+
"NODE_ENV=%v, LOCAL_RELEASE=%v, REDIS_PASSWORD=%vB, REDIS_PORT=%v, RTMP_PORT=%v, "+
"NODE_ENV=%v, LOCAL_RELEASE=%v, REDIS_PASSWORD=%vB, REDIS_HOST=%v, REDIS_PORT=%v, SRS_HOST=%v, RTMP_PORT=%v, "+
"PUBLIC_URL=%v, BUILD_PATH=%v, REACT_APP_LOCALE=%v, PLATFORM_LISTEN=%v, HTTP_PORT=%v, "+
"REGISTRY=%v, MGMT_LISTEN=%v, HTTPS_LISTEN=%v, AUTO_SELF_SIGNED_CERTIFICATE=%v, "+
"NAME_LOOKUP=%v, PLATFORM_DOCKER=%v",
len(os.Getenv("MGMT_PASSWORD")), os.Getenv("GO_PPROF"),
len(os.Getenv("SRS_PLATFORM_SECRET")), os.Getenv("CLOUD"),
os.Getenv("REGION"), os.Getenv("SOURCE"), os.Getenv("SRT_PORT"), os.Getenv("RTC_PORT"),
os.Getenv("NODE_ENV"), os.Getenv("LOCAL_RELEASE"),
len(os.Getenv("REDIS_PASSWORD")), os.Getenv("REDIS_PORT"), os.Getenv("RTMP_PORT"), os.Getenv("PUBLIC_URL"),
len(os.Getenv("REDIS_PASSWORD")), os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT"), os.Getenv("SRS_HOST"), os.Getenv("RTMP_PORT"), os.Getenv("PUBLIC_URL"),
os.Getenv("BUILD_PATH"), os.Getenv("REACT_APP_LOCALE"), os.Getenv("PLATFORM_LISTEN"), os.Getenv("HTTP_PORT"),
os.Getenv("REGISTRY"), os.Getenv("MGMT_LISTEN"), os.Getenv("HTTPS_LISTEN"),
os.Getenv("AUTO_SELF_SIGNED_CERTIFICATE"), os.Getenv("NAME_LOOKUP"),
Expand All @@ -148,7 +154,7 @@ func doMain(ctx context.Context) error {
addr := "localhost:6060"
logger.Tf(ctx, "Start Go pprof at %v", addr)
http.ListenAndServe(addr, nil)
} ()
}()
}

// Setup the base OS for redis, which should never depends on redis.
Expand Down
14 changes: 8 additions & 6 deletions platform/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"io"
"io/ioutil"
"net/http"
Expand All @@ -19,9 +18,12 @@ import (
"sync"
"time"

"github.com/joho/godotenv"

"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
)
Expand Down Expand Up @@ -282,22 +284,22 @@ func handleHTTPService(ctx context.Context, handler *http.ServeMux) error {
handleMgmtStreamsKickoff(ctx, handler)
handleMgmtUI(ctx, handler)

proxy2023, err := httpCreateProxy("http://127.0.0.1:2023")
proxy2023, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":2023")
if err != nil {
return err
}

proxy1985, err := httpCreateProxy("http://127.0.0.1:1985")
proxy1985, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":1985")
if err != nil {
return err
}

proxyWhxp, err := httpCreateProxy("http://127.0.0.1:1985")
proxyWhxp, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":1985")
if err != nil {
return err
}

proxy8080, err := httpCreateProxy("http://127.0.0.1:8080")
proxy8080, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":8080")
if err != nil {
return err
}
Expand Down Expand Up @@ -1534,7 +1536,7 @@ func handleMgmtStreamsKickoff(ctx context.Context, handler *http.ServeMux) {

// Whether client exists in SRS server.
var code int
clientURL := fmt.Sprintf("http://127.0.0.1:1985/api/v1/clients/%v", streamObject.Client)
clientURL := fmt.Sprintf("http://%v:1985/api/v1/clients/%v", os.Getenv("SRS_HOST"), streamObject.Client)
if r0, body, err := requestClient(ctx, clientURL, http.MethodGet); err != nil {
return errors.Wrapf(err, "http query client %v", clientURL)
} else if r0 != 0 && r0 != ErrorRtmpClientNotFound {
Expand Down
38 changes: 35 additions & 3 deletions platform/srs-hooks.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
//
// Copyright (c) 2022-2023 Winlin
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//
package main

import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
Expand Down Expand Up @@ -725,8 +726,39 @@ func handleOnHls(ctx context.Context, handler *http.ServeMux) error {
if msg.Action != SrsActionOnHls {
return errors.Errorf("invalid action=%v", msg.Action)
}

if _, err := os.Stat(msg.File); err != nil {
return errors.Wrapf(err, "invalid ts file %v", msg.File)
logger.Tf(ctx, "invalid ts file %v", msg.File)

if err := os.MkdirAll(filepath.Dir(msg.File), 0755); err != nil {
return errors.Wrapf(err, "failed to create ts file directory %v", filepath.Dir(msg.File))
}

if tsFile, err := os.Create(msg.File); err != nil {
return errors.Wrapf(err, "failed to create ts file %v", msg.File)
} else {
tsUrl := "http://" + os.Getenv("SRS_HOST") + ":" + os.Getenv("SRS_HTTP_STREAM_PORT") + "/" + msg.URL
logger.Tf(ctx, "download ts from %v", tsUrl)
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}

resp, err := client.Get(tsUrl)
if err != nil {
return errors.Wrapf(err, "http error to get url %v", tsUrl)
}
defer resp.Body.Close()

size, err := io.Copy(tsFile, resp.Body)
if err != nil {
return errors.Wrapf(err, "copy http resp to file %v", tsFile)
}
defer tsFile.Close()
logger.Tf(ctx, "Download ts file %s with size %d", tsUrl, size)
}
}
logger.Tf(ctx, "on_hls ok, %v", string(b))

Expand Down
11 changes: 7 additions & 4 deletions platform/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/ossrs/go-oryx-lib/errors"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/golang-jwt/jwt/v4"
Expand Down Expand Up @@ -361,7 +362,7 @@ var rdb *redis.Client

// InitRdb create and init global rdb, which is a redis client.
func InitRdb() error {
addr := "127.0.0.1"
addr := os.Getenv("REDIS_HOST")
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%v:%v", addr, os.Getenv("REDIS_PORT")),
Password: os.Getenv("REDIS_PASSWORD"),
Expand Down Expand Up @@ -615,10 +616,11 @@ func srsGenerateConfig(ctx context.Context) error {

// Reload SRS to apply the new config.
if true {
api := "http://127.0.0.1:1985/api/v1/raw?rpc=reload"
api := "http://" + os.Getenv("SRS_HOST") + ":1985/api/v1/raw?rpc=reload"
res, err := http.DefaultClient.Get(api)
if err != nil {
return errors.Wrapf(err, "reload srs %v", api)
logger.Tf(ctx, "srs api reload error %v", err)
return nil
}
defer res.Body.Close()

Expand All @@ -628,7 +630,8 @@ func srsGenerateConfig(ctx context.Context) error {
}

if res.StatusCode != http.StatusOK {
return errors.Errorf("reload srs %v, code=%v, body=%v", api, res.StatusCode, string(b))
logger.Tf(ctx, "reload srs %v, code=%v, body=%v", api, res.StatusCode, string(b))
return nil
}
logger.Tf(ctx, "reload submit srs ok")
}
Expand Down
Loading