You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.0 KiB

2 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path"
  9. )
  10. func server(port int) {
  11. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  12. err := os.MkdirAll("cpgo_tests", 0755); if err != nil {
  13. w.WriteHeader(http.StatusInternalServerError);
  14. w.Write([]byte(fmt.Sprint("error", err)));
  15. return;
  16. }
  17. data := map[string]interface{}{};
  18. body, _ := io.ReadAll(r.Body);
  19. fmt.Println("body:", string(body));
  20. err = json.Unmarshal(body, &data); if err != nil {
  21. w.WriteHeader(http.StatusBadRequest);
  22. w.Write([]byte(fmt.Sprint("json err", err)));
  23. return;
  24. }
  25. tests := data["tests"].([]interface{});
  26. for idx,t := range tests {
  27. test := t.(map[string]interface{})
  28. input := []byte(test["input"].(string));
  29. output := []byte(test["output"].(string));
  30. os.WriteFile(path.Join("cpgo_tests", fmt.Sprint(idx+1) + ".input"), input, 0644);
  31. os.WriteFile(path.Join("cpgo_tests", fmt.Sprint(idx+1) + ".output"), output, 0644);
  32. }
  33. })
  34. http.ListenAndServe(fmt.Sprintf("127.0.0.1:%v", port), nil);
  35. }