diff --git a/api.exe~ b/api.exe~ new file mode 100644 index 0000000..7d95bad Binary files /dev/null and b/api.exe~ differ diff --git a/controllers/authController.go b/controllers/authController.go index a2aa3e0..ca90fcd 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -146,3 +146,15 @@ func GetAllUsers(c *fiber.Ctx) error { return c.JSON(users) } + +func WixIntegration(c *fiber.Ctx) error { + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return fiber.ErrBadRequest + } + + utils.PrettyPrintJson(data) + + return c.JSON(data) +} diff --git a/controllers/webhookController.go b/controllers/webhookController.go index 93dca44..ce0445c 100644 --- a/controllers/webhookController.go +++ b/controllers/webhookController.go @@ -1,75 +1,70 @@ package controllers import ( - "fmt" + "api/models" + "api/utils" "log" "github.com/gofiber/fiber/v2" ) -type Pub struct { - Server string `json:"server_id"` - Protocol string `json:"protocol"` - Url string `json:"url"` - AppName string `json:"app_name"` - Channel string `json:"stream_name"` - UrlParam string `json:"url_param"` - RemoteAddress string `json:"remotet_addr"` - HasInSession bool `json:"has_in_session"` - HasOutSession bool `json:"has_out_session"` -} - -type Update struct { - Server string `json:"server_id"` - Groups []Group `json:"groups"` -} - -type Group struct { - Channel string `json:"stream_name"` - AudioCodec string `json:"audio_codec"` - VideoCodec string `json:"video_codec"` - VideoWidth int `json:"video_width"` - VideoHeight int `json:"video_height"` - UpdPub UpdPub `json:"pub"` - Subs string `json:"subs"` - UpdPull UpdPub `json:"pull"` -} - -type UpdPub struct { - Protocol string `json:"protocol"` - SessionId string `json:"session_id"` - RemoteAddress string `json:"remote_addr"` - StartTime string `json:"start_time"` - ReadBytesSum int `json:"read_bytes_sum"` - WroteBytesSum int `json:"wrote_bytes_sum"` - Bitrate int `json:"bitrate"` - ReadBitrate int `json:"read_bitrate"` - WriteBitrate int `json:"write_bitrate"` -} - func ServerStart(c *fiber.Ctx) error { - fmt.Println("Server started") - log.Println(string(c.Body())) + // 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 OnUpdate(c *fiber.Ctx) error { - p := new(Update) + 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 Update: %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 { - p := new(Pub) + p := new(models.Pub) if err := c.BodyParser(p); err != nil { return err } @@ -78,7 +73,7 @@ func OnPubStart(c *fiber.Ctx) error { } func OnPubStop(c *fiber.Ctx) error { - p := new(Pub) + p := new(models.Pub) if err := c.BodyParser(p); err != nil { return err } diff --git a/globals/globals.go b/globals/globals.go index 37b1849..8aa09e0 100644 --- a/globals/globals.go +++ b/globals/globals.go @@ -1,6 +1,6 @@ package globals var ( - API_VERSION = "1.0.18" + API_VERSION = "1.0.20" API_RELEASE = "" ) diff --git a/index.html b/index.html new file mode 100644 index 0000000..86cf439 --- /dev/null +++ b/index.html @@ -0,0 +1,30 @@ + + + + + HLS.js Example + + + + + + + + + \ No newline at end of file diff --git a/infra/Caddyfile b/infra/Caddyfile index eb056c2..1daa984 100644 --- a/infra/Caddyfile +++ b/infra/Caddyfile @@ -6,12 +6,3 @@ http://www.pcastt.com.br, http://pcastt.com.br, https://www.pcastt.com.br, https reverse_proxy localhost:8111 } } - -http://www.pcastlive.com.br, http://pcastlive.com.br, https://www.pcastlive.com.br, https://pcastlive.com.br { - root * /root/sites/pcastlive - file_server - - handle_path /api/* { - reverse_proxy localhost:8112 - } -} diff --git a/infra/newapi.sh b/infra/newapi.sh index 709f37b..46bd5b8 100644 --- a/infra/newapi.sh +++ b/infra/newapi.sh @@ -1,4 +1,4 @@ service api stop cp /home/ftpuser/ftp/files/api /root/api -chmod 777 /root//api/api +chmod 777 /root/api/api service api start diff --git a/main.go b/main.go index 37a65ef..62c6af0 100644 --- a/main.go +++ b/main.go @@ -14,17 +14,17 @@ import ( ) func main() { - logFile, err := os.OpenFile("api.log", os.O_CREATE | os.O_APPEND | os.O_RDWR, 0666) + logFile, err := os.OpenFile("api.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) if err != nil { - panic(err) + panic(err) } mw := io.MultiWriter(os.Stdout, logFile) log.SetOutput(mw) log.Println("Starting API", globals.API_VERSION) - log.Println("OS:", os.Getenv("OS")) - + log.Println("OS:", os.Getenv("OS")) + app := fiber.New(fiber.Config{ StrictRouting: false, DisableStartupMessage: true, @@ -42,7 +42,7 @@ func main() { if err != nil { log.Fatalf("error opening file: %v", err) } - + defer file.Close() app.Use(logger.New(logger.Config{ @@ -56,5 +56,7 @@ func main() { routes.Setup(app) log.Println("Server started in port " + os.Getenv("API_PORT")) - app.Listen(":" + os.Getenv("API_PORT")) + if erro := app.Listen(":" + os.Getenv("API_PORT")); err != nil { + panic(erro) + } } diff --git a/models/webhooks.go b/models/webhooks.go new file mode 100644 index 0000000..e43bc70 --- /dev/null +++ b/models/webhooks.go @@ -0,0 +1,51 @@ +package models + +type LalServer struct { + Server string `json:"server_id"` + BinInfo string `json:"-"` + LalVersion string `json:"lal_version"` + APIVersion string `json:"-"` + NotifyVersion string `json:"-"` + WebUIVersion string `json:"-"` + StartTime string `json:"start_time"` +} + +type Pub struct { + Server string `json:"server_id"` + Protocol string `json:"protocol"` + Url string `json:"url"` + AppName string `json:"app_name"` + Channel string `json:"stream_name"` + UrlParam string `json:"url_param"` + RemoteAddress string `json:"remotet_addr"` + HasInSession bool `json:"has_in_session"` + HasOutSession bool `json:"has_out_session"` +} + +type Update struct { + LalServer string `json:"server_id"` + Groups []Group `json:"groups"` +} + +type Group struct { + Channel string `json:"stream_name"` + AudioCodec string `json:"audio_codec"` + VideoCodec string `json:"video_codec"` + VideoWidth int `json:"video_width"` + VideoHeight int `json:"video_height"` + UpdPub UpdPub `json:"pub"` + Subs string `json:"subs"` + UpdPull UpdPub `json:"pull"` +} + +type UpdPub struct { + Protocol string `json:"protocol"` + SessionId string `json:"session_id"` + RemoteAddress string `json:"remote_addr"` + StartTime string `json:"start_time"` + ReadBytesSum int `json:"read_bytes_sum"` + WroteBytesSum int `json:"wrote_bytes_sum"` + Bitrate int `json:"bitrate"` + ReadBitrate int `json:"read_bitrate"` + WriteBitrate int `json:"write_bitrate"` +} diff --git a/routes/routes.go b/routes/routes.go index 7121172..3d644c4 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -12,6 +12,8 @@ import ( // Setup sets up the routes func Setup(app *fiber.App) { + app.Post("/integration", controllers.WixIntegration) + app.Get("/version", controllers.Version) app.Post("/login", controllers.Login) @@ -23,6 +25,7 @@ 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) // Protected routes. Needs login before. protected := app.Group("/") diff --git a/utils/utils.go b/utils/utils.go index 8fe1f87..dd02f79 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,7 +1,9 @@ package utils import ( + "encoding/json" "fmt" + "log" "os" "strings" @@ -61,3 +63,11 @@ func ProcessToken(c *fiber.Ctx) (interface{}, error) { func jwtKeyFunc(token *jwt.Token) (interface{}, error) { return []byte(os.Getenv("API_SECRET")), nil } + +func PrettyPrintJson(data interface{}) { + b, err := json.MarshalIndent(data, "", " ") + if err != nil { + fmt.Println("error:", err) + } + log.Println(string(b)) +}