summaryrefslogtreecommitdiffstats
path: root/hyper/process/main.go
blob: ebeeb6d6c5ec83c7be03261e1e633ffbf0239c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import "json"
import "log"
import "http"
import "gorilla.googlecode.com/hg/gorilla/mux"
import "os"
import "fmt"

import "hyper/process"

var proc = map[string] *hyper.Process{}

// TODO Retrieve Process, Write, Kill [autokill], get exit code

func RespondJSON(res http.ResponseWriter, v interface{}) os.Error {
  content, err := json.Marshal(v)
  if err == nil {
    log.Printf("< %s", content)
    res.Header().Set("Content-Type", "application/json; charset=\"utf-8\"")
    res.WriteHeader(http.StatusOK)
    res.Write(content)
  } else {
    log.Printf("%s while json.Marshal(%s)", err, v)
  }
  return err
}

func CreateProcessHandler(res http.ResponseWriter, req *http.Request) {
  if p, err := hyper.NewProcess(req); err == nil {
    id := p.Id()
    proc[id] = p
    RespondJSON(res, &map[string]string{
      "path": fmt.Sprintf("/proc/%s", id),
    })
  } else {
    log.Printf("%s", err)
    res.WriteHeader(http.StatusInternalServerError)
  }
}

func RetrieveProcess(res http.ResponseWriter, req *http.Request) {
  if p := proc[mux.Vars(req)["id"]]; p != nil {
    RespondJSON(res, p)
  } else {
    res.WriteHeader(http.StatusNotFound)
  }
}

func main() {

  // Gorilla
  mux.HandleFunc("/proc", CreateProcessHandler).Methods("POST")
  mux.HandleFunc("/proc/{id}", RetrieveProcess).Methods("GET")

  err := http.ListenAndServe(":8888", mux.DefaultRouter)
  if err != nil {
    log.Fatal("ListenAndServe: ", err.String())
  }
}