Пример #1
0
func main() {
	jobs := make(map[string]*Job)

	http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "pong")
	})

	http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			w.WriteHeader(405)
			return
		}

		b, _ := ioutil.ReadAll(r.Body)
		job := &Job{
			Command: string(b),
			Running: true,
		}
		id := uuid.NewRandom()
		jobs[id.String()] = job

		go func() {
			output := sandbox.Run(sandbox.Options{
				Image:   "filefrog/sandbox",
				Command: string(job.Command),
			})
			job.Output = output
			job.Running = false
		}()

		fmt.Fprintf(w, "%s", id)
	})

	http.HandleFunc("/output/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			w.WriteHeader(405)
			return
		}

		id := strings.Split(r.URL.Path, "/")[2]
		if job, ok := jobs[id]; ok {
			if job.Running {
				w.WriteHeader(204)
				return
			}

			fmt.Fprintf(w, "%s", job.Output)
			return
		}

		w.WriteHeader(404)
		fmt.Fprintf(w, "uuid %s not found", id)
	})

	http.Handle("/", http.FileServer(http.Dir("static")))
	fmt.Printf("longshoreman up and running at http://localhost:8184\n")
	fmt.Printf("  go check your browser!\n")
	http.ListenAndServe(":8080", nil)
}
Пример #2
0
func worker(in chan *Job) {
	for {
		job := <-in
		job.Output = sandbox.Run(sandbox.Options{
			Image:   "filefrog/sandbox",
			Command: string(job.Command),
		})
		job.Running = false
	}
}
Пример #3
0
func main() {
	http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "pong")
	})

	http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			w.WriteHeader(405)
			return
		}

		b, _ := ioutil.ReadAll(r.Body)
		output := sandbox.Run(sandbox.Options{
			Image:   "filefrog/sandbox",
			Command: string(b),
		})
		fmt.Fprintf(w, "%s", output)
	})

	http.Handle("/", http.FileServer(http.Dir("static")))
	fmt.Printf("longshoreman up and running at http://localhost:8184\n")
	fmt.Printf("  go check your browser!\n")
	http.ListenAndServe(":8080", nil)
}