150 lines
3.8 KiB
Go
150 lines
3.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"api/database"
|
|
"api/models"
|
|
"api/utils"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
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 {
|
|
utils.PrettyPrintJson(c.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 {
|
|
return err
|
|
}
|
|
log.Printf("Start channel %s, urlParam %s, AppName %s\n", p.Channel, p.UrlParam, p.AppName)
|
|
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 %s\n", p.Channel)
|
|
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(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)
|
|
}
|