package handler import ( "fmt" "html/template" "net/http" "git.leonardobishop.net/instancer/pkg/deployer" "git.leonardobishop.net/instancer/pkg/registry" "git.leonardobishop.net/instancer/pkg/session" ) func PostDeploy(tmpl *template.Template, dockerDeployer *deployer.DockerDeployer) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, "Invalid form data", http.StatusBadRequest) return } challenge := r.FormValue("challenge") if challenge == "" { http.Error(w, "No challenge selected", http.StatusBadRequest) return } session := r.Context().Value("session").(*session.UserSession) deployKey := dockerDeployer.StartDeploy(challenge, session.Team) tmpl.ExecuteTemplate(w, "f_deploy.html", struct { DeployKey string Challenge string }{ DeployKey: deployKey, Challenge: challenge, }) } } func DeploySSE(tmpl *template.Template, registryClient *registry.RegistryClient, dockerDeployer *deployer.DockerDeployer) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { flusher, ok := w.(http.Flusher) if !ok { http.Error(w, "Streaming unsupported", http.StatusInternalServerError) return } deployKey := r.URL.Query().Get("deploy") if deployKey == "" { http.Error(w, "No deployment specified", http.StatusBadRequest) return } w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") clientGone := r.Context().Done() job := dockerDeployer.GetJob(deployKey) if job == nil || job.DeployChan == nil { fmt.Fprintf(w, "event: %s\ndata: %s\n\n", "close", "Invalid deployment key") flusher.Flush() return } for { select { case <-clientGone: return case update, ok := <-job.DeployChan: if !ok { return } fmt.Fprintf(w, "event: %s\ndata: %s\n\n", update.Status, update.Message) flusher.Flush() } } } }