package main
 
import (
	"net/http"
	"os/exec"
)

func main() {
	var ttpid int 
	ttpid = 0
	http.HandleFunc("/", http.NotFound)
	//TestToolkit endpoint
	http.HandleFunc("/runtest", func(w http.ResponseWriter, r *http.Request) {
		//check if TestToolkit is running
		if (ttpid > 0) {
			w.WriteHeader(503)
		} else{
		id := r.URL.Query().Get("id")
		//Check if ID argument is given if not return 400
		if (len(id) < 1) {
			w.WriteHeader(400)
		}else {
			//Start TestToolkit, if unable to start command then return 502
			cmd := exec.Command("./test.sh", id)
			err := cmd.Start()
			if err != nil {
				w.WriteHeader(503)
			}
			w.WriteHeader(200)
			ttpid = cmd.Process.Pid
		}
	}
	})
	http.HandleFunc("/poweroff", func(w http.ResponseWriter, r *http.Request) {
		//Poweroff endpoint
		exec.Command("systemctl", "poweroff").Run()
	})
	http.ListenAndServe("127.0.0.1:5000", nil)
}