OLD | NEW |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "crypto/md5" | 5 "crypto/md5" |
6 "database/sql" | 6 "database/sql" |
7 "encoding/base64" | 7 "encoding/base64" |
8 "encoding/json" | 8 "encoding/json" |
9 "flag" | 9 "flag" |
10 "fmt" | 10 "fmt" |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 251 |
252 // response is serialized to JSON as a response to POSTs. | 252 // response is serialized to JSON as a response to POSTs. |
253 type response struct { | 253 type response struct { |
254 Message string `json:"message"` | 254 Message string `json:"message"` |
255 StdOut string `json:"stdout"` | 255 StdOut string `json:"stdout"` |
256 Img string `json:"img"` | 256 Img string `json:"img"` |
257 Hash string `json:"hash"` | 257 Hash string `json:"hash"` |
258 } | 258 } |
259 | 259 |
260 // doCmd executes the given command line string in either the out/Debug | 260 // doCmd executes the given command line string in either the out/Debug |
261 // directory or the inout directory. Returns the stdout, and stderr in the case | 261 // directory or the inout directory. Returns the stdout and stderr. |
262 // of a non-zero exit code. | |
263 func doCmd(commandLine string, moveToDebug bool) (string, error) { | 262 func doCmd(commandLine string, moveToDebug bool) (string, error) { |
264 log.Printf("Command: %q\n", commandLine) | 263 log.Printf("Command: %q\n", commandLine) |
265 programAndArgs := strings.SplitN(commandLine, " ", 2) | 264 programAndArgs := strings.SplitN(commandLine, " ", 2) |
266 program := programAndArgs[0] | 265 program := programAndArgs[0] |
267 args := []string{} | 266 args := []string{} |
268 if len(programAndArgs) > 1 { | 267 if len(programAndArgs) > 1 { |
269 args = strings.Split(programAndArgs[1], " ") | 268 args = strings.Split(programAndArgs[1], " ") |
270 } | 269 } |
271 cmd := exec.Command(program, args...) | 270 cmd := exec.Command(program, args...) |
272 abs, err := filepath.Abs("../../out/Debug") | 271 abs, err := filepath.Abs("../../out/Debug") |
273 if err != nil { | 272 if err != nil { |
274 return "", fmt.Errorf("Failed to find absolute path to Debug dir
ectory.") | 273 return "", fmt.Errorf("Failed to find absolute path to Debug dir
ectory.") |
275 } | 274 } |
276 if moveToDebug { | 275 if moveToDebug { |
277 cmd.Dir = abs | 276 cmd.Dir = abs |
278 } else if !*useChroot { // Don't set cmd.Dir when using chroot. | 277 } else if !*useChroot { // Don't set cmd.Dir when using chroot. |
279 abs, err := filepath.Abs("../../../inout") | 278 abs, err := filepath.Abs("../../../inout") |
280 if err != nil { | 279 if err != nil { |
281 return "", fmt.Errorf("Failed to find absolute path to i
nout directory.") | 280 return "", fmt.Errorf("Failed to find absolute path to i
nout directory.") |
282 } | 281 } |
283 cmd.Dir = abs | 282 cmd.Dir = abs |
284 } | 283 } |
285 log.Printf("Run in directory: %q\n", cmd.Dir) | 284 log.Printf("Run in directory: %q\n", cmd.Dir) |
286 » var stdOut bytes.Buffer | 285 » message, err := cmd.CombinedOutput() |
287 » cmd.Stdout = &stdOut | 286 » log.Printf("StdOut + StdErr: %s\n", string(message)) |
288 » var stdErr bytes.Buffer | |
289 » cmd.Stderr = &stdErr | |
290 » cmd.Start() | |
291 » err = cmd.Wait() | |
292 » message := stdOut.String() | |
293 » log.Printf("StdOut: %s\n", message) | |
294 if err != nil { | 287 if err != nil { |
295 log.Printf("Exit status: %s\n", err.Error()) | 288 log.Printf("Exit status: %s\n", err.Error()) |
296 » » log.Printf("StdErr: %s\n", stdErr.String()) | 289 » » return string(message), fmt.Errorf("Failed to run command.") |
297 » » message += stdErr.String() | |
298 » » return message, fmt.Errorf("Failed to run command.") | |
299 } | 290 } |
300 » return message, nil | 291 » return string(message), nil |
301 } | 292 } |
302 | 293 |
303 // reportError formats an HTTP error response and also logs the detailed error m
essage. | 294 // reportError formats an HTTP error response and also logs the detailed error m
essage. |
304 func reportError(w http.ResponseWriter, r *http.Request, err error, message stri
ng) { | 295 func reportError(w http.ResponseWriter, r *http.Request, err error, message stri
ng) { |
305 log.Printf("Error: %s\n%s", message, err.Error()) | 296 log.Printf("Error: %s\n%s", message, err.Error()) |
306 http.Error(w, message, 500) | 297 http.Error(w, message, 500) |
307 } | 298 } |
308 | 299 |
309 // reportTryError formats an HTTP error response in JSON and also logs the detai
led error message. | 300 // reportTryError formats an HTTP error response in JSON and also logs the detai
led error message. |
310 func reportTryError(w http.ResponseWriter, r *http.Request, err error, message,
hash string) { | 301 func reportTryError(w http.ResponseWriter, r *http.Request, err error, message,
hash string) { |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 http.HandleFunc("/w/", workspaceHandler) | 657 http.HandleFunc("/w/", workspaceHandler) |
667 http.HandleFunc("/recent/", recentHandler) | 658 http.HandleFunc("/recent/", recentHandler) |
668 http.HandleFunc("/iframe/", iframeHandler) | 659 http.HandleFunc("/iframe/", iframeHandler) |
669 http.HandleFunc("/json/", tryInfoHandler) | 660 http.HandleFunc("/json/", tryInfoHandler) |
670 http.HandleFunc("/css/", cssHandler) | 661 http.HandleFunc("/css/", cssHandler) |
671 http.HandleFunc("/js/", jsHandler) | 662 http.HandleFunc("/js/", jsHandler) |
672 // TODO Break out /c/ as it's own handler. | 663 // TODO Break out /c/ as it's own handler. |
673 http.HandleFunc("/", mainHandler) | 664 http.HandleFunc("/", mainHandler) |
674 log.Fatal(http.ListenAndServe(*port, nil)) | 665 log.Fatal(http.ListenAndServe(*port, nil)) |
675 } | 666 } |
OLD | NEW |