69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UpdatePayload struct {
|
|
Action string `json:"action"`
|
|
Target string `json:"target"`
|
|
}
|
|
|
|
func Setup(basePath string) *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) {
|
|
var payload UpdatePayload
|
|
if err := c.BindJSON(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
|
c.Writer.Header().Set("Connection", "keep-alive")
|
|
|
|
flusher, ok := c.Writer.(http.Flusher)
|
|
if !ok {
|
|
c.String(http.StatusInternalServerError, "Streaming not supported")
|
|
return
|
|
}
|
|
|
|
steps := []string{"🚀 Starting...", "📂 Copying...", "🔧 Migrating...", "✅ Done!"}
|
|
for _, step := range steps {
|
|
fmt.Fprintf(c.Writer, "event: log\ndata: %s\n\n", step)
|
|
flusher.Flush() // 🔑 actually push chunk
|
|
time.Sleep(1 * time.Second) // simulate work
|
|
}
|
|
})
|
|
|
|
r.Any(basePath+"/", func(c *gin.Context) { errorApiLoc(c) })
|
|
|
|
return r
|
|
}
|
|
|
|
func errorApiLoc(c *gin.Context) {
|
|
|
|
// log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{
|
|
// "endpoint": c.Request.URL.Path,
|
|
// "client_ip": c.ClientIP(),
|
|
// "user_agent": c.Request.UserAgent(),
|
|
// })
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered a route that dose not exist"})
|
|
}
|