refactor(controller): added env and other fixes to account for running as a service

This commit is contained in:
2025-09-08 16:08:30 -05:00
parent ddfeb5ccb5
commit fc7ecb6ab6
4 changed files with 43 additions and 25 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
socketio "github.com/googollee/go-socket.io"
@@ -13,22 +12,21 @@ func Setup(basePath string, server *socketio.Server) *gin.Engine {
r := gin.Default()
if os.Getenv("NODE") == "production" {
gin.SetMode(gin.ReleaseMode)
}
r.GET(basePath+"/check", func(c *gin.Context) {
c.JSON(http.StatusAccepted, gin.H{"check": "good"})
})
//logger.RegisterLoggerRoutes(r, basePath, db)
r.POST(basePath+"/update", func(c *gin.Context) {
fmt.Println("Just hit the route")
var payload UpdatePayload
if err := c.BindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println("Past first json check")
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
@@ -39,18 +37,29 @@ func Setup(basePath string, server *socketio.Server) *gin.Engine {
return
}
fmt.Println("Past flusher")
// Start the update and get the channel
updates := UpdateApp(server)
ctx := c.Request.Context() // Gin request context
for msg := range updates {
//fmt.Fprintf(c.Writer, "event: log\ndata: %s\n\n", msg)
fmt.Fprintf(c.Writer, "%s\n\n", msg)
flusher.Flush()
fmt.Println("attempting the loop")
for {
select {
case <-ctx.Done():
fmt.Println("Client closed connection")
return
case msg, ok := <-updates:
if !ok {
fmt.Fprintf(c.Writer, "Update process finished\n\n")
flusher.Flush()
return
}
fmt.Fprintf(c.Writer, "%s\n\n", msg)
flusher.Flush()
}
}
fmt.Fprintf(c.Writer, "Update process finished\n\n")
flusher.Flush()
})
r.Any(basePath+"/", func(c *gin.Context) { errorApiLoc(c) })