182 lines
4.9 KiB
Go
182 lines
4.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"api/models"
|
|
"api/utils"
|
|
"bytes"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func KickSession(stream_name, session_id string) {
|
|
url := "http://localhost:8083/api/ctrl/kick_session"
|
|
|
|
values := map[string]string{"stream_name": stream_name, "session_id": session_id}
|
|
|
|
jsonValue, err := json.Marshal(values)
|
|
if err != nil {
|
|
log.Printf("Error Marshall KickSession: %s\n", err)
|
|
}
|
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonValue))
|
|
if err != nil {
|
|
log.Printf("Error Post KickSession: %s\n", err)
|
|
}
|
|
|
|
log.Printf("KickSession status code: %d\n", resp.StatusCode)
|
|
}
|
|
|
|
func ServerStart(c *fiber.Ctx) error {
|
|
// Creates a server struct
|
|
s := new(models.LalServer)
|
|
|
|
// Parse the body of the request to the server struct
|
|
if err := c.BodyParser(s); err != nil {
|
|
log.Printf("Error Start: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
log.Printf("Server started")
|
|
|
|
// Marshal the server struct to JSON
|
|
utils.PrettyPrintJson(s)
|
|
return c.SendString("Server started: " + string(c.Body()))
|
|
}
|
|
|
|
func OnRtmpConnect(c *fiber.Ctx) error {
|
|
|
|
body := new(models.Connect)
|
|
|
|
if err := c.BodyParser(body); err != nil {
|
|
log.Printf("Error Start: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
utils.PrettyPrintJson(body)
|
|
return c.SendString("On_Rtmp_Connect: " + string(c.Body()))
|
|
}
|
|
|
|
func OnUpdate(c *fiber.Ctx) error {
|
|
p := new(models.Update)
|
|
if err := c.BodyParser(p); err != nil {
|
|
log.Printf("Error Update: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
log.Printf("======================== Update")
|
|
utils.PrettyPrintJson(p)
|
|
|
|
if len(p.Groups) > 0 {
|
|
for _, g := range p.Groups {
|
|
log.Printf("======================== Update %s %s [(%dx%d) %d]\n", g.Channel, g.UpdPub.StartTime, g.VideoWidth, g.VideoHeight, g.UpdPub.ReadBytesSum)
|
|
}
|
|
}
|
|
|
|
return c.SendString("On_Update: " + string(c.Body()))
|
|
}
|
|
func OnSubStart(c *fiber.Ctx) error {
|
|
p := new(models.Update)
|
|
if err := c.BodyParser(p); err != nil {
|
|
log.Printf("Error SubStart: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
log.Printf("SubStart")
|
|
utils.PrettyPrintJson(p)
|
|
|
|
// if len(p.Groups) > 0 {
|
|
// for _, g := range p.Groups {
|
|
// log.Printf("Update %s %s [(%dx%d) %d]\n", g.Channel, g.UpdPub.StartTime, g.VideoWidth, g.VideoHeight, g.UpdPub.ReadBytesSum)
|
|
// }
|
|
// }
|
|
|
|
return c.SendString("On_Substart: " + string(c.Body()))
|
|
}
|
|
|
|
func OnPubStart(c *fiber.Ctx) error {
|
|
// ================================================================
|
|
// Called when a publisher starts streaming - Start of Transmission
|
|
// ================================================================
|
|
p := new(models.Pub)
|
|
if err := c.BodyParser(p); err != nil {
|
|
log.Printf("Error PubStart: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
log.Printf("======================== Start StreamName %s, UrlParam %s, SessionId %s\n", p.StreamName, p.UrlParam, p.SessionId)
|
|
|
|
// TODO: Verify if the key is correct. If not, Kick the Session.
|
|
// KickSession(p.StreamName, p.SessionId)
|
|
// return fiber.ErrForbidden
|
|
|
|
return c.SendString("On_Pub_Start: " + string(c.Body()))
|
|
}
|
|
|
|
func OnPubStop(c *fiber.Ctx) error {
|
|
// =============================================================
|
|
// Called when a publisher stops streaming - End of Transmission
|
|
// =============================================================
|
|
p := new(models.Pub)
|
|
if err := c.BodyParser(p); err != nil {
|
|
return err
|
|
}
|
|
log.Printf("======================== Stop Channel %s\n", p.StreamName)
|
|
return c.SendString("On_Pub_Stop: " + string(c.Body()))
|
|
}
|
|
|
|
// func WixIntegration(c *fiber.Ctx) error {
|
|
// var data models.Purchase
|
|
|
|
// if err := c.BodyParser(&data); err != nil {
|
|
// return fiber.ErrBadRequest
|
|
// }
|
|
|
|
// fmt.Println(data.Data.Contact.Email)
|
|
// fmt.Println(data.Data.Contact.Name.First + " " + data.Data.Contact.Name.Last)
|
|
// fmt.Printf("%s", data.Data.PlanOrderID)
|
|
// fmt.Println(data.Data.PlanPrice.Value)
|
|
// fmt.Println(data.Data.PlanStartDate)
|
|
// fmt.Println(data.Data.PlanValidUntil)
|
|
// fmt.Println(data.Data.PlanTitle)
|
|
|
|
// var user models.User
|
|
|
|
// result := database.DB.Debug().Where("email = ?", data.Data.Contact.Email).First(&user)
|
|
|
|
// if result.RowsAffected == 0 {
|
|
// // User does not exist. Inserts it.
|
|
// user := models.User{
|
|
// Email: data.Data.Contact.Email,
|
|
// Name: data.Data.Contact.Name.First + " " + data.Data.Contact.Name.Last,
|
|
// Blocked: "N",
|
|
// Cancelled: "N",
|
|
// }
|
|
|
|
// database.DB.Debug().Create(&user)
|
|
// }
|
|
|
|
// value, err := strconv.ParseFloat(data.Data.PlanPrice.Value, 32)
|
|
|
|
// if err != nil {
|
|
// log.Printf("Error converting plan price: %s\n", err)
|
|
// return fiber.ErrBadRequest
|
|
// }
|
|
|
|
// transaction := models.Transaction{
|
|
// Email: data.Data.Contact.Email,
|
|
// PlanOrderID: data.Data.PlanOrderID,
|
|
// Value: value,
|
|
// PlanStartDate: data.Data.PlanStartDate,
|
|
// PlanValidUntil: data.Data.PlanValidUntil,
|
|
// PlanTitle: data.Data.PlanTitle,
|
|
// Operation: "Compra",
|
|
// Obs: "",
|
|
// }
|
|
|
|
// database.DB.Debug().Create(&transaction)
|
|
|
|
// return c.JSON(transaction)
|
|
// }
|