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.

38 lines
1.0 KiB

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
)
func server(port int) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := os.MkdirAll("cpgo_tests", 0755); if err != nil {
w.WriteHeader(http.StatusInternalServerError);
w.Write([]byte(fmt.Sprint("error", err)));
return;
}
data := map[string]interface{}{};
body, _ := io.ReadAll(r.Body);
fmt.Println("body:", string(body));
err = json.Unmarshal(body, &data); if err != nil {
w.WriteHeader(http.StatusBadRequest);
w.Write([]byte(fmt.Sprint("json err", err)));
return;
}
tests := data["tests"].([]interface{});
for idx,t := range tests {
test := t.(map[string]interface{})
input := []byte(test["input"].(string));
output := []byte(test["output"].(string));
os.WriteFile(path.Join("cpgo_tests", fmt.Sprint(idx+1) + ".input"), input, 0644);
os.WriteFile(path.Join("cpgo_tests", fmt.Sprint(idx+1) + ".output"), output, 0644);
}
os.Exit(0);
})
http.ListenAndServe(fmt.Sprintf("127.0.0.1:%v", port), nil);
}