Created
May 6, 2011 22:42
-
-
Save ashleyh/959941 to your computer and use it in GitHub Desktop.
compile server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// licensed under GPLv3, copyright 2011 aph | |
package main | |
import ( | |
"log" | |
"io" | |
"io/ioutil" | |
"http" | |
"strings" | |
"os" | |
"json" | |
) | |
type DriverResult struct { | |
Success bool | |
Result string | |
} | |
func RunDriver(driver, source string) DriverResult { | |
inR, inW, inE := os.Pipe() | |
outR, outW, outE := os.Pipe() | |
if inE != nil { | |
return DriverResult{false, "internal error: " + inE.String()} | |
} | |
if outE != nil { | |
return DriverResult{false, "internal error: " + outE.String()} | |
} | |
defer inR.Close() | |
defer inW.Close() | |
defer outR.Close() | |
defer outW.Close() | |
zooRoot := "/home/ashley/zoo/compd/" | |
argv := []string{"dash", "../drivers/"+driver} | |
envv := []string{"ZOO_ROOT="+zooRoot} | |
fds := []*os.File{inR, outW, outW} | |
prog := zooRoot + "/dash/bin/dash" | |
dir := zooRoot + "/scratch/" | |
child, err := os.StartProcess(prog, argv, envv, dir, fds) | |
if err != nil { | |
return DriverResult{false, "internal error: " + err.String()} | |
} | |
log.Println("driver: "+driver) | |
io.WriteString(inW, source) | |
inW.Close() | |
child.Wait(0) | |
outW.Close() | |
result, _ := ioutil.ReadAll(outR) | |
return DriverResult{true, string(result)} | |
} | |
func CompileServer(w http.ResponseWriter, req *http.Request) { | |
driver := req.FormValue("driver") | |
source := req.FormValue("source") | |
ok := true | |
if strings.Contains(driver, "/") { | |
ok = false | |
} | |
if ok { | |
dr := RunDriver(driver, source) | |
bytes, _ := json.Marshal(dr) | |
w.Write(bytes) | |
} else { | |
w.WriteHeader(500) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", CompileServer) | |
err := http.ListenAndServe(":7777", nil) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err.String()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment