Last active
June 2, 2019 09:57
-
-
Save terminalcommand/eebe9ccb9e1b701275f817293882218d 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 ( | |
"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