Skip to content

Instantly share code, notes, and snippets.

@terminalcommand
Last active June 2, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terminalcommand/eebe9ccb9e1b701275f817293882218d to your computer and use it in GitHub Desktop.
Save terminalcommand/eebe9ccb9e1b701275f817293882218d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"html"
"log"
"net/http"
"os/exec"
"strings"
)
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/get", qhandler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<head><title>Godoc Helper</title></head>
<body>
<h1>Godoc Helper</h1>
<h2>Please enter your query</h2>
<form action="/get" method="get">
<label>Query: <input type="text" name="q" value="">
</label>
<input type="submit" value="Submit">
</form>
</body>
</html>`)
}
func qhandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Print(err)
}
q := strings.Join(r.Form["q"], "")
cmd := exec.Command("go", "doc", q)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("cmd.Run() failed with %s\n", err)
}
fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<head><title>Godoc Helper</title></head>
<body>
<h1>Godoc Helper</h1>
<h2>Please enter your query</h2>
<form action="/get" method="get">
<label>Query: <input type="text" name="q" value="">
</label>
<input type="submit" value="Submit">
</form>`)
fmt.Fprintf(w, strings.Join([]string{"<pre>", html.EscapeString(string(out)), "</pre>"}, ""))
fmt.Fprintf(w, "</html>")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment