Created
September 1, 2015 21:49
-
-
Save hgrubbs/03c8911bd4c0d0384997 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
"runtime" | |
"strconv" | |
"time" | |
) | |
type Response struct { | |
Output string `json:"output"` | |
} | |
var g_temp_path string | |
var g_progress_path string | |
func timeString() string { | |
// return epoch in nanoseconds prefixed by "abl_", intended for temporary file names | |
now := time.Now().UnixNano() | |
x := strconv.FormatInt(now, 10) | |
return "abl_" + x | |
} | |
func runHandler(w http.ResponseWriter, r *http.Request) { | |
_ = "breakpoint" | |
//extract db from URL, and body from request | |
querystring_args := r.URL.Query() | |
db, exists := querystring_args["db"] | |
if exists == false { | |
http.Error(w, "Missing 'db' variable from in URL", 406) | |
return | |
} | |
db_name := db[0] | |
body, _ := ioutil.ReadAll(r.Body) | |
// store 4gl query in temporary file | |
file_path := g_temp_path + timeString() + ".4gl" | |
f, _ := os.Create(file_path) | |
f.Write(body) | |
f.Close() | |
// setup ENV and shell out to progress | |
ext := exec.Command(g_progress_path) | |
ext.Args = []string{db_name, "-b", "-p", file_path} | |
ext.Env = []string{"TERM=xterm"} | |
output, err := ext.CombinedOutput() | |
os.Remove(file_path) // remove temporary file | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
// encode output to JSON | |
var m Response | |
m.Output = string(output) | |
msg, err := json.Marshal(m) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
} | |
// return JSON | |
fmt.Fprintf(w, string(msg)) | |
} | |
func main() { | |
concurrency := flag.Int("cpus", 1, "Concurrency factor for multiple CPUs") | |
temp_path := flag.String("temp_path", "/tmp/", "Path prefix for temporary query storage") | |
progress_path := flag.String("progress_binary", "_progres", "Explicit path to _progres(eg /foo/bin/_progres)") | |
flag.Parse() | |
g_temp_path = *temp_path | |
g_progress_path = *progress_path | |
runtime.GOMAXPROCS(*concurrency) | |
http.HandleFunc("/query/", queryHandler) | |
http.HandleFunc("/run/", runHandler) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment