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

WIP - live获取配置 #97

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion core/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func StartWithConfig(path string) error {
errCh <- fmt.Errorf("signal %s", sig)
}()

c, err := config.LoadConfig(path)
c, err := config.LoadConfig(log, path)
if err != nil {
return fmt.Errorf("load config file error %v", err)
}
Expand Down
93 changes: 92 additions & 1 deletion core/config/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
package config

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/spf13/viper"

"github.com/qbox/livekit/utils/logger"
"github.com/qbox/livekit/utils/qiniumac"
"github.com/qbox/livekit/utils/rpc"
)

type Config struct {
v *viper.Viper
}

func LoadConfig(path string) (*Config, error) {
type QiniuCinfig struct {
RequestId string `json:"request_id"`
Code int `json:"code"`
Message string `json:"message"`
Data []byte `json:"data"`
}

type QiniuConfigReq struct {
Rtc Rtc `json:"rtc,omitempty"`
Pili Pili `json:"pili,omitempty"`
Censor Censor `json:"censor,omitempty"`
Im Im `json:"im,omitempty"`
}
type Rtc struct {
AppId string `json:"app_id"`
}

type Pili struct {
Hub string `json:"hub"`
}

type Censor struct {
Bucket string `json:"bucket"`
}

type Im struct {
AppId string `json:"app_id"`
}

func LoadConfig(log *logger.Logger, path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open config file %s error %v", path, err)
Expand All @@ -24,6 +60,61 @@ func LoadConfig(path string) (*Config, error) {
if err != nil {
return nil, fmt.Errorf("read config file %s error %v", path, err)
}

ak := v.GetString("account.access_key")
sk := v.GetString("account.secret_key")
hub := v.GetString("pili.hub")
bucket := v.GetString("censor.bucket")
rtc := v.GetString("rtc.app_id")
im := v.GetString("im.app_id")
if ak == "" || sk == "" || hub == "" || rtc == "" || im == "" {
return nil, fmt.Errorf("config yaml not complete,lack of something")
}

req := &QiniuConfigReq{
Rtc: Rtc{AppId: rtc},
Pili: Pili{Hub: hub},
Censor: Censor{Bucket: bucket},
Im: Im{AppId: im},
}
log.Info(req)
mac := &qiniumac.Mac{
AccessKey: ak,
SecretKey: []byte(sk),
}
tr := qiniumac.NewTransport(mac, nil)
httpClient := &http.Client{
Transport: tr,
}

client := &rpc.Client{
Client: httpClient,
}
url := "https://live-admin.qiniu.com" + "/v1/app/config/cache"
ret := &QiniuCinfig{}

fileName := "qiniuConfig"
err = client.CallWithJSON(log, ret, url, req)
var data []byte
if err != nil || ret.Code != 0 {
log.Errorf("request to config server error %v", err)
data, err = ioutil.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("MergeConfig %s error %v", path, err)
}
} else {
data = ret.Data
}

v.SetConfigType("yaml")
err = v.MergeConfig(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("MergeConfig %s error %v", path, err)
}
err = ioutil.WriteFile(fileName, data, 0666)
if err != nil {
log.Errorf("write file fail error %v", err)
}
return &Config{
v,
}, nil
Expand Down
88 changes: 60 additions & 28 deletions core/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,66 @@
node_id: 2

service:
host: 127.0.0.1
port: 8099

cron_config:
single_task_node: 2

jwt_key: jwtkey
httpq:
addr: ":8080"

mysql:
databases:
- host: 127.0.0.1
port: 3306
username: root
password: 123456
max_idle_conns: 10
max_open_conns: 100
database: live
conn_max_life_time: 5
default: live

- host: 127.0.0.1
port: 3306
username: root
password: 123456
max_idle_conns: 10
max_open_conns: 100
database: live
conn_max_life_time: 5
default: live
read_only: true


cache:
type: node
addr: 127.0.0.1:6379

gift:
gift_addr: xxxxxxxxx

callback:
report_host: https://niucube-api.qiniu.com

mysqls:
- host: 127.0.0.1
port: 3306
username: root
password: 123456
max_idle_conns: 10
max_open_conns: 100
database: live
conn_max_life_time: 5
default: live

- host: 127.0.0.1
port: 3306
username: root
password: 123456
max_idle_conns: 10
max_open_conns: 100
database: live
conn_max_life_time: 5
default: live
read_only: true
addr: xxxxxxxxx

auth:
jwt_key: jwtkey
server:
enable: true
access_key: ak
secret_key: sk

account:
access_key: qiniu_ak
secret_key: qiniu_sk


rtc:
app_id: xxxxxxxxx

pili:
hub: xxxxxxxxx
room_token_expire_s: 3600
publish_expire_s: 3700
stream_pattern: qn_live_kit-%s

im:
app_id: xxxxxxxxx

26 changes: 17 additions & 9 deletions core/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package config

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/qbox/livekit/utils/logger"
)

func TestLoadConfig(t *testing.T) {
path := "config.yaml"
c, err := LoadConfig(path)
ctx := context.Background()
log := logger.ReqLogger(ctx)
c, err := LoadConfig(log, path)
assert.Nil(t, err)
assert.NotNil(t, c)
}
Expand All @@ -23,10 +28,12 @@ func TestConfig_Sub(t *testing.T) {
args args
}{
{"", "config.yaml", args{key: "cron_config"}},
{"", "config.yaml", args{key: "service"}},
{"", "config.yaml", args{key: "httpq"}},
}
for _, tt := range tests {
c, _ := LoadConfig(tt.path)
ctx := context.Background()
log := logger.ReqLogger(ctx)
c, _ := LoadConfig(log, tt.path)
t.Run(tt.name, func(t *testing.T) {
assert.NotNil(t, c.Sub(tt.args.key), "Sub(%v)", tt.args.key)
})
Expand All @@ -35,19 +42,20 @@ func TestConfig_Sub(t *testing.T) {

func TestConfig_Unmarshal(t *testing.T) {
path := "config.yaml"
c, _ := LoadConfig(path)
ctx := context.Background()
log := logger.ReqLogger(ctx)
c, _ := LoadConfig(log, path)

sub := c.Sub("service")
sub := c.Sub("httpq")
assert.NotNil(t, sub)

server := Server{}
err := sub.Unmarshal(&server)
assert.Nil(t, err)
assert.Equal(t, "127.0.0.1", server.Host)
assert.Equal(t, 8099, server.Port)

assert.Equal(t, ":8080", server.Addr)
}

type Server struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Addr string `mapstructure:"addr"`
}
1 change: 1 addition & 0 deletions core/config/qiniuConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"x","im":{"app_id":"x","endpoint":"x","token":"x"},"rtc":{"app_id":"x"},"pili":{"hub":"x","room_token_expire_s":3600,"playback_url":"x","stream_pattern":"qn_live_kit-%s","publish_url":"x","publish_domain":"x","rtmp_play_url":"x","flv_play_url":"x","hls_play_url":"x","security_type":"x","publish_key":"x","publish_expire_s":3600},"censor":{"bucket":"x","addr":"x"}}