Skip to content

Instantly share code, notes, and snippets.

@fuziontech
Created May 29, 2014
Embed
What would you like to do?
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"os/exec"
"time"
"github.com/getsentry/raven-go"
)
var sentryDSN string = "http://f854c3521f124124af9ebd959eca3c47:28068212dfa244f4a6e85e2e44bb022d@sentry.services.disqus.com/26"
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Go Drone")
})
http.HandleFunc("/query/", hiveQuery)
//go testQueryEndpoint()
log.Fatal(http.ListenAndServe(":5000", nil))
}
func hiveQuery(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
query := r.Form["query"][0]
out, err := runHive(query, r)
if err != nil {
log.Println("Hive failed, retrying in 30 seconds")
time.Sleep(time.Duration(30) * time.Second)
out, _ = runHive(query, r)
}
fmt.Fprintf(w, "%s", out)
}
func runHive(query string, r *http.Request) (result []byte, err error) {
log.Printf("Hive Query: %s\n", query)
result, err = exec.Command("hive", "-e", query).Output()
if err != nil {
sentryClient, serr := raven.NewClient(sentryDSN, nil)
if serr != nil {
log.Fatal(serr)
}
trace := raven.NewStacktrace(0, 2, nil)
packet := raven.NewPacket(err.Error(), raven.NewException(err, trace), raven.NewHttp(r))
eventID, ch := sentryClient.Capture(packet, nil)
if cerr := <-ch; cerr != nil {
log.Fatal(cerr)
}
message := fmt.Sprintf("Captured error with id %s: %q", eventID, err)
log.Println(message)
}
return
}
func testQueryEndpoint() {
resp, err := http.PostForm("http://127.0.0.1:5000/query/", url.Values{"query": {"show tables"}})
if err != nil {
log.Fatal()
}
resp.Body.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment