|
| 1 | +package regexplanet |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"http" |
| 6 | +"json" |
| 7 | +"os" |
| 8 | +"runtime" |
| 9 | +"time" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | +http.HandleFunc("/", root_handler) |
| 14 | +http.HandleFunc("/status.json", status_handler) |
| 15 | +http.HandleFunc("/test.json", test_handler) |
| 16 | +} |
| 17 | + |
| 18 | +func root_handler(w http.ResponseWriter, r *http.Request) { |
| 19 | +fmt.Fprint(w, "Hello, RegexPlanet!") |
| 20 | +} |
| 21 | + |
| 22 | +type Status struct { |
| 23 | +Success bool |
| 24 | +Hostname string |
| 25 | +Getwd string |
| 26 | +TempDir string |
| 27 | +Envs []string |
| 28 | +Version string |
| 29 | +Seconds int64 |
| 30 | +} |
| 31 | + |
| 32 | +func status_handler(w http.ResponseWriter, r *http.Request) { |
| 33 | +var err os.Error |
| 34 | +status := Status{} |
| 35 | + |
| 36 | +status.Getwd, err = os.Getwd() |
| 37 | +if err != nil { |
| 38 | +status.Getwd = "ERROR!" |
| 39 | +} |
| 40 | + |
| 41 | +status.Hostname, err = os.Hostname() |
| 42 | +if err != nil { |
| 43 | +status.Hostname = "ERROR" |
| 44 | +} |
| 45 | + |
| 46 | +status.TempDir = os.TempDir() |
| 47 | +status.Envs = os.Envs |
| 48 | +status.Version = runtime.Version() |
| 49 | +status.Seconds = time.Seconds() |
| 50 | +status.Success = true |
| 51 | + |
| 52 | +var b []byte |
| 53 | +b, err = json.Marshal(status) |
| 54 | +if err != nil { |
| 55 | +return |
| 56 | +} |
| 57 | + |
| 58 | +if b[2] == 'S' {// HACK: it doesn't get much hackier than this, but json.Marshal doesn't marshal lower-case members. Is there a way around this? |
| 59 | +b[2] = 's' |
| 60 | +} |
| 61 | + |
| 62 | +w.Header().Set("Content-Type", "text/plain; charset=utf8") |
| 63 | +w.Write(b) |
| 64 | +} |
| 65 | + |
| 66 | +type TestResult struct { |
| 67 | +Success bool |
| 68 | +Html string |
| 69 | +} |
| 70 | + |
| 71 | +func test_handler(w http.ResponseWriter, r *http.Request) { |
| 72 | + |
| 73 | +w.Header().Set("Content-Type", "text/plain; charset=utf8") |
| 74 | +w.Header().Set("Access-Control-Allow-Origin", "*") |
| 75 | +w.Header().Set("Access-Control-Allow-Methods", "POST, GET") |
| 76 | +w.Header().Set("Access-Control-Max-Age", "604800") // 1 week |
| 77 | + |
| 78 | +retVal := TestResult{} |
| 79 | + |
| 80 | +retVal.Success = true |
| 81 | +retVal.Html = "<div class=\"alert alert-warning\">Actually, it is a lot less than beta: the real code isn't even written yet!</div>" |
| 82 | + |
| 83 | +var err os.Error |
| 84 | +var b []byte |
| 85 | +b, err = json.Marshal(retVal) |
| 86 | +if err != nil { |
| 87 | +fmt.Fprint(w, "{\"success\":false,\"html\":\"<p>json.Marshal failed</p>\"}") |
| 88 | +return |
| 89 | +} |
| 90 | + |
| 91 | +if b[2] == 'S' {// HACK: it doesn't get much hackier than this, but json.Marshal doesn't marshal lower-case members. Is there a way around this? |
| 92 | +b[2] = 's' |
| 93 | +} |
| 94 | + |
| 95 | +if b[17] == 'H' { |
| 96 | +b[17] = 'h' |
| 97 | +} |
| 98 | + |
| 99 | +w.Write(b) |
| 100 | +} |
0 commit comments