Skip to content

Instantly share code, notes, and snippets.

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