Skip to content

Commit b464789

Browse files
committed
Makefile, log system, errors to fmt
1 parent a15aa45 commit b464789

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+499
-425
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ dist
44
.vscode
55
.tmp
66
vendor
7+
livego

Diff for: AG

Whitespace-only changes.

Diff for: CHANGELOG.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
{
1414
"jwt": {
1515
"secret": "testing",
16-
"algorithm": "HS256s"
16+
"algorithm": "HS256"
1717
},
1818
"server": [
1919
{
2020
"appname": "live",
21-
"liveon": "on",
22-
"hlson": "on"
21+
"live": true,
22+
"hls": true
2323
}
2424
]
2525
}
@@ -32,14 +32,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
"server": [
3333
{
3434
"appname": "live",
35-
"liveon": "on",
36-
"hlson": "on"
35+
"live": true,
36+
"hls": true
3737
}
3838
]
3939
}
4040
```
41+
- Makefile
4142

4243
### Changed
4344
- Show `players`.
4445
- Show `stream_id`.
4546
- Deleted keys saved in physical file, now the keys are in cached using `go-cache` by default.
47+
- Using `logrus` like log system.
48+
- Using method `.Get(queryParamName)` to get an url query param.
49+
- Replaced `errors.New(...)` to `fmt.Errorf(...)`.
50+
- Replaced types string on config params `liveon` and `hlson` to booleans `live: true/false` and `hls: true/false`

Diff for: Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
GOCMD ?= go
2+
GOBUILD = $(GOCMD) build
3+
GOCLEAN = $(GOCMD) clean
4+
GOTEST = $(GOCMD) test
5+
GOGET = $(GOCMD) get
6+
BINARY_NAME = livego
7+
BINARY_UNIX = $(BINARY_NAME)_unix
8+
9+
DOCKER_ACC ?= gwuhaolin
10+
DOCKER_REPO ?= livego
11+
12+
TAG := $(shell git describe --tags --abbrev=0 2>/dev/null)
13+
14+
default: all
15+
16+
all: test build dockerize
17+
build:
18+
$(GOBUILD) -o $(BINARY_NAME) -v -ldflags="-X main.VERSION=$(TAG)"
19+
20+
test:
21+
$(GOTEST) -v ./...
22+
23+
clean:
24+
$(GOCLEAN)
25+
rm -f $(BINARY_NAME)
26+
rm -f $(BINARY_UNIX)
27+
28+
run: build
29+
./$(BINARY_NAME)
30+
31+
build-linux:
32+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_UNIX) -v
33+
34+
dockerize:
35+
docker build -t $(DOCKER_ACC)/$(DOCKER_REPO):$(TAG) .
36+
docker push $(DOCKER_ACC)/$(DOCKER_REPO):$(TAG)

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Run `docker run -p 1935:1935 -p 7001:7001 -p 7002:7002 -d --name livego gwuhaoli
3030

3131
#### Compile from source
3232
1. Download the source code `git clone https://github.com/gwuhaolin/livego.git`
33-
2. Go to the livego directory and execute `go build`
33+
2. Go to the livego directory and execute `go build` or `make run`
3434

3535
## Use
3636
2. Start the service: execute the livego binary file to start the livego service;

Diff for: configure/channel.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@ package configure
22

33
import (
44
"fmt"
5-
"log"
65

76
"livego/utils/uid"
87

98
"github.com/go-redis/redis/v7"
109
"github.com/patrickmn/go-cache"
10+
log "github.com/sirupsen/logrus"
1111
)
1212

13-
var RoomKeys *RoomKeysType
14-
var saveInLocal = true
15-
1613
type RoomKeysType struct {
1714
redisCli *redis.Client
1815
localCache *cache.Cache
1916
}
2017

