package compiler import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "time" ) type Compiler struct { Url string } type Program struct { Main string `json:"body"` } type Event struct { Message string Kind string // "stdout" or "stderr" Delay time.Duration // time to wait before printing Message } type Result struct { Errors string Events []Event } func New(url string) *Compiler { return &Compiler{url} } func (c *Compiler) Run(program *Program) (*Result, error) { jsonRequest, err := json.Marshal(program) if err != nil { return nil, err } req, err := http.NewRequest("POST", c.Url, bytes.NewBuffer(jsonRequest)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) result := &Result{} err = json.Unmarshal(body, result) if err != nil { return nil, fmt.Errorf("Error during JSON unmarshaling. The raw response body was %s. The error is %s", body, err) } return result, nil }