| to be tested

This commit is contained in:
Miki 2024-01-11 12:12:25 +00:00
parent 10c82104d2
commit aebbdccd03
13 changed files with 509 additions and 385 deletions

BIN
backend.light/backend.light Executable file

Binary file not shown.

View file

@ -67,12 +67,3 @@ func (app *App) RegisterWebRoutes() {
app.Engine.ForwardedByClientIP = true
app.Engine.SetTrustedProxies([]string{"127.0.0.1"})
}
func (app *App) RegisterApiRoutes() {
api := app.Engine.Group("/api")
api.GET("/sse", app.Stream.SseHeadersMiddleware(), app.Stream.Stream)
api.GET("/priority", app.GetPriority)
api.POST("/priority", app.SetPriority)
}

50
backend.light/http_api.go Normal file
View file

@ -0,0 +1,50 @@
package main
import (
"bytes"
"io"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func (app *App) RegisterApiRoutes() {
api := app.Engine.Group("/api")
api.GET("/sse", app.Stream.SseHeadersMiddleware(), app.Stream.Stream)
api.GET("/priority", app.GetPriority)
api.POST("/priority", app.SetPriority)
}
func (app *App) GetPriority(c *gin.Context) {
c.JSON(http.StatusOK, app.Stream.Data)
log.Printf("priority: %+v", app.Stream.Data)
}
func (app *App) SetPriority(c *gin.Context) {
var priority = &Priority{}
body, _ := io.ReadAll(c.Request.Body)
log.Printf("set priority %s", string(body))
c.Request.Body = io.NopCloser(bytes.NewReader(body))
c.ShouldBind(priority)
log.Printf("priority: %+v", priority)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": priority,
})
var msg = Message{
Event: "priority",
Data: priority,
}
app.Stream.Send(msg)
app.Stream.Data = priority
}

12
backend.light/priority.go Normal file
View file

@ -0,0 +1,12 @@
package main
type Priority struct {
Surfers []Surfer `json:"surfers"`
Count int `json:"count"`
}
type Surfer struct {
Name string `json:"name"`
Color string `json:"color"`
Priority string `json:"priority"`
}

View file

@ -13,29 +13,6 @@ import (
"github.com/gin-gonic/gin"
)
var events = []string{"message", "timer", "priority", "cmd"}
type Message struct {
Event string `json:"event"`
Data string `json:"data"`
Id string `json:"id"`
}
type ClientChan chan Message
type Client struct {
Id string `json:"id"`
Ip string `json:"ip"`
Chan ClientChan `json:"chan"`
Events []string `json:"events"`
}
type SseStream struct {
Clients []Client `json:"clients"`
MsgId map[string]int `json:"msgid"`
Events []string `json:"events"`
}
func InitSse() *SseStream {
sse := &SseStream{
Clients: make([]Client, 0),
@ -79,7 +56,7 @@ func (sse *SseStream) Stream(c *gin.Context) {
log.Printf("Client %s disconnected", client.Ip)
}()
log.Printf("Client %s connected", client.Ip)
log.Printf("Client %s connected - TOT %+v", client.Ip, sse.Clients)
c.Stream(func(w io.Writer) bool {
msg, ok := <-client.Chan

View file

@ -0,0 +1,25 @@
package main
var events = []string{"message", "timer", "priority", "cmd"}
type Message struct {
Event string `json:"event"`
Data interface{} `json:"data"`
Id string `json:"id"`
}
type ClientChan chan Message
type Client struct {
Id string `json:"id"`
Ip string `json:"ip"`
Chan ClientChan `json:"chan"`
Events []string `json:"events"`
}
type SseStream struct {
Clients []Client `json:"clients"`
MsgId map[string]int `json:"msgid"`
Events []string `json:"events"`
Data interface{} `json:"data"`
}

View file

@ -1,7 +1 @@
package main
type Surfers struct {
Name string `json:"name"`
Color string `json:"color"`
Priority string `json:"priority"`
}