21-
func Init() {
22-
saveInLocal = GetRedisAddr() == nil
18+
var RoomKeys = &RoomKeysType{
19+
localCache: cache.New(cache.NoExpiration, 0),
20+
}
2321

24-
RoomKeys = &RoomKeysType{
25-
localCache: cache.New(cache.NoExpiration, 0),
26-
}
22+
var saveInLocal = true
2723

24+
func Init() {
25+
saveInLocal = GetRedisAddr() == nil
2826
if saveInLocal {
2927
return
3028
}
@@ -37,10 +35,10 @@ func Init() {
3735

3836
_, err := RoomKeys.redisCli.Ping().Result()
3937
if err != nil {
40-
panic(err)
38+
log.Panic("Redis: ", err)
4139
}
4240

43-
log.Printf("Redis connected")
41+
log.Debug("Redis connected")
4442
}
4543

4644
// set/reset a random key for channel
@@ -77,7 +75,7 @@ func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
7775
if !saveInLocal {
7876
if newKey, err = r.redisCli.Get(channel).Result(); err == redis.Nil {
7977
newKey, err = r.SetKey(channel)
80-
log.Printf("[KEY] new channel [%s]: %s", channel, newKey)
78+
log.Debugf("[KEY] new channel [%s]: %s", channel, newKey)
8179
return
8280
}
8381

@@ -90,7 +88,7 @@ func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
9088
return key.(string), nil
9189
}
9290
newKey, err = r.SetKey(channel)
93-
log.Printf("[KEY] new channel [%s]: %s", channel, newKey)
91+
log.Debugf("[KEY] new channel [%s]: %s", channel, newKey)
9492
return
9593
}
9694

Diff for: configure/liveconfig.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
"encoding/json"
55
"flag"
66
"io/ioutil"
7-
"log"
7+
8+
log "github.com/sirupsen/logrus"
89
)
910

