INIT
This commit is contained in:
parent
d9a1837f3b
commit
ebd2f05ae2
69 changed files with 2335 additions and 451 deletions
5
backend.old/.gitignore
vendored
Normal file
5
backend.old/.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
.DS_Store
|
||||
/static
|
||||
.env
|
||||
.env.*
|
||||
go.sum
|
21
backend.old/Heat/Qualifying.1.Under 18 Men.json
Normal file
21
backend.old/Heat/Qualifying.1.Under 18 Men.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "Qualifying",
|
||||
"category": "Under 18 Men",
|
||||
"number": 1,
|
||||
"timer": 20,
|
||||
"status": "idle",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Uno",
|
||||
"color": "red",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Due",
|
||||
"color": "blue",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
33
backend.old/Heat/Quarterfinal.1.Under 14 Men.json
Normal file
33
backend.old/Heat/Quarterfinal.1.Under 14 Men.json
Normal file
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"name": "Quarterfinal",
|
||||
"category": "Under 14 Men",
|
||||
"number": 1,
|
||||
"timer": 20,
|
||||
"status": "idle",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Uno",
|
||||
"color": "green",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Due",
|
||||
"color": "yellow",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Tre",
|
||||
"color": "orange",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Quattro",
|
||||
"color": "red",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
27
backend.old/Heat/Quarterfinal.2.Under 14 Men.json
Normal file
27
backend.old/Heat/Quarterfinal.2.Under 14 Men.json
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"name": "Quarterfinal",
|
||||
"category": "Under 14 Men",
|
||||
"number": 2,
|
||||
"timer": 20,
|
||||
"status": "idle",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Cinque",
|
||||
"color": "red",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Sei",
|
||||
"color": "blue",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Sette",
|
||||
"color": "violet",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
21
backend.old/Heat/Semifinal.1.Under 12 Men.json
Normal file
21
backend.old/Heat/Semifinal.1.Under 12 Men.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "Semifinal",
|
||||
"category": "Under 12 Men",
|
||||
"number": 1,
|
||||
"timer": 20,
|
||||
"status": "running",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Uno",
|
||||
"color": "blue",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Due",
|
||||
"color": "yellow",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
21
backend.old/Heat/Semifinal.1.Under 12 Women.json
Normal file
21
backend.old/Heat/Semifinal.1.Under 12 Women.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "Semifinal",
|
||||
"category": "Under 12 Women",
|
||||
"number": 1,
|
||||
"timer": 20,
|
||||
"status": "idle",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Uno",
|
||||
"color": "red",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Due",
|
||||
"color": "green",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
21
backend.old/Heat/Semifinal.2.Under 12 Men.json
Normal file
21
backend.old/Heat/Semifinal.2.Under 12 Men.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "Semifinal",
|
||||
"category": "Under 12 Men",
|
||||
"number": 2,
|
||||
"timer": 20,
|
||||
"status": "ended",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Tre",
|
||||
"color": "green",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Quattro",
|
||||
"color": "yellow",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
21
backend.old/Heat/Semifinal.2.Under 12 Women.json
Normal file
21
backend.old/Heat/Semifinal.2.Under 12 Women.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "Semifinal",
|
||||
"category": "Under 12 Women",
|
||||
"number": 2,
|
||||
"timer": 20,
|
||||
"status": "idle",
|
||||
"surfers": [
|
||||
{
|
||||
"name": "Tre",
|
||||
"color": "green",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
},
|
||||
{
|
||||
"name": "Quattro",
|
||||
"color": "blue",
|
||||
"priority": "",
|
||||
"score": ""
|
||||
}
|
||||
]
|
||||
}
|
32
backend.old/api.go
Normal file
32
backend.old/api.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
func (w *Webapp) initApi() {
|
||||
|
||||
stream := NewServer()
|
||||
|
||||
w.Stream = stream
|
||||
|
||||
http_api := w.Engine.Group("/api")
|
||||
http_api.GET("/priority", w.GetPriority)
|
||||
http_api.POST("/priority", w.SetPriority)
|
||||
|
||||
// SSE
|
||||
http_api.GET("/sse", HeadersMiddleware(), stream.serveHTTP(), stream.retvalSSE())
|
||||
|
||||
http_api.POST("/startheat", w.StartHeatTimer)
|
||||
http_api.GET("/stopheat", w.StopHeatTimer)
|
||||
http_api.POST("/saveheat", w.SaveHeat)
|
||||
http_api.POST("/deleteheat", w.DeleteHeat)
|
||||
http_api.GET("/loadheats", w.LoadHeats)
|
||||
http_api.GET("/runningheat", w.LoadRunning)
|
||||
|
||||
// // Surfers
|
||||
// http_api.GET("/surfers", w.GetSurfers)
|
||||
// http_api.POST("/updatesurfer", w.UpdateSurfer)
|
||||
// http_api.POST("/deletesurfer", w.DeleteSurfer)
|
||||
|
||||
// // Users
|
||||
// http_api.GET("/users", w.GetUsers)
|
||||
// http_api.POST("/updateuser", w.UpdateUser)
|
||||
// http_api.POST("/deleteuser", w.DeleteUser)
|
||||
}
|
BIN
backend.old/backend
Executable file
BIN
backend.old/backend
Executable file
Binary file not shown.
37
backend.old/color.go
Normal file
37
backend.old/color.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
type Color int
|
||||
|
||||
const (
|
||||
Gray Color = iota
|
||||
Black
|
||||
Blue
|
||||
Red
|
||||
Yellow
|
||||
Green
|
||||
White
|
||||
Magenta
|
||||
)
|
||||
|
||||
func (c Color) String() string {
|
||||
switch c {
|
||||
case Gray:
|
||||
return "gray"
|
||||
case Black:
|
||||
return "black"
|
||||
case Blue:
|
||||
return "blue"
|
||||
case Red:
|
||||
return "red"
|
||||
case Yellow:
|
||||
return "yellow"
|
||||
case Green:
|
||||
return "green"
|
||||
case White:
|
||||
return "white"
|
||||
case Magenta:
|
||||
return "magenta"
|
||||
}
|
||||
|
||||
return "gray"
|
||||
}
|
44
backend.old/db.go
Normal file
44
backend.old/db.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
scribble "github.com/nanobox-io/golang-scribble"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
Db *scribble.Driver
|
||||
}
|
||||
|
||||
func InitDb(dbAddress string) *DB {
|
||||
var err error
|
||||
var db *DB
|
||||
|
||||
db.Db, err = scribble.New("", nil)
|
||||
if err != nil {
|
||||
fmt.Println("Error", err)
|
||||
}
|
||||
|
||||
log.Printf("App: %+v", db)
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *DB) Write(table string, key string, value interface{}) error {
|
||||
err := db.Db.Write(table, key, value)
|
||||
if err != nil {
|
||||
fmt.Println("Error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) Read(table string, key string, value interface{}) error {
|
||||
err := db.Db.Read(table, key, value)
|
||||
if err != nil {
|
||||
fmt.Println("Error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
39
backend.old/go.mod
Normal file
39
backend.old/go.mod
Normal file
|
@ -0,0 +1,39 @@
|
|||
module backend
|
||||
|
||||
go 1.21.4
|
||||
|
||||
require (
|
||||
github.com/gin-contrib/cors v1.5.0
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.10.1 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
||||
github.com/chenzhuoyu/iasm v0.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.15.5 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
golang.org/x/arch v0.5.0 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
golang.org/x/net v0.16.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
12
backend.old/hash.go
Normal file
12
backend.old/hash.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func CalcId(str string, len int) string {
|
||||
id := sha256.Sum256([]byte(str))
|
||||
|
||||
return fmt.Sprintf("%X", id)[0:len]
|
||||
}
|
195
backend.old/heats.go
Normal file
195
backend.old/heats.go
Normal file
|
@ -0,0 +1,195 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
scribble "github.com/nanobox-io/golang-scribble"
|
||||
)
|
||||
|
||||
type Surfer struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
Priority string `json:"priority"`
|
||||
Score string `json:"score"`
|
||||
}
|
||||
|
||||
type Heat struct {
|
||||
Name string `json:"name"`
|
||||
Category string `json:"category"`
|
||||
Number int `json:"number"`
|
||||
Timer int `json:"timer"`
|
||||
Status string `json:"status"`
|
||||
Surfers []Surfer `json:"surfers"`
|
||||
}
|
||||
|
||||
func heatName(heat Heat) string {
|
||||
return fmt.Sprintf("%s.%d.%s", heat.Name, heat.Number, heat.Category)
|
||||
}
|
||||
|
||||
func (w *Webapp) SaveHeat(c *gin.Context) {
|
||||
var heat Heat
|
||||
|
||||
err := c.ShouldBind(&heat)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("req error: %+v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("heat: %+v", heat)
|
||||
|
||||
heat.Status = "idle"
|
||||
|
||||
err = w.DB.Write("Heat", heatName(heat), heat)
|
||||
if err != nil {
|
||||
log.Printf("set error: %+v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": fmt.Sprintf("Error: %+v", err)})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "saved"})
|
||||
}
|
||||
|
||||
func (w *Webapp) DeleteHeat(c *gin.Context) {
|
||||
var heat Heat
|
||||
|
||||
err := c.ShouldBind(&heat)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("req error: %+v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("heat: %+v", heat)
|
||||
|
||||
err = w.DB.Delete("Heat", heatName(heat))
|
||||
if err != nil {
|
||||
log.Printf("set error: %+v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": fmt.Sprintf("Error: %+v", err)})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
}
|
||||
|
||||
func (w *Webapp) LoadRunning(c *gin.Context) {
|
||||
heats := loadHeats(w.DB)
|
||||
|
||||
for _, heat := range heats {
|
||||
if heat.Status == "running" {
|
||||
c.JSON(http.StatusOK, heat)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, "")
|
||||
}
|
||||
|
||||
func (w *Webapp) LoadHeats(c *gin.Context) {
|
||||
heats := loadHeats(w.DB)
|
||||
|
||||
c.JSON(http.StatusOK, heats)
|
||||
|
||||
log.Printf("heats: %+v", heats)
|
||||
}
|
||||
|
||||
func (w *Webapp) StartHeatTimer(c *gin.Context) {
|
||||
var msg Message
|
||||
var err error
|
||||
var timer time.Duration
|
||||
|
||||
if w.Stream.Start {
|
||||
c.JSON(http.StatusOK, w.Stream.Duration)
|
||||
return
|
||||
}
|
||||
|
||||
err = c.ShouldBind(&msg)
|
||||
if err != nil {
|
||||
log.Printf("req error: %+v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
timer, err = time.ParseDuration(msg.Duration)
|
||||
if err != nil {
|
||||
log.Printf("req error: %+v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
w.Stream.Duration = timer
|
||||
w.Stream.Start = true
|
||||
|
||||
startHeat(w.DB, *w.Stream.Heat)
|
||||
|
||||
log.Printf("start timer %s - received %s", w.Stream.Duration, msg.Duration)
|
||||
c.JSON(http.StatusOK, w.Stream.Duration)
|
||||
}
|
||||
|
||||
func (w *Webapp) StopHeatTimer(c *gin.Context) {
|
||||
if !w.Stream.Start {
|
||||
c.JSON(http.StatusOK, w.Stream.Duration)
|
||||
return
|
||||
}
|
||||
|
||||
stopHeat(w.DB, *w.Stream.Heat)
|
||||
w.Stream.Start = false
|
||||
w.Stream.Duration = 0
|
||||
|
||||
log.Printf("start timer %s", w.Stream.Duration)
|
||||
c.JSON(http.StatusOK, w.Stream.Duration)
|
||||
}
|
||||
|
||||
func loadHeats(db *scribble.Driver) []Heat {
|
||||
records, err := db.ReadAll("Heat")
|
||||
if err != nil {
|
||||
fmt.Printf("read error: %+v", err)
|
||||
}
|
||||
|
||||
heats := make([]Heat, 0)
|
||||
for _, record := range records {
|
||||
var heat Heat
|
||||
err = json.Unmarshal([]byte(record), &heat)
|
||||
if err != nil {
|
||||
fmt.Printf("decode error: %+v", err)
|
||||
}
|
||||
heats = append(heats, heat)
|
||||
}
|
||||
|
||||
return heats
|
||||
}
|
||||
|
||||
func startHeat(db *scribble.Driver, heat Heat) error {
|
||||
log.Printf("heat: %+v", heat)
|
||||
|
||||
heat.Status = "running"
|
||||
|
||||
err := db.Write("Heat", heatName(heat), heat)
|
||||
if err != nil {
|
||||
log.Printf("set error: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopHeat(db *scribble.Driver, heat Heat) error {
|
||||
|
||||
log.Printf("heat: %+v", heat)
|
||||
|
||||
heat.Status = "ended"
|
||||
|
||||
err := db.Write("Heat", heatName(heat), heat)
|
||||
if err != nil {
|
||||
log.Printf("set error: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
21
backend.old/main.go
Normal file
21
backend.old/main.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var port string
|
||||
|
||||
Db := InitDb(os.Getenv("DB"))
|
||||
|
||||
webapp := InitHttp(Db)
|
||||
|
||||
if p := os.Getenv("PORT"); p == "" {
|
||||
port = "8080"
|
||||
} else {
|
||||
port = p
|
||||
}
|
||||
|
||||
webapp.Engine.Run(":" + port)
|
||||
}
|
28
backend.old/msgmode.go
Normal file
28
backend.old/msgmode.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
type Mode int
|
||||
|
||||
const (
|
||||
Priority Mode = iota
|
||||
Start
|
||||
Stop
|
||||
Time
|
||||
UpdateHeat
|
||||
)
|
||||
|
||||
func (t Mode) String() string {
|
||||
switch t {
|
||||
case Priority:
|
||||
return "priority"
|
||||
case Stop:
|
||||
return "stop"
|
||||
case Time:
|
||||
return "time"
|
||||
case Start:
|
||||
return "start"
|
||||
case UpdateHeat:
|
||||
return "updateHeat"
|
||||
}
|
||||
|
||||
return "priority"
|
||||
}
|
37
backend.old/priority.go
Normal file
37
backend.old/priority.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ///////// Priority
|
||||
func (w *Webapp) GetPriority(c *gin.Context) {
|
||||
log.Printf("send priority %s", w.Stream.StatusPriority)
|
||||
|
||||
c.JSON(http.StatusOK, w.Stream.StatusPriority)
|
||||
}
|
||||
|
||||
func (w *Webapp) SetPriority(c *gin.Context) {
|
||||
var msg Message
|
||||
var err error
|
||||
|
||||
log.Printf("set priority %s", c.Request.Body)
|
||||
|
||||
err = c.ShouldBind(&msg)
|
||||
if err != nil {
|
||||
log.Printf("req error: %+v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, "OK")
|
||||
|
||||
log.Printf("msg %+v", msg)
|
||||
|
||||
w.Stream.StatusPriority = msg.Priority
|
||||
|
||||
w.Stream.SendPriority(msg.Priority)
|
||||
}
|
216
backend.old/sse.go
Normal file
216
backend.old/sse.go
Normal file
|
@ -0,0 +1,216 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"log"
|
||||
)
|
||||
|
||||
type SurferLive struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
Score string `json:"score"`
|
||||
Priority string `json:"priority"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Surfers []SurferLive `json:"surfers"`
|
||||
Priority []string `json:"priority"`
|
||||
Heat Heat `json:"heat"`
|
||||
Duration string `json:"duration"`
|
||||
Source string `json:"source"`
|
||||
Msg string `json:"msg"`
|
||||
Mode string `json:"mode"`
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Chan ClientChan
|
||||
IP IPAddress
|
||||
Mode Mode
|
||||
}
|
||||
|
||||
type ClientChan chan Message
|
||||
|
||||
type IPAddress string
|
||||
|
||||
type SseStream struct {
|
||||
// Events are pushed to this channel by the main events-gathering routine
|
||||
Message chan Message
|
||||
|
||||
// New client connections
|
||||
NewClients chan Client //chan string
|
||||
|
||||
// Closed client connections
|
||||
ClosedClients chan ClientChan
|
||||
|
||||
// Total client connections
|
||||
TotalClients map[ClientChan]IPAddress //bool
|
||||
|
||||
StatusPriority []string
|
||||
Duration time.Duration
|
||||
Start bool
|
||||
Heat *Heat
|
||||
}
|
||||
|
||||
// Initialize event and Start procnteessing requests
|
||||
func NewServer() (sse *SseStream) {
|
||||
sse = &SseStream{
|
||||
Message: make(chan Message),
|
||||
NewClients: make(chan Client),
|
||||
ClosedClients: make(chan ClientChan),
|
||||
TotalClients: make(map[ClientChan]IPAddress),
|
||||
Start: false,
|
||||
Heat: &Heat{},
|
||||
}
|
||||
|
||||
go sse.listen()
|
||||
|
||||
go sse.timer()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// It Listens all incoming requests from clients.
|
||||
// Handles addition and removal of clients and broadcast messages to clients.
|
||||
func (stream *SseStream) listen() {
|
||||
for {
|
||||
select {
|
||||
// Add new available client
|
||||
case client := <-stream.NewClients:
|
||||
stream.TotalClients[client.Chan] = client.IP
|
||||
log.Printf("Client added. %d - %s registered clients", len(stream.TotalClients), client.IP)
|
||||
|
||||
// Remove closed client
|
||||
case client := <-stream.ClosedClients:
|
||||
ip := stream.TotalClients[client]
|
||||
delete(stream.TotalClients, client)
|
||||
close(client)
|
||||
log.Printf("Removed client. %d - %s registered clients", len(stream.TotalClients), ip)
|
||||
|
||||
// Broadcast message to client
|
||||
case eventMsg := <-stream.Message:
|
||||
for clientMessageChan := range stream.TotalClients {
|
||||
clientMessageChan <- eventMsg
|
||||
log.Printf("Message %+v sent to %s", eventMsg, stream.TotalClients[clientMessageChan])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HeadersMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||
c.Writer.Header().Set("Cache-Control", "no-cache")
|
||||
c.Writer.Header().Set("Connection", "keep-alive")
|
||||
c.Writer.Header().Set("Transfer-Encoding", "chunked")
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (stream *SseStream) serveHTTP() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// Initialize client channel
|
||||
clientChan := make(ClientChan)
|
||||
|
||||
cli := Client{
|
||||
Chan: clientChan,
|
||||
IP: IPAddress(c.ClientIP()),
|
||||
Mode: Priority,
|
||||
}
|
||||
|
||||
// Send new connection to event server
|
||||
stream.NewClients <- cli
|
||||
|
||||
defer func() {
|
||||
// Send closed connection to event server
|
||||
stream.ClosedClients <- clientChan
|
||||
}()
|
||||
|
||||
c.Set("clientChan", clientChan)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (stream *SseStream) retvalSSE() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
if len(stream.StatusPriority) > 0 {
|
||||
stream.SendPriority(stream.StatusPriority)
|
||||
log.Printf("update priority %+v", stream.StatusPriority)
|
||||
}
|
||||
|
||||
v, ok := c.Get("clientChan")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
clientChan, ok := v.(ClientChan)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
// Stream message to client from message channel
|
||||
if msg, ok := <-clientChan; ok {
|
||||
c.SSEvent("message", msg)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (stream *SseStream) SendPriority(pri []string) {
|
||||
stream.Message <- Message{
|
||||
Priority: pri,
|
||||
Mode: Priority.String(),
|
||||
}
|
||||
}
|
||||
|
||||
func (stream *SseStream) timer() {
|
||||
for {
|
||||
if stream.Start {
|
||||
timer := time.NewTimer(stream.Duration)
|
||||
|
||||
select {
|
||||
case <-timer.C:
|
||||
stream.Start = false
|
||||
msg := Message{
|
||||
Msg: "stop",
|
||||
Mode: Stop.String(),
|
||||
}
|
||||
stream.Message <- msg
|
||||
log.Printf("stop timer %+v", stream.Duration)
|
||||
stream.Heat.Status = "ended"
|
||||
continue
|
||||
default:
|
||||
if len(stream.TotalClients) > 0 {
|
||||
if stream.Duration >= 0 {
|
||||
currentTimer := fmt.Sprintf("%v", formatTime(stream.Duration))
|
||||
|
||||
msg := Message{
|
||||
Duration: currentTimer,
|
||||
Mode: Time.String(),
|
||||
}
|
||||
|
||||
// Send current time to clients message channel
|
||||
stream.Message <- msg
|
||||
stream.Duration = stream.Duration - time.Second
|
||||
time.Sleep(time.Second * 1)
|
||||
} else {
|
||||
timer.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func formatTime(d time.Duration) string {
|
||||
minutes := int(d.Minutes()) % 60
|
||||
seconds := int(d.Seconds()) % 60
|
||||
return fmt.Sprintf("%02d:%02d", minutes, seconds)
|
||||
}
|
117
backend.old/surfers.go
Normal file
117
backend.old/surfers.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package main
|
||||
|
||||
// type Surfer struct {
|
||||
// Dbid string `json:"dbid"`
|
||||
// Id string `json:"id"`
|
||||
// Firstname string `json:"firstname"`
|
||||
// Lastname string `json:"lastname"`
|
||||
// }
|
||||
|
||||
///////////// Surfers
|
||||
|
||||
// func (w *Webapp) GetSurfers(c *gin.Context) {
|
||||
// var cursor uint64
|
||||
|
||||
// var surfersKeys []string
|
||||
|
||||
// cursor = 0
|
||||
|
||||
// log.Printf("start scanning %+v", c.ClientIP())
|
||||
// for {
|
||||
// var keys []string
|
||||
// var err error
|
||||
// log.Printf("scan: cursor = %d", cursor)
|
||||
// keys, cursor, err = w.DB.Redis.Scan(w.DB.Ctx, cursor, "surfer:*", 10).Result()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// log.Printf("ret scan: cursor = %d", cursor)
|
||||
|
||||
// log.Printf("scan: %+v", keys)
|
||||
|
||||
// surfersKeys = append(surfersKeys, keys...)
|
||||
|
||||
// if cursor == 0 {
|
||||
// log.Printf("end scan: cursor = %d", cursor)
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
// var surfers []Surfer
|
||||
|
||||
// for u := range surfersKeys {
|
||||
// surf := Surfer{}
|
||||
// ret := w.DB.Redis.HMGet(w.DB.Ctx, surfersKeys[u], "firstname", "lastname", "id", "sex", "birthdate", "stance", "hometown")
|
||||
|
||||
// ret.Scan(&surf)
|
||||
// surf.Dbid = strings.Split(surfersKeys[u], ":")[1]
|
||||
|
||||
// log.Printf("surfer: %+v", surf)
|
||||
// surfers = append(surfers, surf)
|
||||
// }
|
||||
|
||||
// slices.SortFunc(surfers,
|
||||
// func(a, b Surfer) int {
|
||||
// return cmp.Compare(a.Id, b.Id)
|
||||
// })
|
||||
|
||||
// log.Printf("surfers: %+v", surfers)
|
||||
|
||||
// c.JSON(http.StatusOK, surfers)
|
||||
// }
|
||||
|
||||
// func (w *Webapp) DeleteSurfer(c *gin.Context) {
|
||||
// var surfer Surfer
|
||||
// var err error
|
||||
|
||||
// err = c.ShouldBind(&surfer)
|
||||
// if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
// return
|
||||
// }
|
||||
|
||||
// id := CalcId(surfer.Firstname+surfer.Lastname, _MaxLen)
|
||||
|
||||
// log.Printf("deleting: %+v", "surfer:"+id)
|
||||
|
||||
// res, err := w.DB.Redis.HGetAll(w.DB.Ctx, "surfer:"+id).Result()
|
||||
// if err != nil || len(res) == 0 {
|
||||
// log.Printf("del error: %+v", err)
|
||||
// c.JSON(http.StatusNotFound, gin.H{"status": "Not Found"})
|
||||
// return
|
||||
// }
|
||||
|
||||
// log.Printf("found: %+v", res)
|
||||
|
||||
// err = w.DB.Redis.Del(w.DB.Ctx, "surfer:"+id).Err()
|
||||
// if err != nil {
|
||||
// log.Printf("del error: %+v", err)
|
||||
// }
|
||||
|
||||
// log.Printf("del: %+v", surfer)
|
||||
|
||||
// c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
// }
|
||||
|
||||
// func (w *Webapp) UpdateSurfer(c *gin.Context) {
|
||||
// var surfer Surfer
|
||||
// var err error
|
||||
|
||||
// err = c.ShouldBind(&surfer)
|
||||
// if err != nil {
|
||||
// log.Printf("req error: %+v", err)
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
// return
|
||||
// }
|
||||
|
||||
// surfer.Dbid = CalcId(surfer.Firstname+surfer.Lastname, _MaxLen)
|
||||
|
||||
// err = w.DB.Redis.HSet(w.DB.Ctx, "surfer:"+surfer.Dbid, surfer).Err()
|
||||
// if err != nil {
|
||||
// log.Printf("set error: %+v", err)
|
||||
// }
|
||||
|
||||
// log.Printf("new: %+v", surfer)
|
||||
|
||||
// c.JSON(http.StatusOK, gin.H{"status": "added"})
|
||||
// }
|
135
backend.old/users.go
Normal file
135
backend.old/users.go
Normal file
|
@ -0,0 +1,135 @@
|
|||
package main
|
||||
|
||||
// type User struct {
|
||||
// Dbid string `json:"dbid"`
|
||||
// Id string `json:"id"`
|
||||
// Username string `json:"username"`
|
||||
// Password string `json:"password"`
|
||||
// Email string `json:"email"`
|
||||
// Group string `json:"group"`
|
||||
// Enabled bool `json:"enabled"`
|
||||
// }
|
||||
|
||||
///////////////// Users
|
||||
|
||||
// func (w *Webapp) GetUsers(c *gin.Context) {
|
||||
// var cursor uint64
|
||||
|
||||
// var users []string
|
||||
|
||||
// cursor = 0
|
||||
// log.Printf("start scanning %+v", c.ClientIP())
|
||||
// for {
|
||||
// var keys []string
|
||||
// var err error
|
||||
// keys, cursor, err = w.DB.Redis.Scan(w.DB.Ctx, cursor, "user:*", 10).Result()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// log.Printf("scan: %+v", keys)
|
||||
|
||||
// users = append(users, keys...)
|
||||
|
||||
// if cursor == 0 {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
// var retval []User
|
||||
|
||||
// for u := range users {
|
||||
// usr := User{}
|
||||
// hmget := w.DB.Redis.HMGet(w.DB.Ctx, users[u], "username", "email", "group", "enabled")
|
||||
// hmget.Scan(&usr)
|
||||
|
||||
// log.Printf("user: %+v", usr)
|
||||
// retval = append(retval, usr)
|
||||
// }
|
||||
|
||||
// slices.SortFunc(retval,
|
||||
// func(a, b User) int {
|
||||
// return cmp.Compare(a.Username, b.Username)
|
||||
// })
|
||||
|
||||
// c.JSON(http.StatusOK, retval)
|
||||
// }
|
||||
|
||||
// func (w *Webapp) UpdateUser(c *gin.Context) {
|
||||
// var user User
|
||||
|
||||
// if err := c.ShouldBind(&user); err != nil {
|
||||
// log.Printf("req error: %+v", err)
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
// return
|
||||
// }
|
||||
|
||||
// user.Dbid = CalcId(user.Username, 8)
|
||||
|
||||
// ret := w.DB.Redis.HExists(w.DB.Ctx, "user:"+user.Dbid, "username").Val()
|
||||
// if !ret {
|
||||
// log.Printf("Exists: %+v", ret)
|
||||
// err := w.DB.Redis.HSet(w.DB.Ctx, "user:"+user.Dbid, user).Err()
|
||||
// if err != nil {
|
||||
// log.Printf("set error: %+v", err)
|
||||
// }
|
||||
// } else {
|
||||
// err := w.DB.Redis.HSet(w.DB.Ctx, "user:"+user.Dbid, "email", user.Email, "group", user.Group, "enabled", user.Enabled).Err()
|
||||
// if err != nil {
|
||||
// log.Printf("set error: %+v", err)
|
||||
// }
|
||||
// }
|
||||
|
||||
// log.Printf("new: %+v", user)
|
||||
|
||||
// c.JSON(http.StatusOK, gin.H{"status": "added"})
|
||||
// }
|
||||
|
||||
// // func UpdateUser(c *gin.Context) {
|
||||
// // var user common.User
|
||||
|
||||
// // if err := c.ShouldBind(&user); err != nil {
|
||||
// // c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// // id := common.Id(user.Username, 8)
|
||||
|
||||
// // if err := db.Db.Redis.HSet(db.Db.Ctx, "user:"+id, "email", user.Email, "group", user.Group, "enabled", user.Enabled).Err(); err != nil {
|
||||
// // log.Fatalf("set error: %+v", err)
|
||||
// // }
|
||||
|
||||
// // log.Printf("update: %+v", user)
|
||||
|
||||
// // c.JSON(http.StatusOK, gin.H{"status": "updated"})
|
||||
// // }
|
||||
|
||||
// func (w *Webapp) DeleteUser(c *gin.Context) {
|
||||
// var user User
|
||||
|
||||
// if err := c.ShouldBind(&user); err != nil {
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
// return
|
||||
// }
|
||||
|
||||
// id := CalcId(user.Username, 8)
|
||||
|
||||
// log.Printf("deleting: %+v", "user:"+id)
|
||||
|
||||
// res, err := w.DB.Redis.HGetAll(w.DB.Ctx, "user:"+id).Result()
|
||||
// if err != nil || len(res) == 0 {
|
||||
// log.Printf("del error: %+v", err)
|
||||
// c.JSON(http.StatusNotFound, gin.H{"status": "Not Found"})
|
||||
// return
|
||||
// }
|
||||
|
||||
// log.Printf("found: %+v", res)
|
||||
|
||||
// if err := w.DB.Redis.Del(w.DB.Ctx, "user:"+id).Err(); err != nil {
|
||||
// log.Printf("del error: %+v", err)
|
||||
// }
|
||||
|
||||
// log.Printf("del: %+v", user)
|
||||
|
||||
// c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
// }
|
65
backend.old/webapp.go
Normal file
65
backend.old/webapp.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
scribble "github.com/nanobox-io/golang-scribble"
|
||||
)
|
||||
|
||||
type Webapp struct {
|
||||
Engine *gin.Engine
|
||||
Stream *SseStream
|
||||
DB *scribble.Driver
|
||||
}
|
||||
|
||||
// const _MaxLen = 8
|
||||
|
||||
func InitHttp(d *scribble.Driver) *Webapp {
|
||||
|
||||
router := gin.Default()
|
||||
router.Use(cors.Default())
|
||||
|
||||
wapp := &Webapp{
|
||||
Engine: router,
|
||||
DB: d,
|
||||
}
|
||||
|
||||
wapp.initApi()
|
||||
// wapp.initAuth()
|
||||
|
||||
displayH := router.Group("/displayh")
|
||||
displayH.Static("/", "./static/displayh")
|
||||
|
||||
displayV := router.Group("/displayv")
|
||||
displayV.Static("/", "./static/displayv")
|
||||
|
||||
priority := router.Group("/priority")
|
||||
priority.Static("/", "./static/priority")
|
||||
|
||||
mobile := router.Group("/mobile")
|
||||
mobile.Static("/", "./static/mobile")
|
||||
|
||||
setup := router.Group("/setup")
|
||||
setup.Static("/", "./static/setup")
|
||||
|
||||
draws := router.Group("/draws")
|
||||
draws.Static("/", "./static/draws")
|
||||
|
||||
sapp := router.Group("/_app")
|
||||
sapp.Static("/", "./static/_app")
|
||||
|
||||
static := router.Group("/static")
|
||||
static.Static("/", "./static/static")
|
||||
|
||||
router.StaticFile("/", "./static/index.html")
|
||||
router.StaticFile("/favicon.png", "./static/favicon.png")
|
||||
|
||||
router.ForwardedByClientIP = true
|
||||
router.SetTrustedProxies([]string{"127.0.0.1"})
|
||||
|
||||
log.Printf("WebApp: %+v", wapp)
|
||||
|
||||
return wapp
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue