// GeneralHandle function for a simple run. It writes the file with code in // the tmp folder and returns the docker run configuration. func GeneralHandle(w http.ResponseWriter, r *http.Request) (c docker.Config) { // TODO(victorbalan): POST Method check codeData, err := getCodeDataFromRequest(r) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } for availableLanguage := range fileNames { if codeData.Language == availableLanguage { goto LANGUAGE_AVAILABLE } } http.Error(w, "language not available", http.StatusBadRequest) return LANGUAGE_AVAILABLE: c, err = docker.NewConfig(docker.NewImage(codeData.Language), "") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = ioutil.WriteFile(path.Join(c.Volume, fileNames[codeData.Language]), []byte(codeData.CodeBase), 0777) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } return }
func check(task *CodeTask, w http.ResponseWriter) (c *docker.Config) { for availableLanguage := range fileNames { if task.Language == availableLanguage { goto LANGUAGE_AVAILABLE } } http.Error(w, "language not available", http.StatusBadRequest) return LANGUAGE_AVAILABLE: c, err := docker.NewConfig(docker.NewImage(task.Language), "", task.Code) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fileName := path.Join(c.Volume, fileNames[task.Language]) if err := ioutil.WriteFile(fileName, []byte(task.Code), 0777); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } return }
// JavaUnitTest is a handle function for Java unit tests. It writes the Application.java file in //the tmp folder and returns the docker run configuration func JavaUnitTest(w http.ResponseWriter, r *http.Request) { task := decode(w, r) f := flag.NewFlagSet("taskFlags", flag.ContinueOnError) testsFlag := f.String("tests", "", "Defines the tests path") flags := strings.Split(task.Flags, " ") if len(flags) > 0 { if err := f.Parse(flags); err != nil { fmt.Printf(err.Error()) } } if *testsFlag == "" { http.Error(w, "There is no test path provided", http.StatusBadRequest) return } c, err := docker.NewConfig(docker.NewImage(task.Language), "", task.Code) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } appFilePath := path.Join(c.Volume, "src", "main", "java") os.MkdirAll(appFilePath, 777) fileName := path.Join(appFilePath, "Application.java") if err := ioutil.WriteFile(fileName, []byte(task.Code), 0777); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } testFilePath := path.Join(c.Volume, "src", "test", "java") os.MkdirAll(testFilePath, 777) testFile := path.Join(testFilePath, "TestApplication.java") testReader, err := OpenTestFile(*testsFlag) defer testReader.Close() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } test, err := ioutil.ReadAll(testReader) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := ioutil.WriteFile(testFile, test, 0777); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } rr, err := c.Run() var tests UnitTestResult fd, err := os.Open(path.Join(rr.Volume, testResultsFileName)) if err != nil { fmt.Println(err) json.NewEncoder(w).Encode(rr) return } xml.NewDecoder(fd).Decode(&tests) json.NewEncoder(w).Encode(struct { docker.Result Results UnitTestResult }{ Result: *rr, Results: tests, }) }