1011
/*
1112
{
1213
"server": [
1314
{
1415
"appname": "live",
15-
"liveon": "on",
16-
"hlson": "on",
16+
"live": true,
17+
"hls": true,
1718
"static_push": []
1819
}
1920
]
@@ -26,8 +27,8 @@ var (
2627

2728
type Application struct {
2829
Appname string `json:"appname"`
29-
Liveon string `json:"liveon"`
30-
Hlson string `json:"hlson"`
30+
Live bool `json:"liveon"`
31+
Hls bool `json:"hls"`
3132
StaticPush []string `json:"static_push"`
3233
}
3334
type JWTCfg struct {
@@ -45,26 +46,29 @@ type ServerCfg struct {
4546
var RtmpServercfg = ServerCfg{
4647
Server: []Application{{
4748
Appname: "livego",
48-
Liveon: "on",
49-
Hlson: "on",
49+
Live: true,
50+
Hls: true,
5051
StaticPush: nil,
5152
}},
5253
}
5354

5455
func LoadConfig(configfilename string) {
55-
log.Printf("starting load configure file %s", configfilename)
56+
defer Init()
57+
58+
log.Infof("starting load configure file %s", configfilename)
5659
data, err := ioutil.ReadFile(configfilename)
5760
if err != nil {
58-
log.Printf("ReadFile %s error:%v", configfilename, err)
61+
log.Warningf("ReadFile %s error:%v", configfilename, err)
62+
log.Info("Using default config")
63+
return
5964
}
6065

6166
err = json.Unmarshal(data, &RtmpServercfg)
6267
if err != nil {
63-
log.Printf("json.Unmarshal error:%v", err)
68+
log.Errorf("json.Unmarshal error:%v", err)
69+
log.Info("Using default config")
6470
}
65-
log.Printf("get config json data:%v", RtmpServercfg)
66-
67-
Init()
71+
log.Debugf("get config json data:%v", RtmpServercfg)
6872
}
6973

7074
func GetRedisAddr() *string {
@@ -89,16 +93,16 @@ func GetRedisPwd() *string {
8993

9094
func CheckAppName(appname string) bool {
9195
for _, app := range RtmpServercfg.Server {
92-
if (app.Appname == appname) && (app.Liveon == "on") {
93-
return true
96+
if app.Appname == appname {
97+
return app.Live
9498
}
9599
}
96100
return false
97101
}
98102

99103
func GetStaticPushUrlList(appname string) ([]string, bool) {
100104
for _, app := range RtmpServercfg.Server {
101-
if (app.Appname == appname) && (app.Liveon == "on") {
105+
if (app.Appname == appname) && app.Live {
102106
if len(app.StaticPush) > 0 {
103107
return app.StaticPush, true
104108
} else {

Diff for: container/flv/demuxer.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package flv
22

33
import (
4-
"errors"
5-
4+
"fmt"
65
"livego/av"
76
)
87

98
var (
10-
ErrAvcEndSEQ = errors.New("avc end sequence")
9+
ErrAvcEndSEQ = fmt.Errorf("avc end sequence")
1110
)
1211

1312
type Demuxer struct {

Diff for: container/flv/muxer.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package flv
33
import (
44
"flag"
55
"fmt"
6-
"log"
76
"os"
87
"path"
98
"strings"
@@ -13,6 +12,8 @@ import (
1312
"livego/protocol/amf"
1413
"livego/utils/pio"
1514
"livego/utils/uid"
15+
16+
log "github.com/sirupsen/logrus"
1617
)
1718

1819
var (
@@ -25,13 +26,13 @@ func NewFlv(handler av.Handler, info av.Info) {
2526
patths := strings.SplitN(info.Key, "/", 2)
2627
2728
if len(patths) != 2 {
28-
log.Println("invalid info")
29+
log.Warning("invalid info")
2930
return
3031
}
3132
3233
w, err := os.OpenFile(*flvFile, os.O_CREATE|os.O_RDWR, 0755)
3334
if err != nil {
34-
log.Println("open file error: ", err)
35+
log.Error("open file error: ", err)
3536
}
3637
3738
writer := NewFLVWriter(patths[0], patths[1], info.URL, w)
@@ -40,7 +41,7 @@ func NewFlv(handler av.Handler, info av.Info) {
4041
4142
writer.Wait()
4243
// close flv file
43-
log.Println("close flv file")
44+
log.Debug("close flv file")
4445
writer.ctx.Close()
4546
}
4647
*/
@@ -147,25 +148,25 @@ type FlvDvr struct{}
147148
func (f *FlvDvr) GetWriter(info av.Info) av.WriteCloser {
148149
paths := strings.SplitN(info.Key, "/", 2)
149150
if len(paths) != 2 {
150-
log.Println("invalid info")
151+
log.Warning("invalid info")
151152
return nil
152153
}
153154

154155
err := os.MkdirAll(path.Join(*flvDir, paths[0]), 0755)
155156
if err != nil {
156-
log.Println("mkdir error:", err)
157+
log.Error("mkdir error: ", err)
157158
return nil
158159
}
159160

160161
fileName := fmt.Sprintf("%s_%d.%s", path.Join(*flvDir, info.Key), time.Now().Unix(), "flv")
161-
log.Println("flv dvr save stream to: ", fileName)
162+
log.Debug("flv dvr save stream to: ", fileName)
162163
w, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0755)
163164
if err != nil {
164-
log.Println("open file error: ", err)
165+
log.Error("open file error: ", err)
165166
return nil
166167
}
167168

168169
writer := NewFLVWriter(paths[0], paths[1], info.URL, w)
169-
log.Println("new flv dvr: ", writer.Info())
170+
log.Debug("new flv dvr: ", writer.Info())
170171
return writer
171172
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
1111
github.com/patrickmn/go-cache v2.1.0+incompatible
1212
github.com/satori/go.uuid v1.2.0
13+
github.com/sirupsen/logrus v1.5.0
1314
github.com/smartystreets/goconvey v1.6.4 // indirect
1415
github.com/stretchr/testify v1.4.0
1516
github.com/urfave/negroni v1.0.0 // indirect

Diff for: go.sum

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b h1:CvoEHGmxWl5kONC5icxwqV899dkf4VjOScbxLpllEnw=
22
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b/go.mod h1:LWMyo4iOLWXHGdBki7NIht1kHru/0wM179h+d3g8ATM=
3-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
43
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
67
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
78
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
@@ -19,6 +20,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
1920
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
2021
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
2122
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
23+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
24+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
2225
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
2326
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
2427
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -37,11 +40,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3740
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3841
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
3942
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
43+
github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
44+
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
4045
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
4146
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
4247
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
4348
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
4449
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
50+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4551
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
4652
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
4753
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
@@ -54,6 +60,7 @@ golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLL
5460
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5561
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5662
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
63+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5764
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
5865
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5966
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)