added transmission webhooks

main
Nilo Roberto C Paim 2025-08-07 17:59:28 -03:00
parent 4f8b9a6a42
commit c36ac25cef
5 changed files with 83 additions and 21 deletions

View File

@ -0,0 +1,51 @@
package controllers
import (
"api/globals"
"github.com/gofiber/fiber/v2"
)
func GetTransmissions(c *fiber.Ctx) error {
return c.JSON(globals.Transmissions)
}
func GetTransmissionByChannel(c *fiber.Ctx) error {
channel := c.Params("channel")
if channel == "" {
return fiber.ErrBadRequest
}
transmission, exists := globals.Transmissions[channel]
if !exists {
return fiber.ErrNotFound
}
return c.JSON(transmission)
}
// func GetGroups(c *fiber.Ctx) error {
// var groups []models.SystemGroup
// globals.DB.Find(&groups)
// return c.JSON(groups)
// }
// func CreateUser(c *fiber.Ctx) error {
// var body map[string]interface{}
// if err := c.BodyParser(&body); err != nil {
// return fiber.ErrBadRequest
// }
// email := body["email"].(string)
// user := services.GetUserByEmail(email)
// if user.ID == 0 {
// fmt.Println("inexistent user")
// }
// return c.JSON(user)
// }

View File

@ -59,6 +59,14 @@ func ServerStart(c *fiber.Ctx) error {
// return c.SendString("On_Update: " + string(c.Body()))
// }
func OnSubStart(c *fiber.Ctx) error {
return nil
}
func OnSubStop(c *fiber.Ctx) error {
return nil
}
func OnPubStart(c *fiber.Ctx) error {
// ================================================================
// Called when a publisher starts streaming - Start of Transmission
@ -72,20 +80,18 @@ func OnPubStart(c *fiber.Ctx) error {
// Parse the URL parameters, in ordere to get the transmission key and the player key
transmissionkey, playerkey := utils.ParseTransmissionString(p.UrlParam)
log.Printf("======================== Start StreamName %s, Transmission Key %s, PlayerKey %s, SessionId %s\n", p.StreamName, transmissionkey, playerkey, p.SessionId)
// log.Printf("======================== Start StreamName %s (%s), Transmission Key %s, PlayerKey %s, SessionId %s\n", p.StreamName, p.RemoteAddress, transmissionkey, playerkey, p.SessionId)
// Get the channel from the database
ok, currentTransmission := services.VerifyTransmissionAuthorization(p.StreamName, p.SessionId, transmissionkey)
ok, currentTransmission := services.VerifyTransmissionAuthorization(p.StreamName, p.SessionId, transmissionkey, playerkey)
log.Printf("%v\n", currentTransmission)
// Check if the channel has authorized time to transmit
if ok {
// If the channel is authorized, add it to the global transmissions list
globals.Transmissions[p.StreamName] = currentTransmission
services.AddTransmissionlog(p.StreamName, "Transmissão iniciada")
return c.SendString("On_Pub_Start: " + string(c.Body()))
}
@ -103,16 +109,27 @@ func OnPubStop(c *fiber.Ctx) error {
transm := globals.Transmissions[p.StreamName]
// TODO: Check if the transmission does not have errors and log only if true (and only once)
now := time.Now()
duration := now.Sub(transm.StartTime)
minutes := float64(duration) / float64(time.Minute)
minutes := int(duration.Minutes())
if minutes > 43200 {
minutes = 0
return c.SendString("On_Pub_Stop: " + string(c.Body()))
}
log.Printf("======================== Stop StreamName %s - Duration %d\n", p.StreamName, duration)
// TODO: Updates the transmission on database for future calculation of remaining daily time
msg := fmt.Sprintf("Transmissão encerrada: Duration %d\n", int(minutes))
msg := fmt.Sprintf("Transmissão encerrada. Duração %d minutos.\n", int(minutes))
services.AddTransmissionlog(p.StreamName, msg)
delete(globals.Transmissions, p.StreamName)
return c.SendString("On_Pub_Stop: " + string(c.Body()))
}

View File

@ -235,6 +235,7 @@ type CurrentTransmission struct {
StartTime time.Time
Limit time.Time
SessionID string
PlayerKey string
PlanDailyLimit int
Watchers int
}

View File

@ -4,7 +4,6 @@ import (
"api/controllers"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
)
// Setup sets up the routes
@ -28,16 +27,10 @@ func Setup(app *fiber.App) {
// app.Post("/on_update", controllers.OnUpdate)
app.Post("/on_pub_start", controllers.OnPubStart)
app.Post("/on_pub_stop", controllers.OnPubStop)
// app.Post("/on_sub_start", controllers.OnSubStart)
app.Post("/on_sub_start", controllers.OnSubStart)
app.Post("/on_sub_stop", controllers.OnSubStop)
// WebSocket route
app.Use("/ws", func(c *fiber.Ctx) error {
if websocket.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
})
// app.Get("/ws", websocket.New(controllers.WebsocketHandler))
// Interfaces of transmissions
app.Get("/transmissions", controllers.GetTransmissions)
app.Get("/transmission/:channel", controllers.GetTransmissionByChannel)
}

View File

@ -4,12 +4,11 @@ import (
"api/globals"
"api/models"
"api/utils"
"log"
"time"
)
func VerifyTransmissionsLimits() {
log.Println("Verificando")
// log.Println("Verificando")
for channelname, currentTransmission := range globals.Transmissions {
// If the channel has no daily transmission limit, skip the verification (Channels with 24 hours daily limit)
if time.Duration(currentTransmission.PlanDailyLimit) == 1440 {
@ -26,7 +25,7 @@ func VerifyTransmissionsLimits() {
}
}
func VerifyTransmissionAuthorization(channelname, sessionid, transmissionkey string) (bool, models.CurrentTransmission) {
func VerifyTransmissionAuthorization(channelname, sessionid, transmissionkey, playerKey string) (bool, models.CurrentTransmission) {
var (
channel models.Channel
plan models.Plan
@ -39,6 +38,7 @@ func VerifyTransmissionAuthorization(channelname, sessionid, transmissionkey str
currentTransmission.Channel = channel.Name
currentTransmission.SessionID = sessionid
currentTransmission.PlayerKey = playerKey
currentTransmission.PlanDailyLimit = plan.DailyLimitTransmission
currentTransmission.StartTime = time.Now()