diff --git a/controller/.env-example b/controller/.env-example new file mode 100644 index 0000000..9c5e59e --- /dev/null +++ b/controller/.env-example @@ -0,0 +1,3 @@ +# What type of deploy ment do we have for the frontend + +NODE_ENV=dev diff --git a/controller/main.go b/controller/main.go index a4616e8..78fe743 100644 --- a/controller/main.go +++ b/controller/main.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "path/filepath" "time" "github.com/gin-gonic/gin" @@ -14,14 +15,12 @@ import ( func main() { - err := godotenv.Load("../.env") - if err != nil { - fmt.Println("Warning: .env file not found") - } + exePath, _ := os.Executable() + exeDir := filepath.Dir(exePath) - port := os.Getenv("PORT") - if port == "" { - port = "8080" + if err := godotenv.Load(filepath.Join(exeDir, ".env")); err != nil { + // fallback dev path + _ = godotenv.Load("../.env") } // gin stuff @@ -30,6 +29,11 @@ func main() { basePath = "/lst/api/controller" } + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + server := socketio.NewServer(nil) r := Setup(basePath, server) // returns *gin.Engine diff --git a/controller/router.go b/controller/router.go index 2a62b73..1df1e43 100644 --- a/controller/router.go +++ b/controller/router.go @@ -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) }) diff --git a/controller/update_channel.go b/controller/update_channel.go index 53956c4..d4a055e 100644 --- a/controller/update_channel.go +++ b/controller/update_channel.go @@ -88,16 +88,18 @@ func copyLatestBuild(server *socketio.Server, target string) { func triggerRemoteUpdate(server *socketio.Server, remoteURL string, payload UpdatePayload) { - // basePath := "/api/controller" - // if os.Getenv("NODE_ENV") != "production" { - // basePath = "/lst/api/controller" - // } + fmt.Println("running the update process") + basePath := "/api/controller" + if os.Getenv("NODE_ENV") != "production" { + basePath = "/lst/api/controller" + } // send POST request with JSON, expect SSE / streaming text back body, _ := json.Marshal(payload) - //url := fmt.Sprintf("https://%v.alpla.net%v/update", remoteURL, basePath) - url := fmt.Sprintf("http://%v:8080/api/controller/update", remoteURL) + url := fmt.Sprintf("https://%v.alpla.net%v/update", remoteURL, basePath) + fmt.Println(url) + //url := fmt.Sprintf("http://%v:8080/api/controller/update", remoteURL) fmt.Println(url) resp, err := http.Post(url, "application/json", bytes.NewBuffer(body)) if err != nil {