refactor(controller): added env and other fixes to account for running as a service
This commit is contained in:
3
controller/.env-example
Normal file
3
controller/.env-example
Normal file
@@ -0,0 +1,3 @@
|
||||
# What type of deploy ment do we have for the frontend
|
||||
|
||||
NODE_ENV=dev
|
||||
@@ -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
|
||||
|
||||
@@ -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,17 +37,28 @@ 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.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()
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user