Browse Source

Merge branch 'master' into webui

webui
Ruben Cid 5 years ago
parent
commit
20b5415d28
  1. 157
      protocol/httpopera/http_opera.go

157
protocol/httpopera/http_opera.go

@ -3,7 +3,6 @@ package httpopera @@ -3,7 +3,6 @@ package httpopera
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
@ -19,15 +18,15 @@ import ( @@ -19,15 +18,15 @@ import (
)
type Response struct {
w http.ResponseWriter
Status int `json:"status"`
Message string `json:"message"`
w http.ResponseWriter
Status int `json:"status"`
Data interface{} `json:"data"`
}
func (r *Response) SendJson() (int, error) {
resp, _ := json.Marshal(r)
r.w.WriteHeader(r.Status)
r.w.Header().Set("Content-Type", "application/json")
r.w.WriteHeader(r.Status)
return r.w.Write(resp)
}
@ -136,9 +135,18 @@ type streams struct { @@ -136,9 +135,18 @@ type streams struct {
//http://127.0.0.1:8090/stat/livestat
func (server *Server) GetLiveStatics(w http.ResponseWriter, req *http.Request) {
res := &Response{
w: w,
Data: nil,
Status: 200,
}
defer res.SendJson()
rtmpStream := server.handler.(*rtmp.RtmpStream)
if rtmpStream == nil {
io.WriteString(w, "<h1>Get rtmp stream information error</h1>")
res.Status = 500
res.Data = "Get rtmp stream information error"
return
}
@ -173,9 +181,9 @@ func (server *Server) GetLiveStatics(w http.ResponseWriter, req *http.Request) { @@ -173,9 +181,9 @@ func (server *Server) GetLiveStatics(w http.ResponseWriter, req *http.Request) {
}
}
}
resp, _ := json.Marshal(msgs)
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
res.Data = resp
}
//http://127.0.0.1:8090/control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456
@ -183,8 +191,17 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { @@ -183,8 +191,17 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) {
var retString string
var err error
res := &Response{
w: w,
Data: nil,
Status: 200,
}
defer res.SendJson()
if req.ParseForm() != nil {
fmt.Fprintf(w, "url: /control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456")
res.Status = 400
res.Data = "url: /control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456"
return
}
@ -195,7 +212,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { @@ -195,7 +212,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) {
log.Printf("control pull: oper=%v, app=%v, name=%v, url=%v", oper, app, name, url)
if (len(app) <= 0) || (len(name) <= 0) || (len(url) <= 0) {
io.WriteString(w, "control push parameter error, please check them.</br>")
res.Status = 400
res.Data = "control push parameter error, please check them."
return
}
@ -208,7 +226,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { @@ -208,7 +226,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) {
if !found {
retString = fmt.Sprintf("session key[%s] not exist, please check it again.", keyString)
io.WriteString(w, retString)
res.Status = 400
res.Data = retString
return
}
log.Printf("rtmprelay stop push %s from %s", remoteurl, localurl)
@ -216,7 +235,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { @@ -216,7 +235,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) {
delete(s.session, keyString)
retString = fmt.Sprintf("<h1>push url stop %s ok</h1></br>", url[0])
io.WriteString(w, retString)
res.Status = 400
res.Data = retString
log.Printf("pull stop return %s", retString)
} else {
pullRtmprelay := rtmprelay.NewRtmpRelay(&localurl, &remoteurl)
@ -228,7 +248,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { @@ -228,7 +248,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) {
s.session[keyString] = pullRtmprelay
retString = fmt.Sprintf("<h1>push url start %s ok</h1></br>", url[0])
}
io.WriteString(w, retString)
res.Status = 400
res.Data = retString
log.Printf("pull start return %s", retString)
}
}
@ -238,8 +259,16 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { @@ -238,8 +259,16 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) {
var retString string
var err error
res := &Response{
w: w,
Data: nil,
Status: 200,
}
defer res.SendJson()
if req.ParseForm() != nil {
fmt.Fprintf(w, "url: /control/push?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456")
res.Data = "url: /control/push?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456"
return
}
@ -250,7 +279,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { @@ -250,7 +279,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) {
log.Printf("control push: oper=%v, app=%v, name=%v, url=%v", oper, app, name, url)
if (len(app) <= 0) || (len(name) <= 0) || (len(url) <= 0) {
io.WriteString(w, "control push parameter error, please check them.</br>")
res.Data = "control push parameter error, please check them."
return
}
@ -262,7 +291,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { @@ -262,7 +291,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) {
pushRtmprelay, found := s.session[keyString]
if !found {
retString = fmt.Sprintf("<h1>session key[%s] not exist, please check it again.</h1>", keyString)
io.WriteString(w, retString)
res.Data = retString
return
}
log.Printf("rtmprelay stop push %s from %s", remoteurl, localurl)
@ -270,7 +299,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { @@ -270,7 +299,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) {
delete(s.session, keyString)
retString = fmt.Sprintf("<h1>push url stop %s ok</h1></br>", url[0])
io.WriteString(w, retString)
res.Data = retString
log.Printf("push stop return %s", retString)
} else {
pushRtmprelay := rtmprelay.NewRtmpRelay(&localurl, &remoteurl)
@ -283,69 +312,101 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { @@ -283,69 +312,101 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) {
s.session[keyString] = pushRtmprelay
}
io.WriteString(w, retString)
res.Data = retString
log.Printf("push start return %s", retString)
}
}
//http://127.0.0.1:8090/control/reset?room=ROOM_NAME
func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
if r.ParseForm() != nil {
fmt.Fprintf(w, "url: /control/reset?room=ROOM_NAME")
res := &Response{
w: w,
Data: nil,
Status: 200,
}
defer res.SendJson()
if err := r.ParseForm(); err != nil {
res.Status = 400
res.Data = "url: /control/reset?room=ROOM_NAME"
return
}
room := r.Form.Get("room")
if len(room) == 0 {
res.Status = 400
res.Data = "url: /control/get?room=ROOM_NAME"
return
}
room := r.Form["room"][0]
status := 200
msg, err := configure.RoomKeys.SetKey(room)
if err != nil {
msg = err.Error()
status = 400
res.Status = 400
}
res := &Response{
w: w,
Message: msg,
Status: status,
}
res.SendJson()
res.Data = msg
}
//http://127.0.0.1:8090/control/get?room=ROOM_NAME
func (s *Server) handleGet(w http.ResponseWriter, r *http.Request) {
if r.ParseForm() != nil {
fmt.Fprintf(w, "url: /control/get?room=ROOM_NAME")
res := &Response{
w: w,
Data: nil,
Status: 200,
}
defer res.SendJson()
if err := r.ParseForm(); err != nil {
res.Status = 400
res.Data = "url: /control/get?room=ROOM_NAME"
return
}
room := r.Form["room"][0]
status := 200
msg, err := configure.RoomKeys.GetKey(room)
room := r.Form.Get("room")
if err != nil {
msg = err.Error()
status = 400
if len(room) == 0 {
res.Status = 400
res.Data = "url: /control/get?room=ROOM_NAME"
return
}
res := &Response{
w: w,
Message: msg,
Status: status,
msg, err := configure.RoomKeys.GetKey(room)
if err != nil {
msg = err.Error()
res.Status = 400
}
res.SendJson()
res.Data = msg
}
//http://127.0.0.1:8090/control/delete?room=ROOM_NAME
func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) {
if r.ParseForm() != nil {
fmt.Fprintf(w, "url: /control/delete?room=ROOM_NAME")
res := &Response{
w: w,
Data: nil,
Status: 200,
}
defer res.SendJson()
if err := r.ParseForm(); err != nil {
res.Status = 400
res.Data = "url: /control/delete?room=ROOM_NAME"
return
}
room := r.Form["room"][0]
room := r.Form.Get("room")
if len(room) == 0 {
res.Status = 400
res.Data = "url: /control/get?room=ROOM_NAME"
return
}
if configure.RoomKeys.DeleteChannel(room) {
fmt.Fprintf(w, "OK")
} else {
fmt.Fprintf(w, "Room Not Found")
res.Data = "Ok"
return
}
res.Status = 404
res.Data = "Room not found"
}

Loading…
Cancel
Save